Question

This is correct program and now running fine :) thank you sir

I am new to Android. I have a String that contains XML format data (its an XML document) that servers returns to me. I want to parse it in Android with XmlResourceParser but it only accepts xml documents that are stored on your device e.g:

XmlResourceParser xmlResourceParser = getResources().getXml(
                R.xml.new_xml);




 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+ 
                    "<ItemInfo>" + "<Status>Success</Status>"
                + "<Reason></Reason>" + "<IsMore>yes</IsMore>"
                + "<ItemList>"
                + "<Item Name=\"Ahmad\" Level=\"1\" UserId=\"5\"></Item >"
                + "<Item Name=\"Ahmad\" Level=\"1\" UserId=\"5\"></Item >"
                + "<Item Name=\"Ahmad\" Level=\"1\"UserId=\"5\"></Item >"
                + "</ItemList>" + "</ItemInfo>";

I want XmlResourceParser to parse my this string. but how to make it accept this string as an xml document. using following method but crating exception

private XmlPullParser xmlResourceParser;


 xml = xml.replaceAll("\t", "");
                    try {           
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            xmlResourceParser = factory.newPullParser();
            xmlResourceParser.setInput(new StringReader(xml));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
int eventType = -1;
        while (eventType != XmlResourceParser.END_DOCUMENT) {
            try {
                if (eventType == XmlResourceParser.START_TAG) {

                    String strNode = xmlResourceParser.getName();
                    if (strNode.equalsIgnoreCase("Status")) {
                        do {
                            xmlResourceParser.next();
                        } while (eventType == XmlResourceParser.TEXT);
                        if (!xmlResourceParser.getText().equalsIgnoreCase(
                                "success")) {
                            do {
                                eventType = xmlResourceParser.next();
                            } while (eventType != XmlResourceParser.START_TAG);
                            if (xmlResourceParser.getName().equalsIgnoreCase(
                                    "reason"))
                                eventType = xmlResourceParser.next();
                            todoItems.add(xmlResourceParser.getText());
                            return todoItems;
                        }
                    }
                    if (strNode.equals("IsMore")) {
                        do {
                            xmlResourceParser.next();
                        } while (eventType == XmlResourceParser.TEXT);
                        if (xmlResourceParser.getText().equalsIgnoreCase("yes"))
                            todoItems.add("nextPage");// if nextPage then there
                        // is one more page
                        // otherwise no next
                        // page
                        else
                            todoItems.add("lastPage");// show user pervious
                        // button instead of
                        // next button
                    }
                    if (strNode.equals("Item")) {
                        todoItems.add(xmlResourceParser.getAttributeValue(null,
                                "Name"));
                        todoItems.add(xmlResourceParser.getAttributeValue(null,
                                "Level"));
                        todoItems.add(xmlResourceParser.getAttributeValue(null,
                                "UserId"));

                    }                       
                }

                eventType = xmlResourceParser.next();
            } catch (XmlPullParserException xmlPullExp) {
                xmlPullExp.printStackTrace();
                todoItems.add("XmlPullParserException");
            } catch (IOException ioExp) {
                ioExp.printStackTrace();
                todoItems.add("IOException");
            } catch (Exception exp) {
                exp.printStackTrace();
                todoItems.add("Exception");
            }
        }
        return todoItems;
    }
Was it helpful?

Solution

Try using XmlPullParser

http://developer.android.com/reference/org/xmlpull/v1/XmlPullParser.html

For example:

InputStream xmlIS;
.....
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(xmlIS, null);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top