Volley library for Android parse xml response?
Asked Answered
W

4

11

I am using volley library and getting response in XML.I want to know how we can parse response using /volley library.Thank you.

Weltanschauung answered 31/7, 2013 at 6:12 Comment(0)
E
10
    StringRequest req = new StringRequest(Request.Method.GET, url, 
    new Response.Listener<String>() 
    {
        @Override
        public void onResponse(String response) {
            processData(response);
        }
    }, 
    new Response.ErrorListener() 
    {
         @Override
         public void onErrorResponse(VolleyError error) {                                
         // handle error response
       }
    }
);

in processdata method parse the response.Use simple sax parser or dom parser to parse the response string.

Evangelista answered 10/11, 2014 at 9:29 Comment(3)
@IftikarUrrhmanKhan I never noticed it thanks i have updated the answer for the same.Evangelista
How I will request POST method with Username and Password by the format of xml.Linneman
just a detail, you have typo on processData(reponse);Everybody
S
13

Looking at Volley's Source:

https://android.googlesource.com/platform/frameworks/volley/+/android-4.3_r0.9/src/com/android/volley/toolbox/

If I am not mistaken, it comes with JsonObjectRequest and StringRequest, but no XMLRequest.

So, you could use StringRequest to get the response as String, and then use any XML marshalling/serializing tool (ex: Simple) to convert it to an Object.

Check out Simple - XML Marshalling tool: http://simple.sourceforge.net/download.php

If you use StringRequest, something like following should do it:

StringRequest request = new StringRequest(Request.Method.GET, url, 
    new Response.Listener<String>() 
    {
        @Override
        public void onResponse(String response) {
          // convert the String response to XML
          // if you use Simple, something like following should do it
            Serializer serializer = new Persister();
            serializer.read(ObjectType.class, response);  
        }
    }, 
    new Response.ErrorListener() 
    {
         @Override
         public void onErrorResponse(VolleyError error) {                                
         // handle error response
       }
    }
);
queue.add(request);

Alternatively, you could create your own XMLRequest class by extending from Request, and use XML Serialization tool (like Simple) to return the Object.

Hope it helps,

Shrunken answered 2/8, 2013 at 7:1 Comment(5)
I recently had to create a SimpleXMLRequest class to parse XML Response to Objects using Volley. Here's the code snippet should anyone need: gist.github.com/itsalif/6149365Shrunken
Thank you Alif for your answer.Weltanschauung
So,for parsing data we have to use our own parsing logic?Weltanschauung
You are welcome, If you use SimpleXMLRequest, For parsing you have to use XML Serialization tool - simple.sourceforge.net. You only need to create a POJO class corresponding to XML, and then Simple inflate the XML Response to Java Object.Shrunken
I got error in Serializer serializer = new Persister();Mcquade
E
10
    StringRequest req = new StringRequest(Request.Method.GET, url, 
    new Response.Listener<String>() 
    {
        @Override
        public void onResponse(String response) {
            processData(response);
        }
    }, 
    new Response.ErrorListener() 
    {
         @Override
         public void onErrorResponse(VolleyError error) {                                
         // handle error response
       }
    }
);

in processdata method parse the response.Use simple sax parser or dom parser to parse the response string.

Evangelista answered 10/11, 2014 at 9:29 Comment(3)
@IftikarUrrhmanKhan I never noticed it thanks i have updated the answer for the same.Evangelista
How I will request POST method with Username and Password by the format of xml.Linneman
just a detail, you have typo on processData(reponse);Everybody
C
2

Here is my solution. As Sir Alif said above, volley can provide you with raw string of XML data, received from the server. So, all you need to do is to parse this String accordinly to your app logic. So, for parsing the String I used SAX parser. I'm not going to describe here how to use SAX parser cause you can find tons of tutorials by yourself, I'll just describe the key point. The piece of code below shows the moment when you create instances of SAXParserFactory, SAXParser, XMLReader etc., also here you create an instance of InputSource, and this is the key point. Commonly, you open a connection, using certain url address, and then SAX parser parses received data for you. But, as far as we are trying to use Volley library, we will give InputSource not the InputStream, but a StringReader (full information about input source and its public constructors you can find here). It will look like this:

public void makeList(String s){   
   SAXParserFactory factory = SAXParserFactory.newInstance();
   SAXParser sp = factory.newSAXParser();
   XMLReader xmlReader = sp.getXMLReader();
   SaxHandler handler = new SaxHandler();
   xmlReader.setContentHandler(handler);                               
   InputSource is = new InputSource(new StringReader(s));
   is.setEncoding("UTF-8");
   xmlReader.parse(is); 
}

So, in the Activity, where you get the StringRequest from Volley, in onRespone() method you can pass that response to the makeList(String s) method (or to whatever you called the method with the same functionality), which will parse you that response. Hope this helps! If you have some questions please ask in comments.

Countercheck answered 10/11, 2014 at 3:7 Comment(0)
L
0

First you have to get response string by:

  StringRequest req = new StringRequest(Request.Method.GET, url, 
new Response.Listener<String>() 
{
    @Override
    public void onResponse(String response) {
         InputStream is = convertStringToDocument(reponse);
         // now you can parse xml 
    }
}, 
new Response.ErrorListener() 
{
     @Override
     public void onErrorResponse(VolleyError error) {                                
     // handle error response
   }
}

);

after getting respone via volley you have to convert String response to input stream by this method:

 private static InputStream convertStringToDocument(String xmlStr) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;
    try
    {
        builder = factory.newDocumentBuilder();
        Document doc = builder.parse( new InputSource( new StringReader( xmlStr ) ) );
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        Source xmlSource = new DOMSource(doc);
        Result outputTarget = new StreamResult(outputStream);
        TransformerFactory.newInstance().newTransformer().transform(xmlSource, outputTarget);
        InputStream is = new ByteArrayInputStream(outputStream.toByteArray());
        return is;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Lempira answered 8/12, 2016 at 8:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.