admin管理员组

文章数量:1399909

I had been trying to get the tag name and its value in java script. The xml file which I use looks like below:

<root>
    <note>
        <to>Gil</to>
        <from>
            <firstname>john</firstname>
            <lastname>corner</lastname>
        </from>
        <heading>Reminder</heading>
    </note>
    <note>
        <to>Mary</to>
        <from>
            <firstname>Clara</firstname>
            <lastname>Diana</lastname>
        </from>
        <heading>
            How are you
        </heading>
    </note>
</root>

I would like the output to be as below:

TageName : root Attribute: 0 Text: null

TageName : note Attribute: 0 Text: null
TageName : to   Attribute: 0 Text: Gil
TageName : from Attribute: 0 Text: null
TageName : firstname Attribute: 0 Text: john
TageName : lastname Attribute: 0 Text: corner
TageName : heading Attribute: 0 Text: Reminder

TageName : note Attribute: 0 Text: null
TageName : to   Attribute: 0 Text: Mary
TageName : from Attribute: 0 Text: null
TageName : firstname Attribute: 0 Text: Clara
TageName : lastname Attribute: 0 Text: Diana
TageName : heading Attribute: 0 Text: How are you

Is this possible. If so, please help me...

I had been trying to get the tag name and its value in java script. The xml file which I use looks like below:

<root>
    <note>
        <to>Gil</to>
        <from>
            <firstname>john</firstname>
            <lastname>corner</lastname>
        </from>
        <heading>Reminder</heading>
    </note>
    <note>
        <to>Mary</to>
        <from>
            <firstname>Clara</firstname>
            <lastname>Diana</lastname>
        </from>
        <heading>
            How are you
        </heading>
    </note>
</root>

I would like the output to be as below:

TageName : root Attribute: 0 Text: null

TageName : note Attribute: 0 Text: null
TageName : to   Attribute: 0 Text: Gil
TageName : from Attribute: 0 Text: null
TageName : firstname Attribute: 0 Text: john
TageName : lastname Attribute: 0 Text: corner
TageName : heading Attribute: 0 Text: Reminder

TageName : note Attribute: 0 Text: null
TageName : to   Attribute: 0 Text: Mary
TageName : from Attribute: 0 Text: null
TageName : firstname Attribute: 0 Text: Clara
TageName : lastname Attribute: 0 Text: Diana
TageName : heading Attribute: 0 Text: How are you

Is this possible. If so, please help me...

Share Improve this question edited Jul 2, 2012 at 13:33 user372551 asked Jul 2, 2012 at 13:27 CARTICCARTIC 5734 gold badges14 silver badges26 bronze badges 3
  • Is this a homework question? In any case, please show what you've tried so far. – Tomalak Commented Jul 2, 2012 at 13:29
  • 1 Google javascript parse xml – Pekka Commented Jul 2, 2012 at 13:29
  • 1 See these answers: stackoverflow./questions/1972790/… – Kane Commented Jul 2, 2012 at 13:30
Add a ment  | 

2 Answers 2

Reset to default 5
var xml = '<root><note ><to>Gil</to><from><firstname>john</firstname><lastname>corner</lastname></from><heading>Reminder</heading></note><note><to>Mary</to><from><firstname>Clara</firstname><lastname>Diana</lastname></from><heading>How are you</heading></note></root>';

var node = (new DOMParser()).parseFromString(xml, "text/xml").documentElement;

var nodes = node.querySelectorAll("*");

for (var i = 0; i < nodes.length; i++) {
    var text = null;
    if (nodes[i].childNodes.length == 1 && nodes[i].childNodes[0].nodeType == 3) //if nodeType == text node
        text = nodes[i].textContent; //get text of the node
    console.log("TageName : ", nodes[i].tagName, ", Text : ", text);
}​

Here is a jsFiddle that shows the basic use. And here is the basic code using jQuery:

$(document).ready(function()
{
    $(this).find("root").children().each(function()
     {
             alert($(this).get(0).tagName+":"+$(this).text());                                 
       });                                                                                     
 });

Modify it to fit your needs.

本文标签: xml parsingGetting xml tag names in javascriptStack Overflow