Solved

XPath is not working with namespaces

Posted on 2014-11-04
8
108 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Python Assistance 7 73
HSSFWorkbook cannot be resolved error 10 50
Help with a If commands in MS word 2010 when using with a XML drop down box 5 27
XML XSL Message 3 13
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…

911 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

25 Experts available now in Live!

Get 1:1 Help Now