Forum Moderators: coopster
<links>
<category id="1">
<link id="454fgf" url="http://someurl.com" text="Link Text" />
<link id="g645fg" url="http://anotherurl.com" text="Link Text 1" />
<link id="gffgr4" url="http://url.com" text="Link Text 2" />
</category>
<category id="2">
<link id="5err44" url="http://someurl.com" text="Link Text" />
<link id="453435" url="http://anotherurl.com" text="Link Text 1" />
<link id="6g435d" url="http://url.com" text="Link Text 2" />
</category>
<category id="3">
<link id="3454ds" url="http://someurl.com" text="Link Text" />
<link id="45fged" url="http://anotherurl.com" text="Link Text 1" />
<link id="4dsey5" url="http://url.com" text="Link Text 2" />
</category>
</links>
I wish to post link details via a form and insert them directly into the xml. My script so far is as follows:
$xmlfile = "links.xml";
$linkID = substr(md5(uniqid(mt_rand(), true)), 0, 6);
$dom = new DOMDocument();
$dom->load($xmlfile);
$xpath = new DomXPath($dom);
//$root = $dom->firstChild;
$root = $xpath->query("//*category[@id='".$_POST['categoryIDNumber']."']");
$link = $dom->createElement('link');
$identifier = $dom->createAttribute("id");
$link->appendChild($identifier);
$idValue = $dom->createTextNode($linkID);
$identifier->appendChild($idValue);
$url = $dom->createAttribute("url");
$link->appendChild($url);
$urlValue = $dom->createTextNode($_POST['linkURL']);
$url->appendChild($urlValue);
$text = $dom->createAttribute("text");
$link->appendChild($text);
$textValue = $dom->createTextNode($_POST['linkText']);
$text->appendChild($textValue);
$link->appendChild($url);
$link->appendChild($text);
$root->appendChild($link);
$dom->save($xmlfile);
If i use the commented out $root = $dom->firstChild; the child is created and appended as the last record in the xml file without issue. However, I run into problems when I attempt to append the child under a parent category. Can anyone figure out how I can append a child to a specific category?
Regards
Dan