Home > Software engineering >  parsing duplicate tags in xml javascript
parsing duplicate tags in xml javascript

Time:06-28

I wrote the code below to basically get the data from 'point' and print it, so if I select the tagname 'point' it is printing all the coordinates in the tag.

But I want to parse through each 'points' tag in a for loop such that the first iteration of the loop prints the all the coordinates in from first 'points' tag and second iteration prints the coordinates from second 'points' tag and so on,

and I wrote the code accordingly but it is not consoling anything,I even tried .eq() and nth child but the problem is I have multiple 'points' tag

...

 <object>
        <coordinate>pixel</coordinate>
        <type>rectangle</type>
        <description>None</description>
        <possibleresult>
            <name>Intersection</name>
        </possibleresult>
        <points>
            <point>684.999939,182.999985</point>
            <point>682.568420,217.040604</point>
            <point>612.568420,212.040604</point>
        </points>
    </object>
    

...

...

 fetch("file.xml")
 .then(response => response.text())
 .then(data => {
   var txt;
   txt="";
   const parser = new DOMParser();
  const xml = parser.parseFromString(data, "application/xml");
  console.log(xml);
  x = xml.getElementsByTagName("points point");
  for (i = 0; i< x.length; i  ) {

 txt  = x[i].childNodes[0].nodeValue  "\n";
}
 console.log(txt.split("\n")[0]); //prints the first point tag
console.log(txt);
// $(document).ready(function(){
//var x= $("point:nth-child(1)").text();

console.log(x);
});

})
 .catch(console.error);

...

CodePudding user response:

parseFromString returns instance of Document where you can use provided functions to get all children in various ways.

Here's a POC:

const data = "<root><object><coordinate>pixel</coordinate><type>rectangle</type><description>None</description><possibleresult><name>Intersection</name></possibleresult><points><point>684.999939,182.999985</point><point>682.568420,217.040604</point><point>612.568420,212.040604</point><point>614.999939,177.999985</point><point>684.999939,182.999985</point></points></object><object><coordinate>pixel</coordinate><type>rectangle</type><description>None</description><possibleresult><name>Small Car</name></possibleresult><points><point>1.000008,267.000000</point><point>-8.266037,257.072083</point><point>-0.000004,249.357117</point><point>9.266041,259.285034</point><point>1.000008,267.000000</point></points></object><object><coordinate>pixel</coordinate><type>rectangle</type><description>None</description><possibleresult><name>Small Car</name></possibleresult><points><point>267.000000,228.000000</point><point>244.000000,232.000000</point><point>241.944946,220.183472</point><point>264.944946,216.183472</point><point>267.000000,228.000000</point></points></object><object><coordinate>pixel</coordinate><type>rectangle</type><description>None</description><possibleresult><name>Small Car</name></possibleresult><points><point>480.591736,217.018555</point><point>458.591736,218.018555</point><point>458.000000,205.000000</point><point>480.000000,204.000000</point><point>480.591736,217.018555</point></points></object></root>";


const parser = new DOMParser();
const xml = parser.parseFromString(data, 'application/xml');

for(let objectTag of xml.querySelectorAll('root>object')) {
  console.log(`=== name: ${objectTag.querySelector('name').textContent} ===`)
  for(let x of objectTag.getElementsByTagName('point')) {
   console.log(x.textContent)
 }
}

  • Related