
August 17th, 2011, 08:18 PM
|
|
Registered User
|
|
Join Date: Aug 2011
Posts: 18
Time spent in forums: 2 h 36 m 55 sec
Reputation Power: 0
|
|
|
Adding nodes to XML
I am adding nodes to an XML file. Here is the structure.
Code:
<COMMUNITIES>
<COMMUNITY ID="c001">
<URLS>
<URL ID="u001">
<NAME>Google.com</NAME>
<URL>http://www.google.com</URL>
</URL>
</URLS>
</COMMUNITY>
</COMMUNITIES>
I want to update the URLS node to add a new URL.
Code:
<URL ID="u002">
<NAME>Yahoo.com</NAME>
<URL>http://www.yahoo.com</URL>
</URL>
Here is the PHP script.
Code:
function add_url( $nodeid, $urlid, $urlname, $urllink ) {
$dom = new DOMDocument();
$dom->load('communities.xml');
$dom->formatOutput = true;
$dom->preserveWhiteSpace = true;
// get document element
$xpath = new DOMXPath($dom);
$nodes = $xpath->query("//COMMUNITY[@ID='$nodeid']");
if ($nodes->length) {
$node = $nodes->item(0);
$xurl = $dom->createElement("URL");
$xurl->setAttribute("ID", $urlid);
$xuname = $dom->createElement("NAME");
$xunameText = $dom->createTextNode(mysql_escape_mimic($urlname));
$xuname->appendChild($xunameText);
$xulink = $dom->createElement("URL");
$xulinkText = $dom->createTextNode(mysql_escape_mimic($urllink));
$xulink->appendChild($xulinkText);
$xurl->appendChild($xuname);
$xurl->appendChild($xulink);
$xurls = $xpath->query("//COMMUNITY[@ID='$nodeid']/URLS");
if ($xurls->length) {
}
else {
$xurls = $dom->createElement("URLS");
}
$xurls->appendChild($xurl);
}
/* $dom->asXML('communities.xml'); */
$dom->save('communities.xml');
}
The function runs through. The variables coming in are OK. The calls to appendChild don't cause errors. So I am wondering what might be the problem.
|