1

I just wanted to know how to parse the following response of the server. I just wanted to get out the json from the xml. Because I know How to parse the json response So , some one please guide me how can I get this.

I have watched this link for xml parsing but I do not know how to specifically parse this (the underlying given response). the Response is as follows

    <string xmlns="http://tempuri.org/">
[{"OSID":"2","PhoneVersion":"IPHONE5","PhoneOS":"IOS","ImageName":"t_3@2x.png","ImageID":"3"},{"OSID":"2","PhoneVersion":"IPHONE5","PhoneOS":"IOS","ImageName":"t_4@2x.png","ImageID":"4"},{"OSID":"2","PhoneVersion":"IPHONE5","PhoneOS":"IOS","ImageName":"t_5@2x.png","ImageID":"5"},{"OSID":"2","PhoneVersion":"IPHONE5","PhoneOS":"IOS","ImageName":"t_6@2x.png","ImageID":"6"},{"OSID":"2","PhoneVersion":"IPHONE5","PhoneOS":"IOS","ImageName":"t_7@2x.png","ImageID":"7"},{"OSID":"2","PhoneVersion":"IPHONE5","PhoneOS":"IOS","ImageName":"t_8@2x.png","ImageID":"8"},{"OSID":"2","PhoneVersion":"IPHONE5","PhoneOS":"IOS","ImageName":"t_9@2x.png","ImageID":"9"},{"OSID":"2","PhoneVersion":"IPHONE5","PhoneOS":"IOS","ImageName":"t_10@2x.png","ImageID":"10"},{"OSID":"2","PhoneVersion":"IPHONE5","PhoneOS":"IOS","ImageName":"t_11@2x.png","ImageID":"11"},{"OSID":"2","PhoneVersion":"IPHONE5","PhoneOS":"IOS","ImageName":"t_12@2x.png","ImageID":"12"}]
</string>

You can see that its in the

<string xmlns="http://tempuri.org/">

I just wanted to know how to parse this type of respne the rest of the response whcih is Json I can do parsing of json by my self. please just help me how to do it

Coas Mckey
  • 701
  • 1
  • 13
  • 39

1 Answers1

2

This should work I haven't tested it yet

import android.util.Xml;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.InputStream;

public class StringXmlParser {
    // your xml doesn't have any name spacing so make it null.
    private static final String ns = null;

    public String parse(InputStream in) throws XmlPullParserException, IOException {
        try {
            XmlPullParser parser = Xml.newPullParser();
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
            parser.setInput(in,null);
            parser.nextTag();
            return readString(parser);
        } finally {
            in.close();
        }
    }

    private String readString(XmlPullParser parser) throws XmlPullParserException, IOException {
        parser.require(XmlPullParser.START_TAG, ns, "string");
        String jsonString = readText(parser);
        parser.require(XmlPullParser.END_TAG, ns, "string");
        return jsonString;
    }

    private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
        String result = "";
        if (parser.next() == XmlPullParser.TEXT) {
            result = parser.getText();
            parser.nextTag();
        }
        return result;
    }
}

And this is how you use it

StringXmlParser xmlParser = new StringXmlParser();
String jsonString;
try {
    // You need to get the input stream for the xml.
    jsonString = xmlParser.parse(inputStream);
} finally {
    if(inputStream != null) {
        inputStream.close();
    }
}
Bhargav
  • 8,118
  • 6
  • 40
  • 63
  • where it is returning the string after parsing the xml ? – Coas Mckey Nov 06 '15 at 06:55
  • the parse function returns your string, `jsonString` variable in the 2nd piece of code holds your json, in general the parse function is supposed to return whatever is inside the root element of the xml in java object, since the thing inside your root element is a json string that string is returned – Bhargav Nov 06 '15 at 06:57
  • so there is no need to create the string buffer and string builder after it ? – Coas Mckey Nov 06 '15 at 07:07
  • what do you need string buffer or string builder for? you get the entire string thats inside the `string` tag of the xml – Bhargav Nov 06 '15 at 07:08
  • yeah bro I am just asking , as I am beginner – Coas Mckey Nov 06 '15 at 07:10
  • 1
    yes it working as the part of my question is answered , please upvote my question as it is the latest and best working solution you provided me , may be useful to some one some other time . – Coas Mckey Nov 06 '15 at 11:08