最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

how to extract values from an XML document using Javascript - Stack Overflow

programmeradmin6浏览0评论

I am trying to extract values from the xml document and print them. I also want to count the number of children(child nodes) each node has.That is the first tag has 2 child and second tag has 3.

THIS IS THE XML DOCUMENT

<?xml version="1.0" ?> 
  <A>
  <a1>a1</a1> 
  <a2>a2</a2> 
  <B>
  <C>2</C> 
  <C>3</C> 
  </B>
  <B>
  <C>4</C> 
  <C>5</C> 
  <C>6</C>
  </B>
  </A>

THIS IS MY JAVASCRIPT DOCUMENT

if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.open("GET","extractexample.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
xmlObj=xmlDoc.documentElement;
document.write(xmlDoc.getElementsByTagName("B")[0].childNodes[0].nodeValue);

I am trying to extract values from the xml document and print them. I also want to count the number of children(child nodes) each node has.That is the first tag has 2 child and second tag has 3.

THIS IS THE XML DOCUMENT

<?xml version="1.0" ?> 
  <A>
  <a1>a1</a1> 
  <a2>a2</a2> 
  <B>
  <C>2</C> 
  <C>3</C> 
  </B>
  <B>
  <C>4</C> 
  <C>5</C> 
  <C>6</C>
  </B>
  </A>

THIS IS MY JAVASCRIPT DOCUMENT

if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.open("GET","extractexample.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
xmlObj=xmlDoc.documentElement;
document.write(xmlDoc.getElementsByTagName("B")[0].childNodes[0].nodeValue);
Share Improve this question asked Mar 24, 2011 at 6:17 swati guptaswati gupta 111 gold badge2 silver badges4 bronze badges 4
  • What is the issue you are facing? – slifty Commented Mar 24, 2011 at 6:25
  • Are you getting the AJAX result? I mean, XML file in your xmlhttp.responseXML. Check with firebug. – JS Mitrah Commented Mar 24, 2011 at 7:51
  • hi if i write document.write(xmlDoc.getElementsByTagName("C").length); This counts the total number of tags I get the output as 5 whichis correct but i want to seperately count number of child nodes of each tag . I dont know what code shall i write for it. – swati gupta Commented Mar 24, 2011 at 7:59
  • please edit your question to include your last ment – syockit Commented Mar 24, 2011 at 9:47
Add a ment  | 

1 Answer 1

Reset to default 3

Element.childNodes method returns all types of nodes, including whitespace textnodes. It may not be what you want. If you only care for the number of child elements, use childElementCount.

var b = xmlDoc.getElementsByTagName("B")[0];
alert(b.childElementCount); //should output 2

I haven't tried in IE, it may not work. Else, if you want a the element list, use children children not supported on non HTML doc. You can try this function:

function getChildren(element) {
  var nodes = element.childNodes;
  var children = [];
  for (var i = 0; i < nodes.length; i++) {
    if (nodes[i].nodeType == Node.ELEMENT_NODE) children.push(nodes[i]);
  }
  return children;
}

getChildren(b).length;
发布评论

评论列表(0)

  1. 暂无评论