Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing a XML HttpResponse

I'm trying to parse the XML HttpResponse i get from a HttpPost to a server (last.fm), for a last.fm android app. If i simply parse it to string i can see it being a normal xml string, with all the desired information. But i just cant parse the single NameValuePairs. This is my HttpResponse object:

HttpResponse response = client.execute(post);
HttpEntity r_entity = response.getEntity();

I tried two different things and non of them worked. First i tried to retrieve the NameValuePairs:

List<NameValuePair> answer = URLEncodedUtils.parse(r_entity);
String name = "empty";
String playcount = "empty";
for (int i = 0; i < answer.size(); i++){
   if (answer.get(i).getName().equals("name")){
      name = answer.get(i).getValue();
   } else if (answer.get(i).getName().equals("playcount")){
      playcount = answer.get(i).getValue();
   }
}

After this code, name and playcount remain "empty". So i tried to use a XML Parser:

DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document answer = db.parse(new DataInputStream(r_entity.getContent()));
NodeList nl = answer.getElementsByTagName("playcount");
String playcount = "empty";
for (int i = 0; i < nl.getLength(); i++) {
   Node n = nl.item(i);
   Node fc = n.getFirstChild();
   playcount Url = fc.getNodeValue();
}

This seems to fail much earlier since it doesn't even get to setting the playcount variable. But like i said if i perform this:

EntityUtils.toString(r_entity);

I will get a perfect xml string. So it should no problem to pars it since the HttpResponse contains the correct information. What am i doing wrong?

like image 746
gaussd Avatar asked Feb 11 '11 22:02

gaussd


People also ask

How XML is parsed?

The parser reads an XML document from the beginning to the end. When it encounters a node in the document, it generates an event that triggers the corresponding event handler for that node. The handler thus applies the application logic to process the node specifically.

Can I use an XML parser to parse HTML?

You can try parsing an HTML file using a XML parser, but it's likely to fail. The reason is that HTML documents can have the following HTML features that XML parsers don't understand. XML parsers will fail to parse any HTML document that uses any of those features.

Can browser parse XML?

All major browsers have a built-in XML parser to access and manipulate XML.

What is XML Pull Parser?

In android, the XMLPullParser interface provides the functionality to parse the XML files in android applications. The XMLPullParser is a simple and efficient parser method to parse the XML data when compared to other parser methods such as DOM Parser and SAX Parser.


1 Answers

I solved it. The DOM XML parser needed a little more adjustment:

        HttpResponse response = client.execute(post);
        HttpEntity r_entity = response.getEntity();
        String xmlString = EntityUtils.toString(r_entity);
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = factory.newDocumentBuilder();
        InputSource inStream = new InputSource();
        inStream.setCharacterStream(new StringReader(xmlString));
        Document doc = db.parse(inStream);  

        String playcount = "empty";
        NodeList nl = doc.getElementsByTagName("playcount");
        for(int i = 0; i < nl.getLength(); i++) {
            if (nl.item(i).getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
                 org.w3c.dom.Element nameElement = (org.w3c.dom.Element) nl.item(i);
                 playcount = nameElement.getFirstChild().getNodeValue().trim();
             }
        }
like image 69
gaussd Avatar answered Nov 08 '22 05:11

gaussd