0

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
  • 1
    Can you post some quick and dirty sample code? This could be a problem with the code you are using :) Commented Mar 26, 2010 at 17:16

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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?
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.
I apologize I've been going crazy with this code for a while now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.