0

我正在尝试从 tomcat 服务器接收一个 xml 文件并对其进行解析。

但 xmlPullParser 事件在 xml 末尾返回 TEXT(值:4)。

为什么返回 4?如何解决这个问题呢?


[这是xml代码。]

由 OSX 的 texteditor 编写。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <school_record>
        <imagename>mark_goodschool.png</imagename>
        <code>001</code>
        <name>good_elementschool</name>
        <address>inhawaii</address>
        <tel>123-123-1234</tel>
        <homepage>http://elementschool.org</homepage>
    </school_record>
</resources>


[这是解析代码。]

Thread t_downloadRes = new Thread(new Runnable() {
    @Override
    public void run() {
        try {
            //Download xml File and parse it.

            URL url = new URL(STR_SERVER_URL + STR_SERVER_XML);
            InputStream is = url.openStream();

            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            XmlPullParser parser = factory.newPullParser();
            parser.setInput(new InputStreamReader(is, "utf-8"));

            int eventType = parser.getEventType();

            AA_SchoolDTO stu = null;

            while (eventType != XmlPullParser.END_DOCUMENT) {
                switch (eventType) {
                    case XmlPullParser.START_TAG:
                        String startTag = parser.getName();

                        if ("school_record".equals(startTag)) {
                            stu = new AA_SchoolDTO();
                        }
                        if (startTag.equals("imagename")) {
                            stu.setStr_imageName(parser.nextText());
                        }
                        if (startTag.equals("code")) {
                            stu.setStr_code(parser.nextText());
                        }
                        if (startTag.equals("name")) {
                            stu.setStr_name(parser.nextText());
                        }
                        if (startTag.equals("address")) {
                            stu.setStr_address(parser.nextText());
                        }
                        if (startTag.equals("tel")) {
                            stu.setStr_tel(parser.nextText());
                        }
                        if (startTag.equals("homepage")) {
                            stu.setStr_homPage(parser.nextText());
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        String endTag = parser.getName();
                        if ("school_record".equals(endTag))
                            allSchoolList.add(stu);
                        break;
                }
                eventType = parser.next(); //// <== return event TEXT (value : 4) at the end of xml. 
            }
            is.close();

        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
4

1 回答 1

0

问题:

parser.nextText()

您需要Text为解析器使用令牌,因此您需要在 switch 语句中再添加一项,XmlPullParser.TEXT以获取标签内的文本。

此外,您需要在not 中调用类的setter 方法AA_SchoolDTOEND_TAGSTART_TAG

但是AA_SchoolDTO在你的实例化对象START_TAG

样本:

while (eventType != XmlPullParser.END_DOCUMENT) {
    String textData;
    String startTag = parser.getName();
    switch (eventType) {
        case XmlPullParser.START_TAG:
            if ("school_record".equals(startTag)) {
                stu = new AA_SchoolDTO();
            }
            break;
        case XmlPullParser.TEXT:
            textData = xpp.getText();
            break;

        case XmlPullParser.END_TAG:
             if (startTag.equals("imagename")) {
                stu.setStr_imageName(textData);
            }
            if (startTag.equals("code")) {
                stu.setStr_code(textData);
            }
            if (startTag.equals("name")) {
                stu.setStr_name(textData);
            }
            if (startTag.equals("address")) {
                stu.setStr_address(textData);
            }
            if (startTag.equals("tel")) {
                stu.setStr_tel(textData);
            }
            if (startTag.equals("homepage")) {
                stu.setStr_homPage(textData);
            }
            if ("school_record".equals(endTag))
                allSchoolList.add(stu);
            break;
    }
    eventType = parser.next(); //// <== return event TEXT (value : 4) at the end of xml. 
于 2014-07-29T02:42:51.420 回答