Handling XML with Javascript

0

I have a question and I already searched a lot and still could not solve.

I have an XML with a tag called <child id='minhatag'/> . I need to have javascript read this tag and get me to return the value of the child tag ID so that the value I can put on a button, because my screen will have several buttons that will be presented according to the values of "Child's ID's ". Note: It will be several childs for example:

<child id='child1'/>
<child id='child2'/>
<child id='child3'/>

And the XML will be in the same folder structure as the code, or it will not be a URL access, at least not at first.

    
asked by anonymous 21.08.2017 / 18:46

2 answers

1

You can use the code below to do this, however you need a parent to arrange the parse:

var xml, parser, xmlDoc;
text = "<children><child id='child1'/>" +
       "<child id='child2'/>" +
       "<child id='child3'/></children>";

parser = new DOMParser();
xmlDoc = parser.parseFromString(text, "text/xml");
xmlDoc.getElementsByTagName("children")[0].childNodes.forEach(function(value) {
    console.log(value.id);
});
    
21.08.2017 / 19:24
0

Type like this:

var childs = document.getElementsByTagName("child");
for (var i = 0; i < childs.length; i++) {
  console.log(childs[i].id);
}
    <child id='child1'/>
    <child id='child2'/>
    <child id='child3'/>
    
21.08.2017 / 19:25