#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Posts
    2,566
    Rep Power
    171

    Creating xml from array. XML Parsing Error: junk after document element


    Hello why do I get the error?
    PHP Code:
    <?php header('Content-Type:text/xml');
    $dom = new DOMDocument('1.0''utf-8');
    $members =  array('behnam''alext','tiga');
    foreach(
    $members as $val)
        {
            
    $element $dom->createElement('name',$val);
            
    $dom->appendChild($element);
        }
    echo 
    $dom->saveXML();?>
    Thanks
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,910
    Rep Power
    1045
    Hi,

    an XML document always has a single root node (you have three).
  4. #3
  5. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Posts
    2,566
    Rep Power
    171
    Originally Posted by Jacques1
    Hi,

    an XML document always has a single root node (you have three).
    Thanks Jacques1.

    This is pretty basic stuff but got a just a general wondering. I expect the code below to show me open and close tags of <names> and <names/> but I only get the close tag <names/>! What happens to open tag?
    PHP Code:
    <?php header('Content-Type:text/xml');
    $dom = new DOMDocument('1.0''utf-8');
    $book $dom->appendChild($dom->createElement('names'));
    echo 
    $dom->saveXML();?>
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,910
    Rep Power
    1045
    How is the class supposed to know that you want a non-empty element without content rather than an empty element?

    You need to set the content either in the method call or with the nodeValue property:

    PHP Code:
    $book $dom->appendChild($dom->createElement('names''')); 

IMN logo majestic logo threadwatch logo seochat tools logo