Jump to content

[SOLVED] extra line in xml


corillo181

Recommended Posts

i have this code to insert context into xml

the problem is when the context is inserted a extra blank line is inserted..

 

and if you know about xml you can't have no extra blank line..

 

this is the code

 

$dom = new DomDocument();
$dom->load('xml.xml');


$books= $dom->appendChild($dom->createElement('artists'));
$book= $books->appendChild($dom->createElement('artist'));
$tittle= $book->appendChild($dom->createElement('tittle'));
$tittle->appendChild($dom->createTextNode('the malo'));

$dom->ignoreWhite = true;
$dom->formatOutput = true;
$dom->save('xml.xml');

 

and this is the result

1 <?xml version="1.0"?>
2 <artists>
3  <artist>
4    <tittle>the malo</tittle>
5  </artist>
6 </artists>
7

Link to comment
https://forums.phpfreaks.com/topic/66716-solved-extra-line-in-xml/
Share on other sites

i have this code to insert context into xml

the problem is when the context is inserted a extra blank line is inserted..

 

and if you know about xml you can't have no extra blank line..

 

this is the code

 

$dom = new DomDocument();
$dom->load('xml.xml');


$books= $dom->appendChild($dom->createElement('artists'));
$book= $books->appendChild($dom->createElement('artist'));
$tittle= $book->appendChild($dom->createElement('tittle'));
$tittle->appendChild($dom->createTextNode('the malo'));

$dom->ignoreWhite = true;
$dom->formatOutput = true;
$dom->save('xml.xml');

 

and this is the result

1 <?xml version="1.0"?>
2 <artists>
3  <artist>
4    <tittle>the malo</tittle>
5  </artist>
6 </artists>
7

 

I'm not the best with xml, but does that matter?  Perhaps PHP just goes onto the next line ready to input data

 

http://uk.php.net/manual/en/ref.dom.php

 

 

the problem with living a blank line is that next time you try to read the code is going throw a error

 

which is

 

Warning: DOMDocument::load() [function.DOMDocument-load]: Extra content at the end of the document in file:///C%3A/wamp/www/xml.xml, line: 7 in C:\wamp\www\Untitled-2.php on line 5

does any one at least knows how to read xml ?

 

i used xpath to make a query now i want to get only the name or what ever else i want, but it only gives me

all the values with the riquired id.

 

$dom = new DomDocument;
$dom->load('test2.xml');

$xp = new domxpath($dom);
$query = "/artists/artist[@id=1]";

$artist = $xp->query($query);

foreach($artist as $name){

$xml = new SimpleXMLElement($name);  

print $xml->getElementsByTagName('name');

}

I assume you mean this one

 

http://www.ibm.com/developerworks/opensource/library/os-xmldomphp/

 

Just to round off the topic

 

<?php
/**
* data
*/
$tArray = array(1=>'The Tempest', 'Romeo and Juliet', 'Loves Labours Lost', 'King Henry V');

/**
* create the xml file
*/
$dom = new DomDocument();
$dom->formatOutput = true;

$books= $dom->appendChild($dom->createElement('artists'));

foreach ($tArray as $id=>$t)
{
    $book= $books->appendChild($dom->createElement('artist'));
    $book->setAttribute('id', $id);
    $tittle= $book->appendChild($dom->createElement('tittle'));
    $tittle->appendChild($dom->createTextNode($t));
}

$dom->save('xml.xml');

/**
* read it
*/
$xml = simplexml_load_file('xml.xml');

foreach ($xml->artist as $a)
{
    echo "{$a['id']} : $a->tittle<br />";
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.