简体   繁体   中英

Can't parse XML with DOM in android

I am having some trouble parsing XML in Android from an URL. I don't know if it's the XML parsing that's the problem or only when I am trying to show it on the screen because

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

wont show anything. The code below works perfectly in a java program.

URL: 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) {

    }

}

} }

Solved it. I had forgot to add internet permission in the manifest.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM