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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:48:58+00:00 2026-06-15T15:48:58+00:00

Am new in using Xpath parsing in Java for Xmls. But I learnt it

  • 0

Am new in using Xpath parsing in Java for Xmls. But I learnt it and it worked pretty well until this below issue am not sure how to go traverse to next node in this . Please find the below code and Let me know what needs to be corrected .

package test;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.TransformerException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

public class CallTestcall {
    public static void main(String[] args) throws Exception {

        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(true);
        DocumentBuilder builder = domFactory.newDocumentBuilder();

        String responsePath1 = "C:/Verizon/webserviceTestTool/generatedResponse/example.xml";
        Document doc1 = builder.parse(responsePath1);

        String responsePath0 = "C:/Verizon/webserviceTestTool/generatedResponse/response.xml";
        Document doc0 = builder.parse(responsePath0);

        example0(doc0);
        example1(doc1);
    }

    private static void example0(Document example)
            throws XPathExpressionException, TransformerException {
        System.out.println("\n*** First example - namespacelookup hardcoded ***");

        XPath xPath = XPathFactory.newInstance().newXPath();
        xPath.setNamespaceContext(new HardcodedNamespaceResolver());


        String result = xPath.evaluate("s:Envelope/s:Body/ns1:UpdateSessionResponse",
                example);

        // I tried all the Values to traverse further to UpdateSessionResult but am not able to I used the following xpath expressions

        result = xPath.evaluate("s:Envelope/s:Body/ns1:UpdateSessionResponse/a:UpdateSessionResult",
                example);

        result = xPath.evaluate("s:Envelope/s:Body/ns1:UpdateSessionResponse/i:UpdateSessionResult",
                example);

        System.out.println("example0 : "+result);
    }

    private static void example1(Document example)
            throws XPathExpressionException, TransformerException {
        System.out.println("\n*** First example - namespacelookup hardcoded ***");

        XPath xPath = XPathFactory.newInstance().newXPath();
        xPath.setNamespaceContext(new HardcodedNamespaceResolver());

        String result = xPath.evaluate("books:booklist/technical:book/:author",
                example);
        System.out.println("example1 : "+result);
    }

}

Please find the class that implements nameSpaceContext where I have added the prefixes

package test;

import java.util.Iterator;
import javax.xml.XMLConstants;
import javax.xml.namespace.NamespaceContext;

public class HardcodedNamespaceResolver implements NamespaceContext {



/**
     * This method returns the uri for all prefixes needed. Wherever possible it
     * uses XMLConstants.
     * 
     * @param prefix
     * @return uri
     */
    public String getNamespaceURI(String prefix) {
        if (prefix == null) {
            throw new IllegalArgumentException("No prefix provided!");
        } else if (prefix.equals(XMLConstants.DEFAULT_NS_PREFIX)) {
            return "http://univNaSpResolver/book";
        } else if (prefix.equals("books")) {
            return "http://univNaSpResolver/booklist";
        } else if (prefix.equals("fiction")) {
            return "http://univNaSpResolver/fictionbook";
        } else if (prefix.equals("technical")) {
            return "http://univNaSpResolver/sciencebook";
        } else if (prefix.equals("s")) {
            return "http://schemas.xmlsoap.org/soap/envelope/";
        } else if (prefix.equals("a")) {
            return "http://channelsales.corp.cox.com/vzw/v1/data/";
        } else if (prefix.equals("i")) {
            return "http://www.w3.org/2001/XMLSchema-instance";
        } else if (prefix.equals("ns1")) {
            return "http://channelsales.corp.cox.com/vzw/v1/";
        } 


        else {
            return XMLConstants.NULL_NS_URI;
        }
    }

    public String getPrefix(String namespaceURI) {
        // Not needed in this context.
        return null;
    }

    public Iterator getPrefixes(String namespaceURI) {
        // Not needed in this context.
        return null;
    }

}

Please find my Xml ::::

String XmlString  = "<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><UpdateSessionResponse xmlns="http://channelsales.corp.cox.com/vzw/v1/"><UpdateSessionResult xmlns:a="http://channelsales.corp.cox.com/vzw/v1/data/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:ResponseHeader>
<a:SuccessFlag>true</a:SuccessFlag>
<a:ErrorCode i:nil="true"/>
<a:ErrorMessage i:nil="true"/>
<a:Timestamp>2012-12-05T15:28:35.5363903-05:00</a:Timestamp>
</a:ResponseHeader>
<a:SessionId>cd3ce09e-eb33-48e8-b628-ecd406698aee</a:SessionId>
<a:CacheKey i:nil="true"/>
  • 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-15T15:49:00+00:00Added an answer on June 15, 2026 at 3:49 pm

    Try the following. It works for me.

      result = xPath.evaluate("/s:Envelope/s:Body/ns1:UpdateSessionResponse/ns1:UpdateSessionResult",
                      example);
    

    Since you are searching from the root of the document, precede the xpath expression with a forward slash (/)

    Also, in the XML fragment below, the string xmlns="http... means you are setting that to be the default namespace. In your namespace resolver you are giving this the prefix ns1. So even though UpdateSessionResult is defining two namespace prefixes a and i, it does not use those prefixes itself (for example <a:UpdateSessionResult...) therefore it belongs to the default namespace (named ‘ns1’)

    <UpdateSessionResponse xmlns="http://channelsales.corp.cox.com/vzw/v1/">
    <UpdateSessionResult xmlns:a="http://channelsales.corp.cox.com/vzw/v1/data/"  xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    

    That’s why you need to use ns1:UpdateSessionResult instead of either a:UpdateSessionResult or i:UpdateSessionResult

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

Sidebar

Related Questions

I'm new to jQuery. This would be no problem for me using XPath expressions,
I need help with this. I am new to using XPath in javascript and
Not new to Java; but relatively new to XML-parsing. I know a tiny bit
I am new to using XPath and this may be a basic question. Kindly
I'm new to using XPath so I've been fooling around with an XPath Evaluator
I'm new using Yii framework. I show a list a checkbox. But it's not
I am using Xpath in Ruby with following statement. print XPath.first(Document.new(html),//tr[@id='ctl00_c1_rr_ci_trAdd']//td[2]) The Query return
I have an XML file like below , using xpath and xnavigator how can
Ok, so I am still relatively new to Java and I'm making pretty good
Please go easy on me , this is my first time using XPath 2.0

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.