Keshan
Keshan

Reputation: 14677

How to parse a xml type HTTPResponse in Android

I have an android application where I use POST method to get a response. here is my code:

..........................................
HttpResponse httpResponse = httpclient.execute(httppost);
HttpEntity resEntity =  httpResponse.getEntity();

this works fine and i get a response in xml format but i want to parse that xml file and get the node values. i tried this :

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource());
doc.getDocumentElement().normalize();

but I don't know what should give to new InputSource() because I have to use a XML type HTTPResponse not a url.

Thanks !!!

Upvotes: 3

Views: 4087

Answers (5)

Parag Chauhan
Parag Chauhan

Reputation: 35956

            getsyncFlag(String feedData) {

            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();
            xr.setContentHandler(this);
            InputSource is=new InputSource();
            is.setCharacterStream(new StringReader((feedData)));
            xr.parse(is);
}

Upvotes: 0

Aswan
Aswan

Reputation: 5135

if your response in xml string format follow

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is=new InputSource();
is.setCharacterStream(new StringReader(xmlString(ur response)))
Document doc = db.parse(is);

Upvotes: 0

pankajagarwal
pankajagarwal

Reputation: 13582

try

InputStream is = resEntity .getContent()
Document doc = db.parse(is);

Upvotes: 1

Keshan
Keshan

Reputation: 14677

ok thanks everyone for the replies. i just found out a way to overcome my problem. http://www.rgagnon.com/javadetails/java-0573.html

Upvotes: 1

the100rabh
the100rabh

Reputation: 4147

You can use any XML parser like SAX to parse ur XML Data

Upvotes: 0

Related Questions