3

the xml file is like this:

<products>
  <product>
   <id>1</id>
   <name>pen</name>
  </product>
  <product>
   <id>2</id>
   <name>pencil</name>
  </product>
</products>
<employees>
  <employee>
   <name>Jack</name>
  <employee>
  <employee>
   <name>Mark</name>
  <employee>
</employees>

Can I only get the <name> nodes which are the childen of <product>? Is there well adapted xpath api in javascript dom?

3 Answers 3

4

In modern browsers you could use querySelector to traverse the xml-tree. Let's say your xml resides within div#xmlsample, then this code will give you a nodeList for //product/name in prodNames:

var xmlDoc = (new DOMParser())
              .parseFromString(document.querySelector('#xmlsample').innerHTML,
              "application/xml"),
    prodNames = xmlDoc.querySelectorAll('product name');

See this jsfiddle

Sign up to request clarification or add additional context in comments.

Comments

3

jQuery has an api for parsing and traversing xml.

var xmlObj = $.parseXML(xmlObj);

var nameNodes = xmlObj.find('product > name');

From the doc..

jQuery.parseXML uses the native parsing function of the browser to create a valid XML Document. This document can then be passed to jQuery to create a typical jQuery object that can be traversed and manipulated.

2 Comments

That's cool! But is there any javascript native implement about this
Well you asked for an adapted API. For some native implementation, see this article.
1

Can I only get the <name> nodes which are the childen of <product>?

The provided XML isn't a well-formed XML document (a single top element is required).

Also, two <employee> start tags don't have corresponding closing </employee> end-tags.

If the correct XML document is this:

<company>
    <products>
        <product>
            <id>1</id>
            <name>pen</name>
        </product>
        <product>
            <id>2</id>
            <name>pencil</name>
        </product>
    </products>
    <employees>
        <employee>
            <name>Jack</name>
        </employee>
        <employee>
            <name>Mark</name>
        </employee>
    </employees>
</company>

and an XPath expression is really required, then one XPath expression that selects the wanted elements is:

/*/products/product/name

This selects any name element that is a child of any product element that is a child of any products element that is a child of the top element of the XML document.

2 Comments

+1 for really good formatted answer, I'm using nokogiri in ruby for xpath purpose, is there any native xpath support for javascript, and well adapted ?
@yozloy, If you search for "Javascript XPath support" you'll find several good links, like this one: nczonline.net/blog/2009/03/17/xpath-in-javascript-part-1

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.