使用Web服务调用,我从服务器得到以下响应。现在我需要解析这个响应,提取所有字段值并将其存储在字符串值
中<?xml version="1.0" encoding="utf-8"?>
<ecomexpress-objects version="1.0"><object pk="1" model="awb">
<field type="BigIntegerField" name="awb_number">102019265</field>
<field type="CharField" name="orderid">8008444</field>
<field type="FloatField" name="actual_weight">2</field>
<field type="CharField" name="origin">DELHI-DSW</field>
<field type="CharField" name="destination">Mumbai - BOW</field>
<field type="CharField" name="current_location_name">Mumbai - BOW</field>
<field type="CharField" name="current_location_code">BOW</field>
<field type="CharField" name="customer">Ecom Express Private Limited - 32012</field>
<field type="CharField" name="consignee">BEECHAND VERMA</field>
<field type="CharField" name="pickupdate">22-Jan-2014</field>
<field type="CharField" name="status">Undelivered</field>
<field type="CharField" name="tracking_status">Undelivered</field>
<field type="CharField" name="reason_code">221 - Consignee Refused To Accept</field>
<field type="CharField" name="reason_code_description">Consignee Refused To Accept</field>
<field type="CharField" name="reason_code_number">221 </field>
<field type="CharField" name="receiver"></field>
<field type="CharField" name="expected_date" >15-Feb-2014</field>
<field type="CharField" name="last_update_date" ></field>
<field type="CharField" name="delivery_date" ></field>
<field type="CharField" name="ref_awb" >703063993</field>
<field type="CharField" name="rts_shipment" >0</field>
<field type="CharField" name="system_delivery_update" ></field>
<field type="CharField" name="rts_system_delivery_status" Undelivered</field>
<field type="CharField" name="rts_reason_code_number">777</field>
<field type="CharField" name="rts_last_update">22 Jan, 2014, 12:44 </field>
<field type="CharField" name="pincode" >400037</field>
<field type="CharField" name="city" >MUMBAI</field>
<field type="CharField" name="state" >Maharashtra</field>
<field name="scans">
</ecomexpress-objects>
如果我尝试以下代码来解析
String xml=result.toString();
try{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is;
is = new InputSource(new StringReader(xml));
Document doc = db.parse(is);
NodeList nodelist = doc.getChildNodes();
}
catch (SAXException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
我收到以下错误。
org.xml.sax.SAXParseException: expected: /field read: ecomexpress-objects (position:END_TAG </ecomexpress-objects>@1:1917 in java.io.StringReader@39978dff)
我需要将所有字段值存储在相应的字符串变量
中答案 0 :(得分:1)
xml中有几个错误:
没有结束标记object
。
Undelivered
出现在<field type="CharField"
name="rts_system_delivery_status" Undelivered</field>
应该像<field name="scans"/>
只要xml无效,就会不断出现异常。
答案 1 :(得分:0)
您的XML无效。
标签<field name="scans">
未关闭。