Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

XPath is not working with namespaces

Posted on 2014-11-04
8
Medium Priority
?
122 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

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!

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

916 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