-1

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...

3
  • Is this a homework question? In any case, please show what you've tried so far. Commented Jul 2, 2012 at 13:29
  • 1
    Google javascript parse xml Commented Jul 2, 2012 at 13:29
  • 1
    See these answers: stackoverflow.com/questions/1972790/… Commented Jul 2, 2012 at 13:30

2 Answers 2

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);
}​
Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

Hi..Thanks for the response. I had just started to learn xml and java script. Once I'm done with this i will start with Jquery. Anyways..thanks a lot...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.