Create xml on the fly from a string

╄→гoц情女王★ 提交于 2019-12-11 00:06:47

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/4343509/create-xml-on-the-fly-from-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!