Parsing XML from website to an Android device

后端 未结 5 1523
猫巷女王i
猫巷女王i 2021-02-10 15:56

I am starting an Android application that will parse XML from the web. I\'ve created a few Android apps but they\'ve never involved parsing XML and I was wondering if anyone had

5条回答
  •  没有蜡笔的小新
    2021-02-10 16:21

    There are three types of parsing I know: DOM, SAX and XMLPullParsing.

    In my example here you need the URL and the parent node of the XML element.

    try {
        URL url = new URL("http://www.something.com/something.xml");
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new InputSource(url.openStream()));
        doc.getDocumentElement().normalize();
    
        NodeList nodeList1 = doc.getElementsByTagName("parent node here");
        for (int i = 0; i < nodeList1.getLength(); i++) {
            Node node = nodeList1.item(i);
        }
    } catch(Exception e) {
    
    }
    

    Also try this.

提交回复
热议问题