我试图在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;
}