android中的java解析xml

6kkfgxo0  于 2021-07-09  发布在  Java
关注(0)|答案(3)|浏览(404)

我有以下格式的xml

<Report name="MotionSummary" type="device.detail" format="xml">
    <Account>sysadmin</Account>
    <TimeFrom timestamp="1394044200" timezone="GMT+05:30">2014/03/06,00:00:00</TimeFrom>
    <TimeTo timestamp="1394130540" timezone="GMT+05:30">2014/03/06,23:59:00</TimeTo>
    <ValidGPSRequired>false</ValidGPSRequired>
    <SelectionLimit type="FIRST">4000</SelectionLimit>
    <Ascending>true</Ascending>
    <ReportLimit>1000</ReportLimit>
    <Title>Driving/Stopped Time Summary [Based on 0 Minute Minimum Stop]</Title>
    <Subtitle>Honda City [mh-31-cr-0742]\n'2014/03/06' through '2014/03/06
        23:59:00' [GMT+05:30]</Subtitle>
    <ReportHeader>
        <HeaderRow class="rptHdrRow">
            <HeaderColumn id="index" class="rptHdrCol_nosort">#</HeaderColumn>
            <HeaderColumn id="startdatetime" class="rptHdrCol_nosort">Start\nDate/Time</HeaderColumn>
            <HeaderColumn id="drivingelapse" class="rptHdrCol_nosort">Driving\nElapsed</HeaderColumn>
            <HeaderColumn id="odomdelta" class="rptHdrCol_nosort">Driven\nKm</HeaderColumn>
            <HeaderColumn id="stopdatetime" class="rptHdrCol_nosort">Stop\nDate/Time</HeaderColumn>
            <HeaderColumn id="stopgeopoint" class="rptHdrCol_nosort">Lat/Lon</HeaderColumn>
            <HeaderColumn id="stopaddress" class="rptHdrCol_nosort">Address</HeaderColumn>
            <HeaderColumn id="idleelapse" class="rptHdrCol_nosort">Trip Idle\nElapsed</HeaderColumn>
            <HeaderColumn id="stopelapse" class="rptHdrCol_nosort">Stopped\nElapsed</HeaderColumn>
        </HeaderRow>
    </ReportHeader>
    <ReportBody>
        <BodyRow class="rptBodyRowOdd">
            <BodyColumn id="index" class="rptBodyCol">1</BodyColumn>
            <BodyColumn id="startdatetime" class="rptBodyCol">2014/03/06 09:41:38</BodyColumn>
            <BodyColumn id="drivingelapse" class="rptBodyCol">0:00:01</BodyColumn>
            <BodyColumn id="odomdelta" class="rptBodyCol">0.0</BodyColumn>
            <BodyColumn id="stopdatetime" class="rptBodyCol">2014/03/06 09:41:39</BodyColumn>
            <BodyColumn id="stopgeopoint" class="rptBodyCol">21.0906/79.0650</BodyColumn>
            <BodyColumn id="stopaddress" class="rptBodyCol">Home Zone</BodyColumn>
            <BodyColumn id="idleelapse" class="rptBodyCol">0:00:18</BodyColumn>
            <BodyColumn id="stopelapse" class="rptBodyCol">0:00:18</BodyColumn>
        </BodyRow>
        <BodyRow class="rptBodyRowEven">
            <BodyColumn id="index" class="rptBodyCol">2</BodyColumn>
            <BodyColumn id="startdatetime" class="rptBodyCol">2014/03/06 09:41:57</BodyColumn>
            <BodyColumn id="drivingelapse" class="rptBodyCol">0:01:26</BodyColumn>
            <BodyColumn id="odomdelta" class="rptBodyCol">0.3</BodyColumn>
            <BodyColumn id="stopdatetime" class="rptBodyCol">2014/03/06 09:43:23</BodyColumn>
            <BodyColumn id="stopgeopoint" class="rptBodyCol">21.0936/79.0655</BodyColumn>
            <BodyColumn id="stopaddress" class="rptBodyCol" />
            <BodyColumn id="idleelapse" class="rptBodyCol">0:02:07</BodyColumn>
            <BodyColumn id="stopelapse" class="rptBodyCol">0:02:07</BodyColumn>
        </BodyRow>
        <BodyRow class="rptBodyRowOdd">
            <BodyColumn id="index" class="rptBodyCol">3</BodyColumn>
            <BodyColumn id="startdatetime" class="rptBodyCol">2014/03/06 09:45:30</BodyColumn>
            <BodyColumn id="drivingelapse" class="rptBodyCol">0:02:33</BodyColumn>
            <BodyColumn id="odomdelta" class="rptBodyCol">0.7</BodyColumn>
            <BodyColumn id="stopdatetime" class="rptBodyCol">2014/03/06 09:48:03</BodyColumn>
            <BodyColumn id="stopgeopoint" class="rptBodyCol">21.1025/79.0675</BodyColumn>
            <BodyColumn id="stopaddress" class="rptBodyCol" />
            <BodyColumn id="idleelapse" class="rptBodyCol">0:01:47</BodyColumn>
            <BodyColumn id="stopelapse" class="rptBodyCol">0:01:47</BodyColumn>
        </BodyRow>
        <BodyRow class="rptBodyRowEven">
            <BodyColumn id="index" class="rptBodyCol">4</BodyColumn>
            <BodyColumn id="startdatetime" class="rptBodyCol">2014/03/06 09:49:50</BodyColumn>
            <BodyColumn id="drivingelapse" class="rptBodyCol">0:02:46</BodyColumn>
            <BodyColumn id="odomdelta" class="rptBodyCol">0.6</BodyColumn>
            <BodyColumn id="stopdatetime" class="rptBodyCol">2014/03/06 09:52:36</BodyColumn>
            <BodyColumn id="stopgeopoint" class="rptBodyCol">21.0888/79.0651</BodyColumn>
            <BodyColumn id="stopaddress" class="rptBodyCol">Home Zone</BodyColumn>
            <BodyColumn id="idleelapse" class="rptBodyCol">0:31:39</BodyColumn>
            <BodyColumn id="stopelapse" class="rptBodyCol">0:31:39</BodyColumn>
        </BodyRow>
        <BodyRow class="rptBodyRowOdd">
            <BodyColumn id="index" class="rptBodyCol">5</BodyColumn>
            <BodyColumn id="startdatetime" class="rptBodyCol">2014/03/06 10:24:15</BodyColumn>
            <BodyColumn id="drivingelapse" class="rptBodyCol">0:03:23</BodyColumn>
            <BodyColumn id="odomdelta" class="rptBodyCol" />
            <BodyColumn id="stopdatetime" class="rptBodyCol">2014/03/06 10:27:38</BodyColumn>
            <BodyColumn id="stopgeopoint" class="rptBodyCol">21.1016/79.0676</BodyColumn>
            <BodyColumn id="stopaddress" class="rptBodyCol" />
            <BodyColumn id="idleelapse" class="rptBodyCol">0:00:19</BodyColumn>
            <BodyColumn id="stopelapse" class="rptBodyCol">0:00:19</BodyColumn>
        </BodyRow>
        <BodyRow class="rptBodyRowEven">
            <BodyColumn id="index" class="rptBodyCol">6</BodyColumn>
            <BodyColumn id="startdatetime" class="rptBodyCol">2014/03/06 10:27:57</BodyColumn>
            <BodyColumn id="drivingelapse" class="rptBodyCol">0:02:46</BodyColumn>
            <BodyColumn id="odomdelta" class="rptBodyCol">1.4</BodyColumn>
            <BodyColumn id="stopdatetime" class="rptBodyCol">2014/03/06 10:30:43</BodyColumn>
            <BodyColumn id="stopgeopoint" class="rptBodyCol">21.1078/79.0792</BodyColumn>
            <BodyColumn id="stopaddress" class="rptBodyCol">KIPL Zone</BodyColumn>
            <BodyColumn id="idleelapse" class="rptBodyCol">0:00:10</BodyColumn>
            <BodyColumn id="stopelapse" class="rptBodyCol">0:00:10</BodyColumn>
        </BodyRow>
        <BodyRow class="rptBodyRowOdd">
            <BodyColumn id="index" class="rptBodyCol">7</BodyColumn>
            <BodyColumn id="startdatetime" class="rptBodyCol">2014/03/06 10:30:53</BodyColumn>
            <BodyColumn id="drivingelapse" class="rptBodyCol">0:01:20</BodyColumn>
            <BodyColumn id="odomdelta" class="rptBodyCol">0.3</BodyColumn>
            <BodyColumn id="stopdatetime" class="rptBodyCol">2014/03/06 10:32:13</BodyColumn>
            <BodyColumn id="stopgeopoint" class="rptBodyCol">21.1071/79.0824</BodyColumn>
            <BodyColumn id="stopaddress" class="rptBodyCol">KIPL Zone</BodyColumn>
            <BodyColumn id="idleelapse" class="rptBodyCol">2:45:23</BodyColumn>
            <BodyColumn id="stopelapse" class="rptBodyCol">2:45:23</BodyColumn>
        </BodyRow>
        <BodyRow class="rptBodyRowEven">
            <BodyColumn id="index" class="rptBodyCol">8</BodyColumn>
            <BodyColumn id="startdatetime" class="rptBodyCol">2014/03/06 13:17:36</BodyColumn>
            <BodyColumn id="drivingelapse" class="rptBodyCol">0:03:48</BodyColumn>
            <BodyColumn id="odomdelta" class="rptBodyCol" />
            <BodyColumn id="stopdatetime" class="rptBodyCol">2014/03/06 13:21:24</BodyColumn>
            <BodyColumn id="stopgeopoint" class="rptBodyCol">21.0984/79.0669</BodyColumn>
            <BodyColumn id="stopaddress" class="rptBodyCol" />
            <BodyColumn id="idleelapse" class="rptBodyCol">0:00:30</BodyColumn>
            <BodyColumn id="stopelapse" class="rptBodyCol">0:00:30</BodyColumn>
        </BodyRow>
        <BodyRow class="rptBodyRowOdd">
            <BodyColumn id="index" class="rptBodyCol">9</BodyColumn>
            <BodyColumn id="startdatetime" class="rptBodyCol">2014/03/06 13:21:54</BodyColumn>
            <BodyColumn id="drivingelapse" class="rptBodyCol">0:01:59</BodyColumn>
            <BodyColumn id="odomdelta" class="rptBodyCol">1.1</BodyColumn>
            <BodyColumn id="stopdatetime" class="rptBodyCol">2014/03/06 13:23:53</BodyColumn>
            <BodyColumn id="stopgeopoint" class="rptBodyCol">21.0888/79.0651</BodyColumn>
            <BodyColumn id="stopaddress" class="rptBodyCol">Home Zone</BodyColumn>
            <BodyColumn id="idleelapse" class="rptBodyCol">1:30:41</BodyColumn>
            <BodyColumn id="stopelapse" class="rptBodyCol">1:30:41</BodyColumn>
        </BodyRow>
        <BodyRow class="rptBodyRowEven">
            <BodyColumn id="index" class="rptBodyCol">10</BodyColumn>
            <BodyColumn id="startdatetime" class="rptBodyCol">2014/03/06 14:54:34</BodyColumn>
            <BodyColumn id="drivingelapse" class="rptBodyCol">0:00:44</BodyColumn>
            <BodyColumn id="odomdelta" class="rptBodyCol" />
            <BodyColumn id="stopdatetime" class="rptBodyCol">2014/03/06 14:55:18</BodyColumn>
            <BodyColumn id="stopgeopoint" class="rptBodyCol">21.0905/79.0650</BodyColumn>
            <BodyColumn id="stopaddress" class="rptBodyCol">Home Zone</BodyColumn>
            <BodyColumn id="idleelapse" class="rptBodyCol">0:00:07</BodyColumn>
            <BodyColumn id="stopelapse" class="rptBodyCol">0:00:07</BodyColumn>
        </BodyRow>
        <BodyRow class="rptBodyRowOdd">
            <BodyColumn id="index" class="rptBodyCol">11</BodyColumn>
            <BodyColumn id="startdatetime" class="rptBodyCol">2014/03/06 14:55:25</BodyColumn>
            <BodyColumn id="drivingelapse" class="rptBodyCol">0:01:37</BodyColumn>
            <BodyColumn id="odomdelta" class="rptBodyCol">0.7</BodyColumn>
            <BodyColumn id="stopdatetime" class="rptBodyCol">2014/03/06 14:57:02</BodyColumn>
            <BodyColumn id="stopgeopoint" class="rptBodyCol">21.0967/79.0665</BodyColumn>
            <BodyColumn id="stopaddress" class="rptBodyCol" />
            <BodyColumn id="idleelapse" class="rptBodyCol">0:00:25</BodyColumn>
            <BodyColumn id="stopelapse" class="rptBodyCol">0:00:25</BodyColumn>
        </BodyRow>
        <BodyRow class="rptBodyRowEven">
            <BodyColumn id="index" class="rptBodyCol">12</BodyColumn>
            <BodyColumn id="startdatetime" class="rptBodyCol">2014/03/06 14:57:27</BodyColumn>
            <BodyColumn id="drivingelapse" class="rptBodyCol">0:00:50</BodyColumn>
            <BodyColumn id="odomdelta" class="rptBodyCol">0.1</BodyColumn>
            <BodyColumn id="stopdatetime" class="rptBodyCol">2014/03/06 14:58:17</BodyColumn>
            <BodyColumn id="stopgeopoint" class="rptBodyCol">21.0979/79.0668</BodyColumn>
            <BodyColumn id="stopaddress" class="rptBodyCol" />
            <BodyColumn id="idleelapse" class="rptBodyCol">0:01:01</BodyColumn>
            <BodyColumn id="stopelapse" class="rptBodyCol">0:01:01</BodyColumn>
        </BodyRow>
        <BodyRow class="rptBodyRowOdd">
            <BodyColumn id="index" class="rptBodyCol">13</BodyColumn>
            <BodyColumn id="startdatetime" class="rptBodyCol">2014/03/06 14:59:18</BodyColumn>
            <BodyColumn id="drivingelapse" class="rptBodyCol">0:01:50</BodyColumn>
            <BodyColumn id="odomdelta" class="rptBodyCol">0.7</BodyColumn>
            <BodyColumn id="stopdatetime" class="rptBodyCol">2014/03/06 15:01:08</BodyColumn>
            <BodyColumn id="stopgeopoint" class="rptBodyCol">21.1037/79.0682</BodyColumn>
            <BodyColumn id="stopaddress" class="rptBodyCol" />
            <BodyColumn id="idleelapse" class="rptBodyCol">0:00:46</BodyColumn>
            <BodyColumn id="stopelapse" class="rptBodyCol">0:00:46</BodyColumn>
        </BodyRow>
        <BodyRow class="rptBodyRowEven">
            <BodyColumn id="index" class="rptBodyCol">14</BodyColumn>
            <BodyColumn id="startdatetime" class="rptBodyCol">2014/03/06 15:01:54</BodyColumn>
            <BodyColumn id="drivingelapse" class="rptBodyCol">0:02:03</BodyColumn>
            <BodyColumn id="odomdelta" class="rptBodyCol">1.2</BodyColumn>
            <BodyColumn id="stopdatetime" class="rptBodyCol">2014/03/06 15:03:57</BodyColumn>
            <BodyColumn id="stopgeopoint" class="rptBodyCol">21.1078/79.0794</BodyColumn>
            <BodyColumn id="stopaddress" class="rptBodyCol">KIPL Zone</BodyColumn>
            <BodyColumn id="idleelapse" class="rptBodyCol">0:00:10</BodyColumn>
            <BodyColumn id="stopelapse" class="rptBodyCol">0:00:10</BodyColumn>
        </BodyRow>
        <BodyRow class="rptBodyRowOdd">
            <BodyColumn id="index" class="rptBodyCol">15</BodyColumn>
            <BodyColumn id="startdatetime" class="rptBodyCol">2014/03/06 15:04:07</BodyColumn>
            <BodyColumn id="drivingelapse" class="rptBodyCol">0:01:23</BodyColumn>
            <BodyColumn id="odomdelta" class="rptBodyCol">0.4</BodyColumn>
            <BodyColumn id="stopdatetime" class="rptBodyCol">2014/03/06 15:05:30</BodyColumn>
            <BodyColumn id="stopgeopoint" class="rptBodyCol">21.1070/79.0825</BodyColumn>
            <BodyColumn id="stopaddress" class="rptBodyCol">KIPL Zone</BodyColumn>
            <BodyColumn id="idleelapse" class="rptBodyCol">4:43:07</BodyColumn>
            <BodyColumn id="stopelapse" class="rptBodyCol">4:43:07</BodyColumn>
        </BodyRow>
        <BodyRow class="rptBodyRowEven">
            <BodyColumn id="index" class="rptBodyCol">16</BodyColumn>
            <BodyColumn id="startdatetime" class="rptBodyCol">2014/03/06 19:48:37</BodyColumn>
            <BodyColumn id="drivingelapse" class="rptBodyCol">0:03:37</BodyColumn>
            <BodyColumn id="odomdelta" class="rptBodyCol" />
            <BodyColumn id="stopdatetime" class="rptBodyCol">2014/03/06 19:52:14</BodyColumn>
            <BodyColumn id="stopgeopoint" class="rptBodyCol">21.0985/79.0669</BodyColumn>
            <BodyColumn id="stopaddress" class="rptBodyCol" />
            <BodyColumn id="idleelapse" class="rptBodyCol">0:00:05</BodyColumn>
            <BodyColumn id="stopelapse" class="rptBodyCol">0:00:05</BodyColumn>
        </BodyRow>
        <BodyRow class="rptBodyRowOdd">
            <BodyColumn id="index" class="rptBodyCol">17</BodyColumn>
            <BodyColumn id="startdatetime" class="rptBodyCol">2014/03/06 19:52:19</BodyColumn>
            <BodyColumn id="drivingelapse" class="rptBodyCol">0:00:54</BodyColumn>
            <BodyColumn id="odomdelta" class="rptBodyCol">0.4</BodyColumn>
            <BodyColumn id="stopdatetime" class="rptBodyCol">2014/03/06 19:53:13</BodyColumn>
            <BodyColumn id="stopgeopoint" class="rptBodyCol">21.0948/79.0661</BodyColumn>
            <BodyColumn id="stopaddress" class="rptBodyCol" />
            <BodyColumn id="idleelapse" class="rptBodyCol">0:03:19</BodyColumn>
            <BodyColumn id="stopelapse" class="rptBodyCol">0:03:19</BodyColumn>
        </BodyRow>
        <BodyRow class="rptBodyRowEven">
            <BodyColumn id="index" class="rptBodyCol">18</BodyColumn>
            <BodyColumn id="startdatetime" class="rptBodyCol">2014/03/06 19:56:32</BodyColumn>
            <BodyColumn id="drivingelapse" class="rptBodyCol">0:01:20</BodyColumn>
            <BodyColumn id="odomdelta" class="rptBodyCol" />
            <BodyColumn id="stopdatetime" class="rptBodyCol">2014/03/06 19:57:52</BodyColumn>
            <BodyColumn id="stopgeopoint" class="rptBodyCol">21.0887/79.0651</BodyColumn>
            <BodyColumn id="stopaddress" class="rptBodyCol">Home Zone</BodyColumn>
            <BodyColumn id="idleelapse" class="rptBodyCol" />
            <BodyColumn id="stopelapse" class="rptBodyCol" />
        </BodyRow>
        <BodyRow class="rptTotalRow">
            <BodyColumn id="index" class="rptTotalCol">1</BodyColumn>
            <BodyColumn id="startdatetime" class="rptTotalCol" />
            <BodyColumn id="drivingelapse" class="rptTotalCol">0:34:20</BodyColumn>
            <BodyColumn id="odomdelta" class="rptTotalCol">8.1</BodyColumn>
            <BodyColumn id="stopdatetime" class="rptTotalCol" />
            <BodyColumn id="stopgeopoint" class="rptTotalCol" />
            <BodyColumn id="stopaddress" class="rptTotalCol" />
            <BodyColumn id="idleelapse" class="rptTotalCol">9:41:54</BodyColumn>
            <BodyColumn id="stopelapse" class="rptTotalCol">9:41:54</BodyColumn>
        </BodyRow>
    </ReportBody>
    <Partial>false</Partial>
</Report>

我需要解析这个xml,所以你能告诉我你是怎么做到的吗?

a9wyjsp7

a9wyjsp71#

请使用此代码

package other;

import java.util.Vector;

import data.GlobelData;

public class Xmlparser {

    public static String getTag(String XML,String TagName)
    {

        try {

            String ans = "error";
            String front1 = "<" + TagName;
            String back1 = "=";
            String[] sssnew = split(XML, front1);
            if (sssnew.length > 1) {
                String[] sss1new = split(sssnew[1], back1);
                if ((sss1new[0]).compareTo(" i:nil") < 1) {
                    ans = "";
                    return ans;
                }
            }
            String front = "<" + TagName + ">";
            String back = "</" + TagName + ">";
            String[] sss = {};
            sss = split(XML, front);
            if (sssnew.length > 1) {
                String[] sss1 = {};
                sss1 = split(sss[1], back);
                ans = sss1[0];
            }

            return ans;
        } catch (Exception e) {
            return "";
        }
    }

    private static String[] split(String inString, String delimeter)
    {
        String[] retAr = new String[0];
        try {
            Vector vec = new Vector();
            int indexA = 0;
            int indexB = inString.indexOf(delimeter);

            while (indexB != -1) {
                if (indexB > indexA)
                    vec.addElement(new String(inString
                            .substring(indexA, indexB)));
                indexA = indexB + delimeter.length();
                indexB = inString.indexOf(delimeter, indexA);
            }
            vec.addElement(new String(inString.substring(indexA, inString
                    .length())));
            retAr = new String[vec.size()];
            int intIteration = vec.size();
            for (int i = 0; i < intIteration; i++) {
                retAr[i] = vec.elementAt(i).toString();
            }
        } catch (Exception e)
        {

        }
        return retAr;
    }
}
3lxsmp7m

3lxsmp7m2#

用于sax或dom解析器。
例如(回答您的意见):

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setIgnoringComments(true);
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(xml source);

NodeList bodyRowNodes = doc.getElementsByTagName("BodyRow");
for (int j = 0; j < bodyRowNodes.getLength(); j++) {
    Node bodyRowNode = bodyRowNodes.item(j);

    if ("rptBodyRowOdd".equals(bodyRowNode.getNodeName())) {
        NodeList bodyColumns = bodyRowNode.getChildNodes();

        for (int i = 0; i < bodyColumns.getLength(); i++) {
            Node bodyColumn = bodyColumns.item(i);

            String bodyColumnIdAttribute = bodyColumn.getAttributes().getNamedItem("id").getNodeValue();
        }
    }
}
djmepvbi

djmepvbi3#

使用此演示示例解析xml:link

相关问题