XML Question

Hi people,

Another moment of frustruation which I hope this community can help with. I am working with an XML document that looks something like this.

		<page parent="" order="1" name="Home" permalink="/" path="" />
		<page parent="" order="2" name="About" permalink="/about-us/" path="" />
			<page parent="About" order="3" name="The Team" permalink="/about-us/the-team/" path="" />
			<page parent="About" order="1" name="Mission Stement" permalink="/about-us/xxx/" path="" />
				<page parent="Mission Stement" order="2" name="Third Level Tier" permalink="/about-us/yyy/sss/" path="" />
			<page parent="About" order="2" name="Test" permalink="/about-us/yyy/" path="" />
			<page parent="About" order="1" name="ZZZZZZ" permalink="/about-us/xxx/" path="" />
		<page parent="" order="1" name="Services" permalink="/services/" path="" />

I need to add an XML node to a specific location. Here is an example if I create a new page and set its parent to be “About” ideally I would want it to appear underneath the xml node with the name=“About”. I can select the parent node the issue I have is appending my new node directly underneath. Here is my example code:

$xmlDoc = new DOMDocument();
		$xpath = new DOMXPath($xmlDoc);
		// Create New Tag
		$newPage = $xmlDoc->createElement('page');
		$newPage->setAttribute("parent", $parent);
		$newPage->setAttribute("order", $xmlOptions['order']);
		$newPage->setAttribute("name", $xmlOptions['name']);
		$newPage->setAttribute("permalink", $xmlOptions['permalink']);
		$newPage->setAttribute("path", $xmlOptions['path']);
                        $query = "//site/pages/page[@name='About']";
			$pages =  $xpath->query($query);
			$XMLparent = $pages->item(0);
			echo $xmlDoc->saveXML();

Any Ideas Folks? I will love you forever

Instead of appendTo use insertBefore on the first child.

So something like this

$query = "//site/pages/page[@name='$parent']";
$pages =  $xpath->query($query);
$XMLparent = $pages->item(0);
// Get The First Child of XML parent
$xmlDoc->insertBefore($newPage, $XMLparent->firstChild);
echo $xmlDoc->saveXML();

Got it

$query = "//site/pages/page[@parent='$parent']";
			$pages =  $xpath->query($query)->item(0);
			$pages->parentNode->insertBefore($newPage, $pages);
			echo $xmlDoc->saveXML();