Get child nodes values of an xml file with javascript - Stack Overflow

I have an xml file that looks like this : <Factory><Limits><Point X="92" Y=&q

I have an xml file that looks like this :

<Factory>
<Limits>
  <Point X="92" Y="489"/>
  <Point X="570" Y="487"/>
  <Point X="570" Y="138"/>
  <Point X="92" Y="140"/>
  <Point X="92" Y="139"/>
</Limits>
<Cells>
  <Cell>
    <Point X="358" Y="138"/>
    <Point X="361" Y="487"/>
    <Point X="570" Y="487"/>
    <Point X="570" Y="138"/>
    <Point X="358" Y="138"/>
  </Cell>
  <Cell>
    <Point X="311" Y="139"/>
    <Point X="311" Y="488"/>
    <Point X="92" Y="489"/>
    <Point X="92" Y="140"/>
    <Point X="311" Y="139"/>
  </Cell>
</Cells>

I'm trying to get the X and Y values of each "Point" in "Limits" (not those in "Cell") using Javascript. I saw a lot of examples with getElementByTagName, childNodes, ... but I can't figure out how to get the data I'm looking for. I thought something like this would work:

var M = xmlDoc.getElementsByTagName("Limits")[0].childNodes;
for (i=0;i<M.length;i++){
  console.log(M[i].childNodes.item(0));
}

I tried a few different things but I always end up either with a null value or with an error.

Is there any simple way to do what I need ?

I have an xml file that looks like this :

<Factory>
<Limits>
  <Point X="92" Y="489"/>
  <Point X="570" Y="487"/>
  <Point X="570" Y="138"/>
  <Point X="92" Y="140"/>
  <Point X="92" Y="139"/>
</Limits>
<Cells>
  <Cell>
    <Point X="358" Y="138"/>
    <Point X="361" Y="487"/>
    <Point X="570" Y="487"/>
    <Point X="570" Y="138"/>
    <Point X="358" Y="138"/>
  </Cell>
  <Cell>
    <Point X="311" Y="139"/>
    <Point X="311" Y="488"/>
    <Point X="92" Y="489"/>
    <Point X="92" Y="140"/>
    <Point X="311" Y="139"/>
  </Cell>
</Cells>

I'm trying to get the X and Y values of each "Point" in "Limits" (not those in "Cell") using Javascript. I saw a lot of examples with getElementByTagName, childNodes, ... but I can't figure out how to get the data I'm looking for. I thought something like this would work:

var M = xmlDoc.getElementsByTagName("Limits")[0].childNodes;
for (i=0;i<M.length;i++){
  console.log(M[i].childNodes.item(0));
}

I tried a few different things but I always end up either with a null value or with an error.

Is there any simple way to do what I need ?

Share Improve this question asked Mar 6, 2013 at 11:47 phil vdphil vd 612 silver badges6 bronze badges 4
  • 1 is needs to be like this <Point>data</Point> – coolguy Commented Mar 6, 2013 at 11:51
  • 1 can you use jquery solution?? – Manish Mishra Commented Mar 6, 2013 at 11:52
  • Yes, I could use jquery. – phil vd Commented Mar 6, 2013 at 12:37
  • @ubercooluk Sadly the xml is generated by another application and I can't change the way it's made. – phil vd Commented Mar 6, 2013 at 12:57
Add a ment  | 

2 Answers 2

Reset to default 3

You need to get the attributes. With javascript:

M[i].getAttribute("X");

with jQuery:

M[i].attr("X");
var M = xmlDoc.getElementsByTagName("Limits");
for (i = 0; i < M.length; i++){
   for(var j = 0; j < M[i].childNodes.length; j++){
      console.log(M[i].childNodes[j].getAttribute('X'));
      console.log(M[i].childNodes[j].getAttribute('Y'));
   }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745149153a4613787.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信