Solved

XPath is not working with namespaces

Posted on 2014-11-04
8
117 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 60

Expert Comment

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

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

622 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