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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:55:22+00:00 2026-06-14T05:55:22+00:00

I want to read XML data using XPath in Java. I have the next

  • 0

I want to read XML data using XPath in Java.

I have the next XML file named MyXML.xml:

<?xml version="1.0" encoding="iso-8859-1" ?>
<REPOSITORY xmlns:LIBRARY="http://www.openarchives.org/LIBRARY/2.0/"
            xmlns:xsi="http://www.w3.prg/2001/XMLSchema-instance"
            xsi:schemaLocation="http://www.openarchives.org/LIBRARY/2.0/ http://www.openarchives.org/LIBRARY/2.0/LIBRARY-PHM.xsd">
    <repository>Test</repository>
    <records>
        <record>
            <ejemplar>
                <library_book:book
                        xmlns:library_book="http://www.w3c.es/LIBRARY/book/"
                        xmlns:book="http://www.w3c.es/LIBRARY/book/"
                        xmlns:bookAssets="http://www.w3c.es/LIBRARY/book/"
                        xmlns:bookAsset="http://www.w3c.es/LIBRARY/book/"
                        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                        xsi:schemaLocation="http://www.w3c.es/LIBRARY/book/ http://www.w3c.es/LIBRARY/replacement/book.xsd">
                    <book:bookAssets count="1">
                        <book:bookAsset nasset="1">
                            <book:bookAsset.id>value1</book:bookAsset.id>
                            <book:bookAsset.event>
                                <book:bookAsset.event.id>value2</book:bookAsset.event.id>
                            </book:bookAsset.event>
                        </book:bookAsset>
                    </book:bookAssets>
                </library_book:book>
            </ejemplar>
        </record>
    </records>
</REPOSITORY>

I want access to value1 and value2 values. For this, I try this:

// Standard of reading a XML file
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder;
Document doc = null;
XPathExpression expr = null;
builder = factory.newDocumentBuilder();
doc = builder.parse("MyXML.xml");

// Create a XPathFactory
XPathFactory xFactory = XPathFactory.newInstance();

// Create a XPath object
XPath xpath = xFactory.newXPath();

expr = xpath.compile("//REPOSITORY/records/record/ejemplar/library_book:book//book:bookAsset.event.id/text()");

Object result = expr.evaluate(doc, XPathConstants.STRING);

System.out.println("RESULT=" + (String)result);

But I don’t get any results. Only prints RESULT=.

¿How to access to value1 and value2 values?. ¿What is the XPath filter to apply?.

Thanks in advanced.

I’m using JDK6.

  • 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-14T05:55:23+00:00Added an answer on June 14, 2026 at 5:55 am

    You are having problems with namespaces, what you can do is

    1. take them into account
    2. ignore them using the XPath local-name() function

    Solution 1 implies implementing a NamespaceContext that maps namespaces names and URIs and set it on the XPath object before querying.

    Solution 2 is easy, you just need to change your XPath (but depending on your XML you may fine-tune your XPath to be sure to select the correct element):

    XPath xpath = xFactory.newXPath();
    expr = xpath.compile("//*[local-name()='bookAsset.event.id']/text()");
    Object result = expr.evaluate(doc, XPathConstants.STRING);
    System.out.println("RESULT=" + result);
    

    Runnable example on ideone.

    You can take a look at the following blog article to better understand the uses of namespaces and XPath in Java (even if old)

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

Sidebar

Related Questions

I want to read XML data using XPath in Java, so for the information
I want to read this xml file using Id on <page> tag. <?xml version=1.0
Hi I can't understand this error I want read this XML file: <?xml version=1.0
I have an xml file in which i want to read the child nodes
I want to read this XML with using a Ext.data.Model into a ExtJS Store:
i am using xml parser to read data from xml file. In this file
I want to read xml file in my lift app: val data = XML.load(new
I am reading xml gps data using xmlreader.read(). I want to output all coordinate
I'm using xCode 4.3.2. In this project, i have read the xml data and
I want to read an xml file placed in assets/ directory. I've tried this

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.