XML DOM解析中的访问特定元素

时间:2017-06-09 10:38:16

标签: java android xml xml-parsing

我正在尝试解析下面的xml数据,我可以在Android中成功解析。现在我想只根据名称检索某些值(例如   1.awb_number   2.原产地   目的地   4. status)以便我只能更新应用程序中的那些信息。我应该怎么做

<?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"></field>
 </object>
 </ecomexpress-objects>

我能够成功解析XML数据并检索NodeValues。输出

I/System.out: Nodelength     29
I/System.out: Node Name  field
I/System.out: Node Value  102019265
I/System.out: Node Name  field
I/System.out: Node Value  8008444
I/System.out: Node Name  field
I/System.out: Node Value  2
I/System.out: Node Name  field
I/System.out: Node Value  DELHI-DSW
I/System.out: Node Name  field
I/System.out: Node Value  Mumbai-BOW
I/System.out: Node Name  field
I/System.out: Node Value  Mumbai - BOW
I/System.out: Node Name  field
I/System.out: Node Value  BOW
I/System.out: Node Name  field
I/System.out: Node Value  Ecom Express Private Limited - 32012
I/System.out: Node Name  field
I/System.out: Node Value  BEECHAND VERMA
I/System.out: Node Name  field
I/System.out: Node Value  22-Jan-2014
I/System.out: Node Name  field
I/System.out: Node Value  Undelivered
I/System.out: Node Name  field
I/System.out: Node Value  Undelivered
I/System.out: Node Name  field
I/System.out: Node Value  221 - Consignee Refused To Accept
I/System.out: Node Name  field
I/System.out: Node Value  Consignee Refused To Accept
I/System.out: Node Name  field
I/System.out: Node Value  221 
I/System.out: Node Name  field
I/System.out: Node Name  field
I/System.out: Node Value  15-Feb-2014
I/System.out: Node Name  field
I/System.out: Node Name  field
I/System.out: Node Name  field
I/System.out: Node Value  703063993
I/System.out: Node Name  field
I/System.out: Node Value  0
I/System.out: Node Name  field
I/System.out: Node Name  field
I/System.out: Node Value   Undelivered
I/System.out: Node Name  field
I/System.out: Node Value  777
I/System.out: Node Name  field
I/System.out: Node Value  22 Jan, 2014, 12:44 
I/System.out: Node Name  field
I/System.out: Node Value  400037
I/System.out: Node Name  field
I/System.out: Node Value  MUMBAI
I/System.out: Node Name  field
I/System.out: Node Value  Maharashtra
I/System.out: Node Name  field

XML Parser

  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                    DocumentBuilder db = dbf.newDocumentBuilder();
                    InputSource is;
                    is = new InputSource(new StringReader(xml));
                    Document doc = db.parse(is);
                    doc.getDocumentElement().normalize();
                    NodeList nodes = doc.getElementsByTagName("field");
                    System.out.println("Nodelength     "+nodes.getLength());
                    for (int j=0; j<nodes.getLength(); j++) {

                        Node node = nodes.item(j);
                        String t1 = node.getNodeName();
                        System.out.println("Node Name  " + t1);
                        NodeList nodeList1 = node.getChildNodes();
                        for (int k = 0; k < nodeList1.getLength(); k++) {

                            Node node1 = nodeList1.item(k);
                            String  t3 = node1.getNodeValue();
                            System.out.println("Node Value  " + t3);
                        }
                    }

                } catch (SAXException e) {
                    e.printStackTrace();
                } catch (ParserConfigurationException e) {
                    e.printStackTrace();
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
             catch (Exception e) {
                e.printStackTrace();
            }

2 个答案:

答案 0 :(得分:0)

我会用Xpath解决这个问题。例如:

假设xml文件位于文件:“/ tmp / ecomexpress.xml”

在下面的示例中,我将找到名称为“current_location_name”的字段值

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.File;
import java.io.IOException;


/**
 *
 */
public class XmlParser {

    public static void main(String argv[]) throws IOException, SAXException, XPathExpressionException, ParserConfigurationException {

        File fXmlFile = new File("/tmp/ecomexpress.xml");
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(fXmlFile);
        XPathFactory xPathfactory = XPathFactory.newInstance();
        XPath xpath = xPathfactory.newXPath();
        XPathExpression expr = xpath.compile("//object/field[@name='current_location_name']/text()");

        String value = (String) expr.evaluate(doc, XPathConstants.STRING);
        System.out.println(value);  // Mumbai - BOW

    }
}

答案 1 :(得分:0)

我使用以下代码解决了它

try {
       DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
       DocumentBuilder db = dbf.newDocumentBuilder();
       InputSource is;
       is = new InputSource(new StringReader(xml));
       Document doc = db.parse(is);
       doc.getDocumentElement().normalize();
       NodeList nodes = doc.getElementsByTagName("field");
       Node node = nodes.item(1);
       if (node.getNodeType() == Node.ELEMENT_NODE)
       {
           Element eElement = (Element) node;
           String orderid=eElement.getTextContent();
           orderidtv.setText(orderid);

       }
       Node node2 = nodes.item(3);
       if (node2.getNodeType() == Node.ELEMENT_NODE)
       {
                    Element eElement = (Element) node2;
                    origintv.setText(orderid);

       }
   }