Jump to content

DOM XML parsing help


mpsn

Recommended Posts

Hi, I want this function to output all the nodes of the email.xml file. But for some reason, it only outputs "ELEMENT NODE: email"

 

Please see the first post in this SimpleXML thread and the PHP help topic is SimpleXML whereas this one is just pure DOM BUT both are using email.xml (please see link below) or if it is broken, please search for PHP help topic: Need help with SimpleXML to check if node has attributes

 

http://www.phpfreaks.com/forums/index.php?topic=346028.0

 

Here is the script:

<?php
/*FOR DOM */
$dom=new DOMDocument();
$dom->load("email.xml");

function writeXMLtoScreenViaDOM($dom) {

//print current tag node names 
//	if current tag node has whitespace, go to the next sibling that's
// guaranteed to be a tag node
if(trim($dom->firstChild->nodeName)=="") {
	$dom=$dom->nextSibling;
}
print "<strong>ELEMENT NODE:</strong>".$dom->nodeName."<br />";

//print the current tag node's text node child if any
if($dom->nodeType==XML_TEXT_NODE) {
	if(trim($dom->nodeValue)=="") 
		print "<strong>TEXT NODE:</strong> has child tag nodes.<br />";
	else 
		print "<strong>TEXT NODE:</strong>".$dom->nodeValue."<br />";	
}
//print any attributes
// NB: later make sure to skip over EMPTY ATTRIBUTE NODE VALUES
if($dom->hasAttributes()) {
	for($i=0;$i<$dom->length;$i++) {
		print "<strong>ATTRIBUTE NODE:</strong>".$dom->attributes->item($i)->nodeValue."<br />";
	}
}

//check if any child tag nodes
if($dom->hasChildNodes()) {
	//NB: think need for loop to know what the current index in item(index) is
	foreach($dom->childNodes->item(0) AS $curNode)
		writeXMLtoScreenViaDOM($curNode);
}

}//END FCN writeXMLtoScreenViaDOM

writeXMLtoScreenViaDOM($dom->documentElement);
?>

 

Please any help is appreciated!

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.