Albinoswordfish
Albinoswordfish

Reputation: 1957

XML parsing problem

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>";

Upvotes: 0

Views: 156

Answers (1)

jarnbjo
jarnbjo

Reputation: 34313

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.

Upvotes: 1

Related Questions