...

View Full Version : DOM Manipulation in PHP 5



drgonzo
11-06-2006, 05:55 PM
Last time I posted this question (http://www.codingforums.com/showthread.php?t=99908), 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.


<?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:


$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:
<?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!

johnnyb
11-06-2006, 06:08 PM
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:


$cat = $catalog->documentElement;
$cat->appendChild($item);
$catalog->appendChild($cat);


Another possibility is that in the line

$cat = $catalog->documentElement;
perhaps you need to specify that <catalog> is the documentElement?

Hope this helps.

John

drgonzo
11-06-2006, 06:32 PM
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:
$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!

dumpfi
11-06-2006, 08:37 PM
Your error lies here:

$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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum