1

我现在完全被这个 DOM XML Parser 难住了。我得到了两个标签的错误信息,我无法弄清楚为什么这两个标签(名称和 cmt)保持相同的信息而不是正确的信息。

我使用的 XML 如下:

<?xml version="1.0" encoding="UTF-8"?>
<gpx xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.topografix.com/GPX/1/1" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd" version="1.1" creator="http://ridewithgps.com/">
  <metadata>
    <name>Home Test</name>
    <link href="http://ridewithgps.com/routes/1714475">
      <text>Home Test</text>
    </link>
    <time>2012-09-13T16:08:10Z</time>
  </metadata>
  <rte>
    <name>Home Test</name>
    <rtept lat="39.41333" lon="-77.4624">
      <name>Right</name>
      <cmt>Turn right onto Grouse Dr</cmt>
    </rtept>
    <rtept lat="39.41288" lon="-77.46297000000004">
      <name>Right</name>
      <cmt>Turn right onto Hunting Horn Ln</cmt>
    </rtept>
    <rtept lat="39.41431" lon="-77.46625">
      <name>Right</name>
      <cmt>Turn right onto Partridge Way</cmt>
    </rtept>
    <rtept lat="39.41431" lon="-77.46625">
      <name>Left</name>
      <cmt>Turn left onto Hunting Horn Ln</cmt>
    </rtept>
    <rtept lat="39.41288" lon="-77.46297000000004">
      <name>Left</name>
      <cmt>Turn left onto Grouse Dr</cmt>
    </rtept>
    <rtept lat="39.41333" lon="-77.4624">
      <name>Right</name>
      <cmt>Turn right onto Grouse Ct</cmt>
    </rtept>
  </rte>
</gpx>

