无法在android中使用DOM解析XML

时间:2011-11-25 18:40:19

标签: android xml dom

我在从Android解析Android中的XML时遇到了一些麻烦。我不知道XML解析是否是问题,或者只是当我试图在屏幕上显示它时,因为

setListAdapter(new ArrayAdapter<String>(ANDROIDXMLActivity.this, android.R.layout.simple_list_item_1, stopNumbers)); 

不会显示任何内容。下面的代码在java程序中完美运行。

网址:http://maps.travelsouthyorkshire.com/iGNMSearchService.asmx/FindObjectsWithinExtent?xMin=435360&yMin=387260&xMax=435960&yMax=387860&zoomLevel=0

    try {
        ArrayList<Double> xCords = new ArrayList<Double>();
        ArrayList<Double> yCords = new ArrayList<Double>();
        ArrayList<String> stopNumbers = new ArrayList<String>();
        ArrayList<String> bussLocations = new ArrayList<String>();
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
                .newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
        Document doc = docBuilder
                .parse(new URL(
                        "http://maps.travelsouthyorkshire.com/iGNMSearchService.asmx/FindObjectsWithinExtent?xMin=435360&yMin=387260&xMax=435960&yMax=387860&zoomLevel=0")
                        .openStream());

        // normalize text representation
        doc.getDocumentElement().normalize();
        NodeList listOfObjects = doc.getElementsByTagName("iGNMObject");
        for (int s = 0; s < listOfObjects.getLength(); s++) {
            Node firstPersonNode = listOfObjects.item(s);

            if (firstPersonNode.getNodeType() == Node.ELEMENT_NODE) {

                Element firstPersonElement = (Element) firstPersonNode;
                NodeList stopNumList = firstPersonElement
                        .getElementsByTagName("StopNumber");
                Element ageElement = (Element) stopNumList.item(0);
                if (ageElement != null) {
                    NodeList textAgeList = ageElement.getChildNodes();

                    String stop = ((Node) textAgeList.item(0))
                            .getNodeValue().trim();
                    stopNumbers.add(stop);

                    // ------
                    // -------
                    NodeList xPosList = firstPersonElement
                            .getElementsByTagName("XPosition");
                    Element firstNameElement = (Element) xPosList.item(0);
                    NodeList textFNList = firstNameElement.getChildNodes();

                    String temp2 = ((Node) textFNList.item(0))
                            .getNodeValue().trim();
                    double x = Double.parseDouble(temp2);
                    xCords.add(x);

                    // -------
                    NodeList yPosList = firstPersonElement
                            .getElementsByTagName("YPosition");
                    Element lastNameElement = (Element) yPosList.item(0);
                    NodeList textLNList = lastNameElement.getChildNodes();
                    String temp3 = ((Node) textLNList.item(0))
                            .getNodeValue().trim();
                    double y = Double.parseDouble(temp3);
                    yCords.add(y);

                    // ----

                    NodeList stopAkaList = firstPersonElement
                            .getElementsByTagName("StopAka");
                    Element stopAka = (Element) stopAkaList.item(0);
                    NodeList textStopAKAList = stopAka.getChildNodes();
                    String plats = ((Node) textStopAKAList.item(0))
                            .getNodeValue().trim();
                    bussLocations.add(plats);
                } else {

                }

            }// end of if clause
        }// end of for loop with s var

        setListAdapter(new ArrayAdapter<String>(ANDROIDXMLActivity.this,
                android.R.layout.simple_list_item_1, stopNumbers));

    } catch (Exception e) {

    }

}

}     }

1 个答案:

答案 0 :(得分:0)

解决了它。我忘了在清单中添加互联网权限。