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
https://forums.phpfreaks.com/topic/249854-dom-xml-parsing-help/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.