Jump to content

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 />";
}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.