Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    May 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    DOM Manipulation in PHP 5

    Last time I posted this question, thirty two people looked at it and nobody responded so maybe a simpler example of my problem will be easier to digest.

    I have an xml file I want to load, and add an item to. This is the xml file, very very simple.

    Code:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <catalog>
    	<item>
    		<name>Item One</name>
    		<price>55.00</price>
    	</item>
    	<item>
    		<name>Item Two</name>
    		<price>43.34</price>
    	</item>
    	<item>
    		<name>Item Three</name>
    		<price>23.53</price>
    	</item>
    	<item>
    		<name>Item Four</name>
    		<price>13.11</price>
    	</item>	
    </catalog>
    This is the PHP that does the transformation:

    PHP Code:
    $catalog = new DomDocument;
    $catalog->load("testcat.xml");
    $item $catalog->createElement("item");
    $name $catalog->createElement("name","Item Name");
    $price $catalog->createElement("price","44.12");
    $item->appendChild($name);
    $item->appendChild($price);
    $cat $catalog->documentElement;
    $catalog $cat->appendChild($item);
    print 
    $catalog->savexml(); 
    This code generates this error, line 21 is the last line:

    Fatal error: Call to undefined method DOMElement::savexml() in /home/.kentucky/matgilbert/shortmat.net/catalog/testxml.php on line 21

    If I remove the $cat = $catalog->documentElement; and just go straight to $catalog->appendChild($item), the new item node gets added AFTER the ending </catalog> tag, which is completely useless to me.

    Example:
    Code:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <catalog>
    	<item>
    		<name>Item One</name>
    		<price>55.00</price>
    	</item>
    	<item>
    		<name>Item Two</name>
    		<price>43.34</price>
    	</item>
    	<item>
    		<name>Item Three</name>
    		<price>23.53</price>
    	</item>
    	<item>
    		<name>Item Four</name>
    		<price>13.11</price>
    	</item>	
    </catalog><item><name>newitem</name><price>11.24</price></item>
    I am really stumped on how to set a variable to the root node of an xml document. It's rediculous that there are no decent tutorials on doing this on the net (I've been looking for days) and I intend on publishing one... that is once I figure it out. =P

    Hope somebody actually replies this time!

  • #2
    Regular Coder
    Join Date
    Apr 2003
    Location
    Montreal, QC
    Posts
    340
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Hi,

    First, I'm no expert with PHP DOM at all, actually I just came here looking for some help with it myself

    I've been working a lot with Javascript DOM manipulation though, and applying that knowledge to PHP here's where I think you're going wrong:

    I think you're pulling out the documentElement, appending the new item to it, then not re-appending it back onto the document itself.

    Perhaps if you try something like this:
    PHP Code:
    $cat $catalog->documentElement;
    $cat->appendChild($item);
    $catalog->appendChild($cat); 
    Another possibility is that in the line
    PHP Code:
    $cat $catalog->documentElement
    perhaps you need to specify that <catalog> is the documentElement?

    Hope this helps.

    John
    Search for Laughter or Just Search?
    GiggleSearch.org
    Blog: www.johnbeales.com
    All About Ballet: www.the-ballet.com

  • #3
    New to the CF scene
    Join Date
    May 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks johnnyb, that totally worked! I have the issue of whitespace, but as far as parsing the document and applying xslt to it, it works for now!

    Here is the code that worked:
    PHP Code:
    $item $catalog->createElement("item");
    $name $catalog->createElement("name","Item Name");
    $price $catalog->createElement("price","44.12");
    $item->appendChild($name);
    $item->appendChild($price);
    $cat $catalog->documentElement;
    $cat->appendChild($item);
    $catalog->appendChild($cat);
    print 
    $catalog->savexml(); 
    It seems kinda strange that after you have to define the document element (root node) as a seperate variable, you have to add it to the original dom document again. The above code works, but it looks to me like it should be creating a duplicate of the entire document with the new node added in. Whatever, I'm not going to look a gift horse in the mouth

    Also, I adapted the concept you showed here to my project (a bit more complex) and it works! johnnyb yr my hero!

  • #4
    Regular Coder
    Join Date
    Jun 2004
    Posts
    565
    Thanks
    0
    Thanked 18 Times in 18 Posts
    Your error lies here:
    PHP Code:
    $catalog $cat->appendChild($item); 
    The appendChild() method returns the node added. And thus $catalog holds now a DOMNode object and not a DOMDocument object anymore. Then you are trying to call the saveXML() method on $catalog, which is only implemented by DOMDocument objects.

    dumpfi
    "Failure is not an option. It comes bundled with the software."
    ....../)/)..(\__/).(\(\................../)_/)......
    .....(-.-).(='.'=).(-.-)................(o.O)...../<)
    ....(.).(.)("}_("}(.)(.)...............(.)_(.))Ż/.
    ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
    Little did the bunnies suspect that one of them was a psychotic mass murderer with a 6 ft. axe.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •