1

I have a 1 xml file with a different parent node. The node created dynamically.

Here's the sample nodes;

1st content:

<error code ='0'><id>1234</id><message>ERROR PROCESS</message></error>

2nd content

<name>
<lastname>Doe</lastname>
<firstname>John</firstname>
</name>

just to identify the parent node. please help.. ;(

3 Answers 3

5

Since JQuery 1.9 you can use the :root selector instead of getting ALL the nodes, so:

$(xml).find(":root")[0];
Sign up to request clarification or add additional context in comments.

Comments

4
    var xml = "<name><lastname>Doe</lastname><firstname>John</firstname></name>",
    xmlDoc = $.parseXML( xml ),
    $xml = $( xmlDoc ),
        $title = $xml;

    console.log($xml.find("*").eq(0));

DEMO

if you want to get the root node name then you can use the nodeName property

console.log($xml.find("*").eq(0)[0].nodeName);

Comments

0

From the parseXml and contents() documentation:

var xmlDoc = $.parseXML(xml);
var $xml = $(xmlDoc);
alert($xml.contents()[0].nodeName);

When filling xml with your example XML documents, it should show error or name. I know it is not the best jQuery code, but it should give you a nice example to start with.

Use this demo to add other XML document to try it.

Comments

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.