Avatar of cgray1223
 asked on

XPath Parsing of Namespace Declarations


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='http://c1.net.corbis.com/' 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='http://c1.net.corbis.com/'><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='http://cachens.corbis.com/CorbisImage/thumb/25/76/38/25763849/42-25763849.jpg'/></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++) {

Open in new window

JavaJava EE

Avatar of undefined
Last Comment

8/22/2022 - Mon

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

I am using Sprig, so I implemented your second example and it works perfectly.  As did you first one.  Thanks for the great explanation and through examples, much appreciated!

Not a problem. I should probably add that it may or may not be obvious, but the use of ns on lines 9 and 11 of the second example is purely arbitrary (other than they have to be the same), if there is something more appropriate (maybe corbis) you can use that.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck