Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7040641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:56:33+00:00 2026-05-28T01:56:33+00:00

I’m having a hard time getting XML values from an XML file using XPath.

  • 0

I’m having a hard time getting XML values from an XML file using XPath. The XML file has the following schema:

<devicesInfo>
    <deviceInfo>
    <deviceId>device1</deviceId>
    <macAddress>00:21:85:C9:19:A4</macAddress>
    <deviceType>deviceType1</deviceType>
    <deviceClass>/Server/SSH/Linux/</deviceClass>
    <location>location1</location>
    <productionState>Decommissioned</productionState>
</deviceInfo>
<deviceInfo>
    <deviceId>device2</deviceId>
    <macAddress>00:21:85:C9:19:F5</macAddress>
    <deviceType>deviceType1</deviceType>
    <deviceClass>/Server/SSH/Linux/</deviceClass>
    <location>location1</location>
    <productionState>Decommissioned</productionState>
</deviceInfo>
</devicesInfo>

What I tried to do is first of all, creating an instance of a custom class that I have made, which contains the following method:

  public NodeList getNodesList(String xmlQuery) throws XPathExpressionException {
        NodeList nodes = null;
        try {
            nodes = (NodeList) xPath.evaluate(xmlQuery, doc, XPathConstants.NODESET);
        } catch (XPathExpressionException ex) {
            throw ex;
        }
        return nodes;

    }

This method is then called with the query:

 NodeList nodes = xmlHandler.getNodesList("/devicesInfo/deviceInfo");

So far so good, for this is returning a NodeList with length 2, which is basically all the deviceInfo elements in the file.
What I wish to do next is iterating over each deviceInfo in order to get the value of its children (deviceId,macAddress,deviceType,deviceClass,location,productionState).

I tried to do the following:
NodeList list = nodes.item(0).getChildNodes();

Then

 System.out.println("First element is "+list.item(0).getTextContent());

This has given me an empty string. Though when I try to do this:

System.out.println("First element is "+list.item(1).getTextContent());

which is basically displaying item with index “1” rather than item with index “0”, this ends up printing “device1” which is the value of the first element.

So in order to get the values of all the elements, I’d use the indices 1,3,5,7,9,11. I know I’m doing something wrong. Could anyone help me please?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:56:33+00:00Added an answer on May 28, 2026 at 1:56 am

    The children of the <deviceInfo> element include a number of whitespace-only text nodes. You need to filter those out so that you’re only iterating over the children that are elements, or parse the XML in a mode where white-space only text nodes inside <deviceInfo> elements are ignored.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are using XSLT to translate a RIXML file to XML. Our RIXML contains
In my XML file chapters tag has more chapter tag.i need to display chapters
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm parsing an XML file, the creators of it stuck in a bunch social
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.