0

我试图在android使用中解析 XML 数据,XMLPullParser但我无法解析它。

以下是我的 XML 数据的一部分。任何人都可以帮我解析这个 XML。

任何帮助将不胜感激。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<live
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <tt1>
        <active_flag>A</active_flag>
        <apt/>
        <attachments>false</attachments>
        <avail_for_dispatch_time>0</avail_for_dispatch_time>
        <calltaker_id/>
        <call_checks>0</call_checks>
        <call_number>20</call_number>
        <city/>
        <common_name/>
        <community/>
        <cr1_direction/>
        <cr1_suffix>AV</cr1_suffix>
        <cr2_direction>S</cr2_direction>
        <cr2_suffix>AV</cr2_suffix>
        <cr_st_1>SYMONDS</cr_st_1>
        <cr_st_2>ORLANDO</cr_st_2>
        <ct_position/>
        <department>FD</department>
        <direction>N</direction>
        <dispatch>false</dispatch>
        <dispatcher_id>INFOTECH03</dispatcher_id>
        <display_code>FD</display_code>
        <disposition/>
        <disp_area/>
        <disp_date>2014-04-02</disp_date>
        <disp_time>48536</disp_time>
        <disp_to/>
        <event_number>2014001037</event_number>
        <event_type>DEMO</event_type>
        <exact_number>200</exact_number>
        <final_dispos_code>Report</final_dispos_code>
        <first_disp_date>2014-04-02</first_disp_date>
        <first_disp_time>48574</first_disp_time>
        <first_unit_id/>
        <f_arr_date>2014-04-02</f_arr_date>
        <f_arr_time>49635</f_arr_time>
        <f_enr_date>2014-04-02</f_enr_date>
        <f_enr_time>49635</f_enr_time>
        <hold_alert_counter>0</hold_alert_counter>
        <jurisdiction>WP</jurisdiction>
        <level2>6103</level2>
        <level3/>
        <level4/>
        <level5>WINTER PARK</level5>
        <lupdt_date>2014-08-19</lupdt_date>
        <lupdt_time>47303</lupdt_time>
        <lupdt_user>1023</lupdt_user>
        <lupdt_workstation_id>JWPC</lupdt_workstation_id>
        <l_clr_date>2014-08-13</l_clr_date>
        <l_clr_time>40233</l_clr_time>
        <next_check_time>99999999</next_check_time>
     </tt1>
</live>

以下是我用于解析的代码:

public void parse(String liveCADData) throws XmlPullParserException,
        IOException {
    InputStream in = null;
    try {
        in = new ByteArrayInputStream(liveCADData.getBytes());
        InputStream in1 = in;
        BOMInputStream bin = new BOMInputStream(in1, false,
                ByteOrderMark.UTF_8);
        InputStreamReader in2 = new InputStreamReader(bin);
        XmlPullParser parser = Xml.newPullParser();
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
        parser.setInput(in2);
        parser.nextTag();
        readItems(parser);
    } finally {
        in.close();
    }
}

private void readItems(XmlPullParser parser) throws XmlPullParserException,
        IOException {
    parser.require(XmlPullParser.START_TAG, ns, "live");
    while (parser.next() != XmlPullParser.END_TAG) {
        if (parser.getEventType() != XmlPullParser.END_TAG) {
            continue;
        }
        String name = parser.getName();
        if (name.equals("tt1")) {
            String val = readStringValue(parser, "tt1");
        }
    }

}

private String readStringValue(XmlPullParser parser, String tag)
            throws IOException, XmlPullParserException {
        parser.require(XmlPullParser.START_TAG, ns, tag);
        String stringValue = readText(parser);
        parser.require(XmlPullParser.END_TAG, ns, tag);
        return stringValue;
    }
4

1 回答 1

1

标签tt1没有价值。标记的名称是tt1

考虑以下

<active_flag>A</active_flag>

active_flag是标签名称,值为A

