Jump to content

PHP DOM XML replaceChild


Jay@TastefulTitles.com
 Share

Recommended Posts

At Ingolme's suggestion, I've switched from simpleXML to DOMXML for this because I need to be able to replace a node. I've done my best to follow PHP.com examples, and everything seems to work up until the point of the actual replacement. I hope you can spot what's keeping it from working.

The vars found in the heredoc are assigned earlier from the customer input form. if $found is positive, it contains a cId (customer ID) that exists in the DB and should be updated with any new info (code shown). If negative, it's abs() is the cId for a new node to be added (code omitted here).

In $xml, the <customer> node is child of <customers>, which is child of <root>.

If I comment out the replaceChild line before the break, execution continues unhindered. Nodename reports "customer" and nodetype reports 'XML_ELEMENT_NODE' for both $node and $oldNode, so I don't know what is preventing the replacement.

//			SAVE CUSTOMER DATA			//
	$found= $cId;
	$cId	= abs($found);	
	//		Create DOM from data file
	$xml = new DOMDocument;
	$xml->formatOutput = true;
	$xml->load("Titles data.xml");
	
	//		Create node from customer input
	$str	= <<<XX
		<customer>
			<cId>$cId</cId>
			<organization>$organization</organization>
			<website>$URL</website>
			<contact>$name</contact>
			<email>$email</email>
			<phone>$phone</phone>
			<addr>$address</addr>
			<city>$city</city>
			<state>$state</state>
			<zip>$zip</zip>
			<orders>
				<oId></oId>
			</orders>
		</customer>
XX;
	$DOMChild = new DOMDocument;
	$DOMChild->loadXML($str);					//turn $str into DOMDoc
	$node = $DOMChild->documentElement;				//not sure if this is right, example suggests it is
	$node = $xml->importNode($node, true);				//Is this necessary? examples suggest it is
	
	if ($found > 0) {						//if positive, $found contains cId of desired customer
		$list = $xml->getElementsByTagName("cId");		//list of ID's to search
		for ($r = $list->length -1; $r > 0; $r--) {		//find customer that fits cId
			if ($list->item($r)->nodeValue==$cId) {		//when found, $r is index to customer node list
				$oldnode=$xml->getElementsByTagName("customer")->item($r);// get node to be replaced
				$msg=$r."—";				//$msg is included in email sent later; shows arrival here
				$node->replaceChild($node, $oldNode);	//THIS LINE FAILS, HALTING EXECUTION
				break;
			}
		} 
	}

 

Link to comment
Share on other sites

So I do. Thanks for catching that!

I was so hopeful that correcting the var name would solve the problem, but there is no change. That line now looks like this:

				$node->replaceChild($node, $oldnode);		//THIS LINE FAILS

I think that may have been a relatively recent error, as I changed some var names to make it easier to compare with the example code, but its failure predates that. I hope you can take another look and find what else is screwed up.

Logically, there must be some problem with the params I'm sending in this line, because the previous line executes without problem, and if I comment this line out, everything thereafter runs. My understanding of the DOM may still be a little fuzzy, so I can't see what isn't right. Using textContent on both vars shows exactly what I would expect.

I even tried simply retyping the line, because I've seen instances where that made a difference, but not this time.

Link to comment
Share on other sites

Solved! 😀

My brain had a sudden flash, so I decided to try something. Lo and behold, it worked! Here's what it took:

		$xml->getElementsByTagName("customers")->item(0)->replaceChild($node, $oldnode);

I had not understood from the examples that I have to start with the parent node to replace a child node. Seems logical now that I've found it, but I sure didn't see it before. 

I doubt I could have got there if you hadn't found my var error, so many thanks for that.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...