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

The Archive Base Latest Questions

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

How can I differentiate between these two pieces of XML with XPath in Java?

  • 0

How can I differentiate between these two pieces of XML with XPath in Java?

<iq type="result">
    <query xmlns="jabber:iq:roster">
    </query>
</iq>

and

<iq type="result">
    <query xmlns="vcard-temp">
    </query>
</iq>

I tried this and it always prints null,

public class Test {

    public static void main(String[] args) throws Exception {
        final String xml = "<iq type=\"result\"><query xmlns=\"jabber:iq:roster\"></query></iq>";

        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document document = builder.parse(createInputSource(xml));

        XPath xpath = XPathFactory.newInstance().newXPath();
        xpath.setNamespaceContext(new NamespaceContext() {
            @Override
            public String getNamespaceURI(String prefix) {
                switch (prefix) {
                    case "roster":
                        return "jabber:iq:roster";
                    case "vcard":
                        return "vcard-temp";
                }

                return XMLConstants.NULL_NS_URI;
            }

            @Override
            public String getPrefix(String namespaceURI) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public Iterator getPrefixes(String namespaceURI) {
                throw new UnsupportedOperationException("Not supported yet.");
            }
        });

        Node match = (Node) xpath.evaluate("/iq/roster:query", document, XPathConstants.NODE);
        System.out.println(match);
    }

    private static InputSource createInputSource(String xml) {
        return new InputSource(new StringReader(xml));
    }
}
  • 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:31:17+00:00Added an answer on June 17, 2026 at 8:31 am

    Whatever API you’re using to query your XML documents with XPath most likely has a way to register namespace prefixes for namespaces, such as with a NamespaceContext.
    So you would assign a different prefix to each namespace and retrieve the corresponding nodes with something like:

    /iq/jRoster:query
    /iq/vcard:query
    

    The other option is to skip the NamespaceContext and use namespace-uri():

    /iq/*[local-name() = "query" and namespace-uri() = "jabber:iq:roster"]
    /iq/*[local-name() = "query" and namespace-uri() = "vcard-temp"]
    

    It looks like you also need to use setNamespaceAware(true) on the when creating the document builder:

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I differentiate between two local branches in git ? How can I
I'm trying to use type hinting to differentiate between two single-arg methods. For example,
In TCP, you can differentiate between servers and clients, because servers are those who
How can we differentiate between var foo; and var foo=undefined; ? typeof foo will
Can any one tell, how to get the result of LINQ query contains group
How to differentiate between value-type, nullable value-type, enum, nullable-enum, reference-types through reflection? enum MyEnum
I was wondering how to differentiate between two different instances of a software that
I'm trying to differentiate between two (or more) UITouch objects on the iPhone. Specifically,
Can any one differentiate Fragments and shared preferences on android. Because, Right now i
I want to differentiate between separate types of Not Found errors. For Example given

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.