Create xml on the fly from a string

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

Answers


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

Need Your Help

When my timer ticks… .NET Memory Leak

c# .net multithreading timer

I have a .NET System.Threading.Timer timer that ticks every 60 seconds and introduces a memory leak on each tick.

How to get the value of record from array?

arrays postgresql

I need to get the value of first element of array records.