本文介绍了在从字符串动态创建的XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Andr​​oid。我有一个包含XML格式的数据(它的XML文档)服务器返回给我一个字符串。我想与 XmlResourceParser 解析它在Android中,但只接受存储在您的设备,例如XML文档:

  XmlResourceParser xmlResourceParser = getResources()的getXML(
                R.xml.new_xml);




 &所述;?xml的版本= \1.0 \编码= \UTF-8 \>+
                    &其中; ItemInfo>中+<状态>成功与LT; /状态>
                +<原因>< /原因>中+< IsMore>是< / IsMore>中
                +&其中; ITEMLIST>中
                +<项目名称= \艾哈迈德\等级= \1 \用户ID = \5 \>< /项目>中
                +<项目名称= \艾哈迈德\等级= \1 \用户ID = \5 \>< /项目>中
                +<项目名称= \艾哈迈德\等级= \1 \用户ID = \5 \>< /项目>中
                +&所述; / ITEMLIST>中+&所述; / ItemInfo>中;
 

我想XmlResourceParser来分析我的这个字符串。但如何使之接受这个字符串作为一个XML文档。使用下面的方法,但装箱异常

 私人XmlPullParser xmlResourceParser;


 XML = xml.replaceAll(\ t的,);
                    尝试 {
            XmlPullParserFactory工厂= XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(真正的);
            xmlResourceParser = factory.newPullParser();
            xmlResourceParser.setInput(新StringReader(XML));
        }赶上(例外前){
            ex.printStackTrace();
        }
INT EVENTTYPE = -1;
        而(EVENTTYPE!= XmlResourceParser.END_DOCUMENT){
            尝试 {
                如果(EVENTTYPE == XmlResourceParser.START_TAG){

                    串strNode = xmlResourceParser.getName();
                    如果(strNode.equalsIgnoreCase(状态)){
                        做 {
                            xmlResourceParser.next();
                        }而(EVENTTYPE == XmlResourceParser.TEXT);
                        如果(!xmlResourceParser.getText()。equalsIgnoreCase(
                                成功)) {
                            做 {
                                EVENTTYPE = xmlResourceParser.next();
                            }而(EVENTTYPE = XmlResourceParser.START_TAG!);
                            如果(xmlResourceParser.getName()。equalsIgnoreCase(
                                    原因))
                                EVENTTYPE = xmlResourceParser.next();
                            todoItems.add(xmlResourceParser.getText());
                            返回todoItems;
                        }
                    }
                    如果(strNode.equals(IsMore)){
                        做 {
                            xmlResourceParser.next();
                        }而(EVENTTYPE == XmlResourceParser.TEXT);
                        如果(xmlResourceParser.getText()。equalsIgnoreCase(是))
                            todoItems.add(下一页);如果//下一页再有
                        //是另外一页
                        //否则没有下
                        // 页
                        其他
                            todoItems.add(末页); //显示用户透水
                        //按钮代替
                        //下一个按钮
                    }
                    如果(strNode.equals(项目)){
                        todoItems.add(xmlResourceParser.getAttributeValue(空,
                                名称));
                        todoItems.add(xmlResourceParser.getAttributeValue(空,
                                水平));
                        todoItems.add(xmlResourceParser.getAttributeValue(空,
                                用户帐号));

                    }
                }

                EVENTTYPE = xmlResourceParser.next();
            }赶上(XmlPullParserException xmlPullExp){
                xmlPullExp.printStackTrace();
                todoItems.add(XmlPullParserException);
            }赶上(IOException异常ioExp){
                ioExp.printStackTrace();
                todoItems.add(IOException异常);
            }赶上(例外EXP){
                exp.printStackTrace();
                todoItems.add(异常);
            }
        }
        返回todoItems;
    }
 

解决方案

尝试使用XmlPullParser

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

例如:

 的InputStream xmlIS;
.....
XmlPullParserFactory工厂= XmlPullParserFactory.newInstance();
factory.setNamespaceAware(真正的);
XmlPullParser XPP = factory.newPullParser();
xpp.setInput(xmlIS,NULL);
 

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;
    }
解决方案

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

这篇关于在从字符串动态创建的XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 05:42