troubleshooting Question

XPath Parsing of Namespace Declarations

Avatar of cgray1223
cgray1223 asked on
JavaJava EE
3 Comments1 Solution262 ViewsLast Modified:

I'm using XPath to pull out just the values of URL128 XML element.  There can be many of these even though I just have one in the below example.  When I include xmlns='' on the SearchResponse element I get an empty NodeList, but when I remove that namespace element it works fine.  Is there a configuration I'm missing?

  String xmlData = "<SearchResponse xmlns=''><searchResultDataXML><SearchResultData><SearchRequestUID Scope='Public' Type='Guid' Value='{cded773c-c4b7-4dd8-aaee-8e5b8b7a2475}'/><StartPosition Scope='Public' Type='Long' Value='1'/><EndPosition Scope='Public' Type='Long' Value='50'/><TotalHits Scope='Public' Type='Long' Value='323636'/></SearchResultData></searchResultDataXML><imagesXML><Images><Image><ImageUID Scope='Public' Type='Guid' Value='{a6f6d3e2-2c3f-4502-9741-eae2e1bb573a}'/><CorbisID Scope='Public' Type='String' Value='42-25763849'/><Title Scope='Public' Type='String' Value='Animals figurines'/><CreditLine Scope='Public' Type='String' Value='© Ocean/Corbis'/><IsRoyaltyFree Scope='Public' Type='Boolean' Value='True'/><AspectRatio Scope='Public' Type='String' Value='0.666667'/><URL128 Scope='Public' Type='String' Value=''/></Image></Images></imagesXML></SearchResponse>";
    		    InputSource source = new InputSource(new StringReader(xmlData));
    		    XPath xPath = XPathFactory.newInstance().newXPath();
    		    NodeList list = null;
    		    try {
    		    	list = (NodeList) xPath.evaluate("//URL128/@Value", source, XPathConstants.NODESET);
    		    } catch (Exception ex) {
    		    for (int i = 0; i < list.getLength(); i++) {
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 3 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros