tags:

views:

79

answers:

1

I'm having this strange XML parsing problem.

I have this XML string I'm trying to parse

<?xml version="1.0"?>
<response status="success">
<lot>32342</lot>
</response>

I'm using XPath with Java in order to do this. I'm using the Xpath expression "/response/@status" to find the text "success". However whenever I evaluate this expression I get an empty string.

However I am able to successfully parse this string using "/response/@type"

<?xml version="1.0"?>
<response type="success">
<lot>32342</lot>
</response>

So why would simply changing the name of the attribute change the return string to nothing?

is = new InputSource(new StringReader(testWOcreateStrGood)); 
xPathexpressionSuccess = xPath.compile("/response/@status");
responseStr = xPathexpressionSuccess.evaluate(is);

reponseStr is the string I posted earlier with the "status" attribute

Also I declared testWOcreateStrGood as below

    private String testWOcreateStrGood = "<?xml version=\"1.0\"?>\n" +
                            "<response status=\"success\">\n" +
                            "<lot>32342</lot>\n" +
                            "</response>";
+1  A: 

So why would simply changing the name of the attribute change the return string to nothing?

It shouldn't. You must be doing something else wrong, e.g. accessing the wrong XML document or not actually using the XPath expression you believe to be using.


To your code example:

Check the API documentation for InputSource. You cannot pass an XML document as a string directly to the constructor.

jarnbjo
As an addition, a quick clean/recompile would be a good idea, and check the paths - maybe dump the xml to the System.out or a log?
aperkins
Sorry I didn't copy my code correctly, I've updated it
Albinoswordfish
And in your code now, you're using XPath to access the "status" attribute of an XML document with a "type" attribute. That is of course not going to work. It's not very easy to help you if you're not even able to ask a plain question without creating such a mess.
jarnbjo
I apologize I've been going crazy with this code for a while now
Albinoswordfish