Jump to content

Dom Problem Parsing Empty Tag


suddenzero
 Share

Recommended Posts

Hello All,I am having an issue using DOM to parse a XML document that is produced by a third party website. This XML document is a valid XML document. I have saved a copy of the XML and checked it to make sure it is valid. The parsing script I am working on uses a recursive method to retrieve a parent node and all of the nodes within the parent and its children. However, when the script gets to an empty node i.e. (<NodeName />) it bombs. An example of the script is as follows:/*********************************** Code Starts Here *********************************************/<html><head><script type="text/javascript">var parentNodeArray;var childNodeArray;var xmlhttp;var siblingArray = [];var finalArray = [];function loadXMLDoc(url){xmlhttp=null;xmlhttp=new XMLHttpRequest(); if (xmlhttp!=null){ xmlhttp.onreadystatechange=state_Change; xmlhttp.open("GET",url,true); xmlhttp.send(null); }}function state_Change(){if (xmlhttp.readyState==4){ if (xmlhttp.status==200){ //Getting the desired metric parentNodeArray=xmlhttp.responseXML.documentElement.getElementsByTagName("AC04"); //Getting child nodes of metric childNodeArray=parentNodeArray[0].childNodes; //looping through the array for(var i=0; i < childNodeArray.length; i++){ if(!childNodeArray.firstChild.hasChildNodes){ document.write(childNodeArray.firstChild.nodeValue + "<br/>"); }else{ var childNode=childNodeArray.firstChild; siblingArray[0] = childNode; var finalArray = GetAllSiblings(childNode); for(var y = 0; y < finalArray.length; y++){ var grandChildNodeArray = finalArray[y].childNodes for(var z = 0; z < grandChildNodeArray.length; z++){ document.write(grandChildNodeArray[z].firstChild.nodeValue+"<br/>"); } } siblingArray = []; } } }else{ alert("Problem retrieving XML data:" + xmlhttp.statusText); } }}/*********** getAllSiblings ******************/function GetAllSiblings(node){ var sibling = node.nextSibling; while(sibling != null){ siblingArray[siblingArray.length] = sibling; return GetAllSiblings(sibling); } return siblingArray;}</script></head><body onload="loadXMLDoc('http://www.helpussaveamerica.org/davesXML.xml')"></body></html>/*********************************** Code Ends Here *********************************************/If one were to copy paste the above code into an html application (a file ending in .hta for those of you that aren't familiar with html applications) when opened the application would start and parse all of the values out up to the empty tag and then it will bomb stating object required. I need to find a way to get past the empty tag without editing the xml as I cannot edit the original on the third party site. Any help with this will be greatly appreciated.Thanks,

Link to comment
Share on other sites

An empty element shouldn't make a difference. I can't see where in your script that would happen. Are you sure you're accessing the right node?There's a problem in your getAllSiblings function: the while loop is endless. Add this line:

function GetAllSiblings(node){  var sibling = node.nextSibling;  while(sibling != null){	siblingArray[siblingArray.length] = sibling;	/***********************/	sibling = sibling.nextSibling;	/***********************/	return GetAllSiblings(sibling);  }  return siblingArray;}

Edited by Ingolme
Link to comment
Share on other sites

Use "nodeValue" or "innerHTML" (whichever works; prefferably the first) directly on the element, not on its "firstChild". And forget about "firstChild" altogether actually, as it's simple - empty elements have no child node, including a first one.If you want to loop over all elements, and do pretty much the same thing, why not use getElementsByTagName("*")? Why do you need the recursivity?

Edited by boen_robot
Link to comment
Share on other sites

This is not true because this is a recursive method and eventually the call to GetAllSiblings(sibling); in the return statement will pass in null when the previously passed in node has no siblings. Thanks anyway. Recursion can be tricky. I have tested this code with several XML files that do not have empty tags and the loop ends exactly where it is supposed to. The problem is if a tag is declared like this <TagName /> the DOM functionality doesn't know what to do when get child is called because there is not an opening and closing tag.

An empty element shouldn't make a difference. I can't see where in your script that would happen. Are you sure you're accessing the right node?There's a problem in your getAllSiblings function: the while loop is endless. Add this line:
function GetAllSiblings(node){  var sibling = node.nextSibling;  while(sibling != null){	siblingArray[siblingArray.length] = sibling;	/***********************/	sibling = sibling.nextSibling;	/***********************/	return GetAllSiblings(sibling);  }  return siblingArray;}

Link to comment
Share on other sites

Well I am fairly new to XML and didn't realize that you could use getElementsByTagName("*") to get all the Elements. This will probably work for me with some manipulation. Thanks,

Use "nodeValue" or "innerHTML" (whichever works; prefferably the first) directly on the element, not on its "firstChild". And forget about "firstChild" altogether actually, as it's simple - empty elements have no child node, including a first one.If you want to loop over all elements, and do pretty much the same thing, why not use getElementsByTagName("*")? Why do you need the recursivity?
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...