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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:14:05+00:00 2026-06-17T08:14:05+00:00

java – xpath – pom.xml I am using following pom.xml file as xml dom

  • 0

java – xpath – pom.xml

I am using following pom.xml file as xml dom resource, i want to get value of tag <version> without iterating in my code that’s why i am using xpath but i guess my xpath express is not correct that’s why it is not returning any value. any help appreciated.

java code

    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(true);

    DocumentBuilder builder = domFactory.newDocumentBuilder();
    Document doc = builder.parse("C:\\projects\\pom.xml");
    XPath xpath = XPathFactory.newInstance().newXPath();

    XPathExpression expr = xpath.compile("project/version");
    Object result = expr.evaluate(doc, XPathConstants.NODESET);
    NodeList nodes = (NodeList) result;
    for (int i = 0; i < nodes.getLength(); i++) {
        System.out.println(nodes.item(i).getNodeValue());
    }

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.dc.app</groupId>
  <artifactId>rum</artifactId>
  <packaging>war</packaging>
  <version>12.54-SNAPSHOT</version>
  <name>rum</name>
  <description>rum</description>
</project>
  • 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-17T08:14:06+00:00Added an answer on June 17, 2026 at 8:14 am

    Make a javax.xml.NamespaceContext and feed it to your xpath:

    xpath.setNamespaceContext(ctx);
    

    So:

    DocumentBuilderFactory domFactory = DocumentBuilderFactory
            .newInstance();
    domFactory.setNamespaceAware(true);
    
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    Document doc = builder.parse("blah.pom");
    XPath xpath = XPathFactory.newInstance().newXPath();
    Map<String, String> namespaces = new HashMap<String, String>();
    namespaces.put("pom", "http://maven.apache.org/POM/4.0.0");
    
    xpath.setNamespaceContext(
            new NamespaceContextImpl("http://maven.apache.org/POM/4.0.0", 
                    namespaces));
    
    XPathExpression expr = xpath.compile("/pom:project/pom:version");
    Object result = expr.evaluate(doc, XPathConstants.NODESET);
    NodeList nodes = (NodeList) result;
    for (int i = 0; i < nodes.getLength(); i++) {
        System.out.println(nodes.item(i).getTextContent());
    }
    

    yields:

    12.54-SNAPSHOT
    

    EDIT: an implementation of the NamespaceContext:

    public class NamespaceContextImpl implements NamespaceContext {
       private final Map<String, String> namespaces;
       private final String defaultNamespaceURI;
    
       public NamespaceContextImpl(String defaultNamespaceURI,
                                   Map<String, String> namespaces) {
          this.defaultNamespaceURI = defaultNamespaceURI;
          this.namespaces = namespaces;
       }
    
       public Iterator getPrefixes(String namespaceURI) {
          throw new IllegalStateException("Not Implemented.");
       }
    
       public String getPrefix(String namespaceURI) {
          throw new IllegalStateException("Not Implemented.");
       }
    
       public String getNamespaceURI(String prefix) {
          if (prefix == null) {
             throw new IllegalArgumentException();
          }
          if (prefix.equals(XMLConstants.XMLNS_ATTRIBUTE)) {
             return XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
          }
          if (prefix.equals(XMLConstants.XML_NS_PREFIX)) {
             return XMLConstants.XML_NS_URI;
          }
          if (prefix.equals(XMLConstants.DEFAULT_NS_PREFIX)) {
             return defaultNamespaceURI;
          }
          String result = namespaces.get(prefix);
          if (result == null) {
             result = XMLConstants.NULL_NS_URI;
          }
          return result;
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Java: 1.6 Woodstox: 4.1.4 I just want to skip part of xml file, while
java.lang.NoClassDefFoundError: javax/servlet/jsp/el/ELException from using <c:set var=testVar value=testVal /> All other JSTL tags work. jsp
Java noob question: Consider the following C array and initializer code: struct { int
Java newbie here, I am trying to find the output of the following chunk
java.nio.file.Files.newByteChannel returns SeekableByteChannel . But actual class of the object returned is sun.nio.ch.FileChannelImpl (in
Java - Jsch sudo command. I am using Jsch and my task is to
Java: 1.6 Woodstox: 4.1.4 I'm currently trying to make Woodstox xml parser my friend.
Java PreparedStatement provides a possibility to explicitely set a Null value. This possibility is:
Java Setup on Scientific Linux showing two diffent versions. I am trying to get
Java or .NET answers are fine. I create an Android Map with the following,

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.