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!
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!