Jump to content

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 post
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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...