-2

Can Any one tell me How to parse the following

    <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>

I am hitting my url with some paramaeters with the help of following method , is it right ?

 public String getJSON(String url, int timeout) {
                HttpURLConnection c = null;
                try {
                    URL u = new URL(url);
                    c = (HttpURLConnection) u.openConnection();
                    c.setRequestMethod("GET");
                    c.setRequestProperty("Content-length", "0");
                    c.setUseCaches(false);
                    c.setAllowUserInteraction(false);
                    c.setConnectTimeout(timeout);
                    c.setReadTimeout(timeout);
                    c.setRequestProperty("Content-Type", "application/json");
                    c.connect();
                    int status = c.getResponseCode();

                    switch (status) {
                        case 200:
                        case 201:
                            BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
                            StringBuilder sb = new StringBuilder();
                            String line;
                            while ((line = br.readLine()) != null) {
                                sb.append(line+"\n");
                            }
                            br.close();
                            return sb.toString();
                    }

                } catch (MalformedURLException ex) {
                    Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
                } finally {
                    if (c != null) {
                        try {
                            c.disconnect();
                        } catch (Exception ex) {
                            Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }
                return null;
            }

is Right to do and How to parse the response please answer me with source code.

Coas Mckey
  • 701
  • 1
  • 13
  • 39
  • 1
    obviously, first parse xml, then inner json ... – Selvin Nov 05 '15 at 14:43
  • ... on the other hand ... why? why? why on hell server-side returns such thing ? ... why not json or xml - not both – Selvin Nov 05 '15 at 14:45
  • I do not know why they are sending me this reply I am bound , so please can you tell me how to parse turn by turn , parsing and Am I sending and conecting to server right ? – Coas Mckey Nov 05 '15 at 14:49
  • 1
    its better to advice server side change this format to json , or you will have to parse xml then read json and parse it , you can skip huge coding to parse xml tag by using pattern – Mina Fawzy Nov 08 '15 at 15:53

1 Answers1

0

Thanks for all for the replies and make me understand what steps do I need to do this. Well I was asking about how to remove the xml tags when parsing xml well it was quite obvious that parsing xml is little more tricky as compare to json and yes from the tutorial I learnt how to parse it but was answerable how to parse this xml response which has no name space and no method name etc

So I am posting this as an answer if some one trap in the same case (specially when web designers/server side programmers putting it on you :( . )

I have used the following class to parse the xml with out name space and r returned me string. The code goes like as following

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;
}

}

This has solved my case. I hope if some one want the same solution then it may help him out. Just uploading the answer for the sole purpose of helping pure beginners.

Coas Mckey
  • 701
  • 1
  • 13
  • 39