这是你出错的地方

  private String readStringValue(XmlPullParser parser, String tag)
            throws IOException, XmlPullParserException {
        parser.require(XmlPullParser.START_TAG, ns, tag);
        String stringValue = readText(parser); // no value for tt1
        parser.require(XmlPullParser.END_TAG, ns, tag);
        return stringValue;
    }

我只展示了如何从特定标签中获取值。样本应该足以自己解析其余部分。解析

public class XMLPullParserHandler {

    private String text;

    public XMLPullParserHandler() {

    }
    public Void parse(InputStream is) {
        XmlPullParserFactory factory = null;
        XmlPullParser parser = null;
        try {
            factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            parser = factory.newPullParser();

            parser.setInput(is, null);
            boolean check =false;

           //factory instantiates an object

            int eventType = parser.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                 String tagname = parser.getName();
                switch (eventType) {
                case XmlPullParser.START_TAG:
                    break;

                case XmlPullParser.TEXT:
                    text = parser.getText();
                    break;

                case XmlPullParser.END_TAG:
                          String val = null; 

                          if (tagname.equalsIgnoreCase("active_flag")) { 

                             Log.i("Flag is",text);

                            }
                          if (tagname.equalsIgnoreCase("cr_st_1")) { 

                                 Log.i("cr_st_1 is",text);

                             }
                          if (tagname.equalsIgnoreCase("cr_st_2")) { 

                             Log.i("cr_st_2 is",text);

                          }                     

                    break;

                default:
                    break;
                }
                eventType = parser.next();
            }

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

        return null;
    }
}

这是日志

08-20 12:53:09.155: I/Flag is(11804): A
08-20 12:53:09.160: I/cr_st_1 is(11804): SYMONDS
08-20 12:53:09.160: I/cr_st_2 is(11804): ORLANDO

我希望您可以根据上面的示例解析其余的值

更多信息在

http://developer.android.com/training/basics/network-ops/xml.html

编辑:

按照您的解析方式进行

public class ParserParsing {
    private static final String ns = null;
    private Context mContext;

public Void parse(InputStream in,Context context)
{ 
    mContext = context;      
    try {
        XmlPullParser parser = Xml.newPullParser();
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
        parser.setInput(in, null);
        parser.nextTag();
        readFeed(parser);

    } catch(Exception e){
            e.printStackTrace();
    }
     return null;
}
private  Void readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {

parser.require(XmlPullParser.START_TAG, ns, "live");
    while (parser.next() != XmlPullParser.END_TAG) {
        if (parser.getEventType() != XmlPullParser.START_TAG) {
            continue;
        }
        String name = parser.getName();
        if (name.equals("tt1")) {
            readTt1(parser);
        } else {
            skip(parser);
        }
    }  
    return null;
}
private Void readTt1(XmlPullParser parser) throws XmlPullParserException, IOException {
    parser.require(XmlPullParser.START_TAG, ns, "tt1");
    while (parser.next() != XmlPullParser.END_TAG) {
        if (parser.getEventType() != XmlPullParser.START_TAG) {
            continue;
        }
        String name = parser.getName();

        if (name.equals("active_flag")) {
            String flag =readflag(parser);
            Toast.makeText(mContext, flag, Toast.LENGTH_LONG).show();
        } else {
            skip(parser);
        }
    } 
    return null;
}
private String readflag(XmlPullParser parser) throws IOException, XmlPullParserException {
    parser.require(XmlPullParser.START_TAG, ns, "active_flag");
    String flag =  readText(parser);

    parser.require(XmlPullParser.END_TAG, ns, "active_flag");
    return flag;
}
     private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
            String result = "";
            if (parser.next() == XmlPullParser.TEXT) {
                result = parser.getText();
                parser.nextTag();
            }
            return result;
        }
        private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                throw new IllegalStateException();
            }
            int depth = 1;
            while (depth != 0) {
                switch (parser.next()) {
                case XmlPullParser.END_TAG:
                    depth--;
                    break;
                case XmlPullParser.START_TAG:
                    depth++;
                    break;
                }
            }
         }    
}
于 2014-08-20T07:26:15.380 回答