我整理的 XML Parser 如下:

        public void parseXMLFile(String fullpath) {
        String metadata_name = null;
        String metadata_link_href = null;
        String metadata_link_name = null;
        String metadata_time = null;
        String rte_name = null;
        Double point_lat = null;
        Double point_lon = null;
        Double last_lat = null;
        Double last_lon = null;
        String point_name = null;
        String point_cmt = null;
        Float distance_to = null;

        // File f = new File(fullpath);
        String fileContents = null;
        try {
            fileContents = readFileAsString(fullpath);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            Document doc = (Document) loadXMLFromString(fileContents);
            doc.getDocumentElement().normalize();
            Element root = doc.getDocumentElement();
            Log.d("XML", "ROOTNODE:" + root.getNodeName());

            NodeList nodeList = doc.getElementsByTagName("metadata");

            Node nodee = nodeList.item(0);
            Log.d("XML", "NODELISTLENGTH1:" + 
                    nodee.getChildNodes().getLength());

            for (int i = 0; i < nodee.getChildNodes().getLength(); i++) {

                Node node = nodee.getChildNodes().item(i);
                if (node.getNodeName().equalsIgnoreCase("name")) {
                    metadata_name = node.getTextContent();

                } else if (node.getNodeName().equalsIgnoreCase("link")) {
                    NamedNodeMap attributes = node.getAttributes();
                    if (attributes.getNamedItem("href") != null) {
                        String href = attributes.getNamedItem("href")
                                .getNodeValue();
                        metadata_link_href = href;

                    }
                    metadata_link_name = node.getTextContent();
                    // Log.i("XML", "link node:" + node.getTextContent());
                } else if (node.getNodeName().equalsIgnoreCase("time")) {

                    metadata_time = node.getTextContent();
                }

            }

            // DO THE RTE NODES AFTER THIS
            NodeList nodeList2 = doc.getElementsByTagName("rte");

            Node nodee2 = nodeList2.item(0);
            Log.d("XML", "NODELISTLENGTH2:"
                    + nodee2.getChildNodes().getLength());

            for (int i = 0; i < nodee2.getChildNodes().getLength(); i++) {

                Node node = nodee2.getChildNodes().item(i);

                if (node.getNodeName().equalsIgnoreCase("name")) {

                    rte_name = node.getTextContent();

                } else if (node.getNodeName().equalsIgnoreCase("rtept")) {

                    NamedNodeMap attributes = node.getAttributes();
                    if (attributes.getNamedItem("lat") != null) {
                        // STORE THE LAST LAT FOR MEASURING DISTANCE BETWEEN
                        if (point_lat != null) {
                            last_lat = point_lat;
                        }
                        point_lat = Double.parseDouble(attributes.getNamedItem(
                                "lat").getNodeValue());

                    }
                    if (attributes.getNamedItem("lon") != null) {
                        // STORE THE LAST LON FOR MEASURING DISTANCE BETWEEN
                        if (point_lon != null) {
                            last_lon = point_lon;
                        }
                        point_lon = Double.parseDouble(attributes.getNamedItem(
                                "lon").getNodeValue());

                    }
                    NodeList rteptList = doc.getElementsByTagName("rtept");
//                  NodeList rteptList = node.getChildNodes();


                    Node nodee3 = rteptList.item(0);
                    Log.d("XML", "NODELISTLENGTH3:"
                            + nodee3.getChildNodes().getLength());
                    for (int i1 = 0; i1 < nodee3.getChildNodes().getLength(); i1++) {
                        Node rteptNode = nodee3.getChildNodes().item(i1);
                        if (rteptNode.getNodeName().equalsIgnoreCase("name")) {

                            point_name = rteptNode.getFirstChild().getNodeValue();

                        } else if (rteptNode.getNodeName().equalsIgnoreCase("cmt")) {

                            point_cmt = rteptNode.getFirstChild().getNodeValue();

                        }

                    }
                    // DO THE DATABASE STORAGE HERE
                    Log.i("XML", "name node:" + metadata_name);
                    Log.i("XML", "link node:" + metadata_link_name);
                    Log.i("XML", "link attribute [href]:" + metadata_link_href);
                    Log.i("XML", "time node:" + metadata_time);
                    Log.i("XML", "rte_name:" + rte_name);
                    Log.i("XML", "point_lat:" + point_lat.toString());
                    Log.i("XML", "point_lon:" + point_lon.toString());
                    Log.i("XML", "point_name:" + point_name);
                    Log.i("XML", "point_cmt:" + point_cmt);
                }

            }
        } catch (Exception e) {
            String estr = e.getStackTrace().toString();
            Log.d("XML", "XML Exception: " + e + ":" + estr);
        }
    }

`

我看到输出是错误的......

09-15 03:37:20.963: D/XML(14951): NODELISTLENGTH3:5
09-15 03:37:20.963: I/XML(14951): name node:Home Test
09-15 03:37:20.963: I/XML(14951): link node:      Home Test    
09-15 03:37:20.963: I/XML(14951): link attribute [href]:http://ridewithgps.com/routes/1714475
09-15 03:37:20.963: I/XML(14951): time node:2012-09-13T16:08:10Z
09-15 03:37:20.963: I/XML(14951): rte_name:Home Test
09-15 03:37:20.963: I/XML(14951): point_lat:39.41288
09-15 03:37:20.963: I/XML(14951): point_lon:-77.46297000000004
09-15 03:37:20.963: I/XML(14951): point_name:Right
09-15 03:37:20.963: I/XML(14951): point_cmt:Turn right onto Grouse Dr
09-15 03:37:20.973: D/XML(14951): NODELISTLENGTH3:5
09-15 03:37:20.973: I/XML(14951): name node:Home Test
09-15 03:37:20.973: I/XML(14951): link node:      Home Test    
09-15 03:37:20.973: I/XML(14951): link attribute [href]:http://ridewithgps.com/routes/1714475
09-15 03:37:20.973: I/XML(14951): time node:2012-09-13T16:08:10Z
09-15 03:37:20.973: I/XML(14951): rte_name:Home Test
09-15 03:37:20.973: I/XML(14951): point_lat:39.41333
09-15 03:37:20.973: I/XML(14951): point_lon:-77.4624
09-15 03:37:20.973: I/XML(14951): point_name:Right
09-15 03:37:20.973: I/XML(14951): point_cmt:Turn right onto Grouse Dr

为什么 point_name 总是“向右”而 point_cmt 总是“向右转到 Grouse Dr”?我该如何解决?

提前致谢。

4

3 回答 3

0

尝试在您的代码中替换以下代码段。

 Node nodee3 = rteptList.item(0);
                    Log.d("XML", "NODELISTLENGTH3:"
                            + nodee3.getChildNodes().getLength());
                    for (int i1 = 0; i1 < nodee3.getChildNodes().getLength(); i1++) {
                        Node rteptNode = nodee3.getChildNodes().item(i1);
                        if (rteptNode.getNodeName().equalsIgnoreCase("name")) {

                            point_name = rteptNodenode.getTextContent();

                        } else if (rteptNode.getNodeName().equalsIgnoreCase("cmt")) {

                            point_cmt = rteptNodenode.getTextContent();

                        }

                    }
于 2012-09-15T08:13:59.117 回答
0

在你的代码中我看到

NodeList rteptList = doc.getElementsByTagName("rtept");
// NodeList rteptList = node.getChildNodes();

Node nodee3 = rteptList.item(0);

您当前的代码仅获取第一个“rtept”节点并获取“名称”和“cmt”,因此仅使用第一个节点的值。

注释掉的行在正确的轨道上。您需要从当前节点获取子节点并在其中找到“rtept”节点。

于 2012-09-16T13:23:38.247 回答
0

我决定使用 JOOX 来完成它,因为代码更容易理解......

public void parseXMLFile(String fullpath) {
    String metadata_name = null;
    String metadata_link_href = null;
    String metadata_link_name = null;
    String metadata_time = null;
    String point_lat = null;
    String point_lon = null;
    String last_lat = null;
    String last_lon = null;
    String point_name = null;
    String point_cmt = null;
    String fileContents = null;

    try {
        fileContents = readFileAsString(fullpath);

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        Document doc = (Document) loadXMLFromString(fileContents);

        for (Match metadata : $(doc).find("metadata").each()) {
            Match c = metadata.children();
            for (int i = 0; i < c.size(); i++) {
                metadata_name = c.find("name").text();
                metadata_link_href = c.find("link").attr("href").toString();
                metadata_link_name = c.find("link").find("text").text();
                metadata_time = c.find("time").text();
            }
        }

        for (Match rte2 : $(doc).xpath("//rte").andSelf().each()) {
            rte2.children();
            for(Match rtetag : rte2.children().each()) {
                if(rtetag.tag().equalsIgnoreCase("name")) {
                    rtetag.text();
                }
                if(rtetag.tag().equalsIgnoreCase("rtept")) {
                    for(Match rtepttag : rtetag.children().andSelf().each()) {
                        if(rtepttag.tag().equals("name")) {
                            point_name = rtepttag.text();
                        }
                        if(rtepttag.tag().equals("cmt")) {
                            point_cmt = rtepttag.text();
                        }
                        if(rtepttag.tag().equals("rtept")) {
                            if(last_lon == null) {
                                last_lon = point_lon;
                            }
                            if(last_lat == null) {
                                last_lat = point_lat;
                            }
                            point_lat = rtepttag.attr("lat");
                            point_lon = rtepttag.attr("lon");

                            //done parsing? here's some log stuff to check
                             Log.d("XML", "META_NAME:" + metadata_name);
                             Log.d("XML", "META_LINK_HREF:" + metadata_link_href);
                             Log.d("XML", "META_LINK_NAME:" + metadata_link_name);
                             Log.d("XML", "META_TIME:" + metadata_time);
                             Log.d("XML", "POINT_NAME:" + point_name);
                             Log.d("XML","POINT_CMT:"+point_cmt);
                             Log.d("XML", "POINT_LAT:" + point_lat);
                             Log.d("XML", "POINT_LON:" + point_lon);

                        }
                    }
                }
            }
        }


    } catch (Exception e) {
        String estr = e.getStackTrace().toString();
        Log.d("XML", "XML Exception: " + e + ":" + estr);
    }
}
于 2012-09-19T01:08:33.740 回答