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 9114161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:08:16+00:00 2026-06-17T04:08:16+00:00

This feels like such a noob question. I’m looking at a pile of Java

  • 0

This feels like such a noob question.

I’m looking at a pile of Java code that manipulates an XML DOM. (The classes are the stock org.w3c.dom.Document and javax.xml.xpath.XPath and such that ship with JDK 7.) It has a ton of places that look like this:

String expr = "/fixed/path/through/the/hierarchy";

// actual code reuses factory instances, etc
XPath xpath = XPathFactory.newInstance().newXPath();
Node topNode = someDocumentInstance.getFirstChild();
Node node = (Node) xpath.evaluate (expr, topNode, XPathConstants.NODE);

NodeList children = node.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
    Node child = children.item(i);
    if (child.getNodeName().equalsIgnoreCase("somePrefix:someTag")) {
        // "return child;" or otherwise break out of the loop
    }
}

And it all works. But that loop seems a tedious effort; if we’re already using XPath to get a node, why then iterate over that node’s children looking for a known tag?

So I tried to rewrite a section to fetch the child node directly. But querying using

String expr = "/fixed/path/through/the/hierarchy/somePrefix:someTag";

never matches anything. I’ve tried variations like requesting XPathConstants.NODESET or .STRING, but still no results. (There should only ever be one of these nodes anyhow.)

I feel like I’m missing something supremely obvious here, but I can’t figure out why the full query fails, when the query-for-parent plus a manual loop through the children works. Is XPath testing some quality of a node beyond getNodeName() when I use a query like that?

The only theory I’ve come up with is that it has something to do with XML namespaces, which aren’t used in this project. (There’s actually a call to .setNamespaceAware(false) on the DocumentBuilderFactory instance with a comment saying “leave this off or everything everywhere breaks”.)

  • 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-06-17T04:08:17+00:00Added an answer on June 17, 2026 at 4:08 am

    If you’re parsing without namespaces, then you should leave somePrefix out of your expression:

    String expr = "/fixed/path/through/the/hierarchy/someTag";
    

    The reason for this is that XPath performs matches on namespace and local name, not qualified name (which is what getNodeName() returns). If you put a prefix in your XPath expression, the XPath interpreter will use that to retrieve the namespace from its namespace mapping. Since you haven’t given it any mappings, that will fail.

    Also, you probably want to use NODESET if you’re going to iterate over the child nodes.

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

Sidebar

Related Questions

This feels like the kind of code that only fails in-situ, but I will
This feels like such a stupid question, but how can I find a string
I feel like such a noob asking this but, for some reason a horizontal
This feels like a super newb question, so apologizes in advance. How does one
This feels like a really basic question, but I can't figure it out anyway..
OK, this feels like an idiot question, but I'm stuck - I don't know
This feels like a really simple question, but I can't find the answer anywhere.
This feels like a dumb question, but I see the following in the Oracle
I know such questions are in ton and it feels like a dupe question.
This feels like a stupid question, but is there a simple control for WPF

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.