Jump to content

Parsing XML with Java XPATH - Need help


fenstermakera@yahoo.com

Recommended Posts

I'm having trouble parsing XML return value with XPath - can anyone help me? Here is the result I'm trying to parse: ****START XML RESPONSE***** <?xml version="1.0" encoding="UTF-8"?> <aws:UrlInfoResponse xmlns:aws=" http://alexa.amazonaws.com/doc/2005-10-05/"> <aws:Response xmlns:aws=" http://awis.amazonaws.com/doc/2005-07-11"> <aws:OperationRequest> <aws:RequestId>da127cf1-d0f5-4516-8e04-66a2c0b5312d</aws:RequestId> </aws:OperationRequest> <aws:UrlInfoResult> <aws:Alexa> <aws:TrafficData> <aws:DataUrl type="canonical">loquisoft.com/</aws:DataUrl> <aws:Rank>3388928</aws:Rank> </aws:TrafficData> </aws:Alexa> </aws:UrlInfoResult> <aws:ResponseStatus xmlns:aws=" http://alexa.amazonaws.com/doc/2005-10-05/"> <aws:StatusCode>Success</aws:StatusCode> </aws:ResponseStatus> </aws:Response> </aws:UrlInfoResponse> ***END XML RESPONSE ****** I'm trying to parse the rank # (in this case "3388928") with the following code: *** START CODE SNIPPET **** URL url = new URL(urlBuffer.toString()); URLConnection conn = url.openConnection(); InputStream stream = conn.getInputStream(); SAXBuilder builder = new SAXBuilder(); Document doc = builder.build(stream); XPath xp = XPath.newInstance("//aws:Rank"); xp.addNamespace("aws", " http://webservices.amazon.com/AWSAlexa/2005-07-11"); Element rank = (Element) xp.selectSingleNode(doc); if (rank == null) { System.out.println("Could not get rank."); outputter.output(doc, System.out); return ""; } return rank.getText(); *** END CODE SNIPPET **** The rank value is returned as null. I think the problem is I'm not clear on the syntax on the newInstance and addNamespace parameters. I had this code working at one time, but then the XML format changed and now it's broken.Can anyone help? Thanks! -Adam

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...