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

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,660
    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,959
    Rep Power
    1014
    Hi,

    an XML document always has a single root node (you have three).
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,660
    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,959
    Rep Power
    1014
    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''')); 
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo