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

admin2025-04-20  0

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'));
   }
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745149154a287521.html

最新回复(0)