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
2 Answers
Reset to default 5var 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
版权声明:本文标题:xml parsing - Getting xml tag names in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744233758a2596461.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论