Solved

XPath is not working with namespaces

Posted on 2014-11-04
8
114 Views
Last Modified: 2014-11-14
I want to query an XML using XPath.

I couldn't query properly when there is name space in XML.
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
   <env:Body>
      <ns2:fld1 xmlns:ns3="http://esb.tiaa.org" xmlns:ns2="http://esb.tiaa.org/participant-portfolio-v1/types">
         <ns2:fld2>
            <ns2:fld3>446623.5298</ns2:fld3>
         </ns2:fld2>
      </ns2:fld1>
   </env:Body>
</env:Envelope>

Open in new window


Below is the java code to query
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		factory.setNamespaceAware(true);
		DocumentBuilder builder = factory.newDocumentBuilder();
		Document doc = null;
// "xmlStr" contain above XML
		doc = builder.parse(new InputSource(new StringReader(xmlStr)));
		XPathFactory xpathFactory = XPathFactory.newInstance();
		XPath xpath = xpathFactory.newXPath();

		List<String> list = new ArrayList<String>();
		StringBuffer strRowData = null;
		strRepeatableElement = "/env:Envelope/env:Body/ns2:fld1/ns2:fld2";
		XPathExpression expr = xpath.compile("count(" + strRepeatableElement + ")");
System.out.println((Double) expr.evaluate(doc, XPathConstants.NUMBER)); // Printing 0

Open in new window


Search result is count is printed as 0 in last line. What could be the problem?
0
Comment
Question by:Ravi Kalla
  • 5
  • 2
8 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 40422536
you will need a namespace manager, but first try this to see the only issue is the namespace

strRepeatableElement = "//*[local-name() = 'fld2']";
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 40422546
after you initialised xpath
add
xpath.addNamespace(Namespace.getNamespace("ns2", "http://esb.tiaa.org/participant-portfolio-v1/types");

for each namespace prefix you want to use in the XPath
0
 
LVL 6

Accepted Solution

by:
Ravi Kalla earned 0 total points
ID: 40422732
@Geert Bormans : I couldn't find "Namespace" class.

I found a solution and it is working fine now:
		Map<String, String> prefMap = new HashMap<String, String>();
		prefMap.put("env", "http://schemas.xmlsoap.org/soap/envelope/");
		SimpleNamespaceContext namespaces = new SimpleNamespaceContext(prefMap);
		xpath.setNamespaceContext(namespaces);

Open in new window

0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 60

Expert Comment

by:Geert Bormans
ID: 40422831
it always help if you specify which parser classes you are using :-)
0
 
LVL 35

Expert Comment

by:mccarl
ID: 40422913
it always help if you specify which parser classes you are using :-)
Well, to be fair Geert, it is reasonably obvious from the code posted that the javax.xml classes were being used!
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 40422943
my bad, to me it was not ...
apologies then
0
 
LVL 6

Author Comment

by:Ravi Kalla
ID: 40433668
I've requested that this question be closed as follows:

Accepted answer: 0 points for Ravi Kalla's comment #a40422732

for the following reason:

This is working for me
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 40433669
I like to point out that this is a working solution
ID: 40422536
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question