Re: >> Very Simple PHP DOM Question..Please help!
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.php archive

Re: >> Very Simple PHP DOM Question..Please help!

From: Azeus <IFARTINYOURGENERALDIRECTION@MP.com>
Date: Tue Jul 05 2005 - 17:34:08 CEST

Try this:

<?php
        $dom = new DOMDocument("1.0","iso-8859-1");

        /* Create all elements */
        $comment = $dom->createElement("comment"); //root element
        $user = $dom->createElement("user", "Carl");
        $date = $dom->createElement("date", "1-1-2005");

        /* Now place them in the correct place in the tree */

        $comment->appendChild($user); // User is a child of Comment
        $comment->appendChild($date); // Date is a child of comment
        $dom->appendChild($comment); // Comment is a child of rootdoc

        echo $dom->saveXML();
?>

It outputs :
<?xml version="1.0" encoding="iso-8859-1"?>
<comment>
             <user>Carl</user>
        <date>1-1-2005</date>
</comment>

Make sure you append children according to their place in the hierarchy.

gene.ellis@gmail.com wrote:
> Actually I just erased it. But it was basically using these functions:
>
> DOMDocument->createElement()
> DOMNode->appendChild()
>
> My resulting file ends up resembling this:
> <comment><user>john<date>some date</user></date></comment>
>
> So I understand how to do everything except for getting these tags to
> close in the right place. any idea what I could be doing wrong? How
> could I use those two functions to create a node tree that looks like:
> <comment><user>john</user><date>some date</date></comment>
>
> Thanks a lot for your help. I have been racking my brains on this for a
> long time
>
Received on Mon Oct 17 21:02:37 2005