Solved

XPath is not working with namespaces

Posted on 2014-11-04
8
105 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
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 40422831
it always help if you specify which parser classes you are using :-)
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now