Parsing XML from website to an Android device

后端 未结 5 1379
猫巷女王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:15

    I always use the w3c dom classes. I have a static helper method that I use to parse the xml data as a string and returns to me a Document object. Where you get the xml data can vary (web, file, etc) but eventually you load it as a string.

    something like this...

        Document document = null;
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder;
    
        try
        {
            builder = factory.newDocumentBuilder();
            InputSource is = new InputSource(new StringReader(data));
            document = builder.parse(is);
        }
        catch (SAXException e) { }
        catch (IOException e) { }
        catch (ParserConfigurationException e) { }
    
    0 讨论(0)
  • 2021-02-10 16:19

    Here's an example:

            try {
                URL url = new URL(/*your xml url*/);
                URLConnection conn = url.openConnection();
    
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                DocumentBuilder builder = factory.newDocumentBuilder();
                Document doc = builder.parse(conn.getInputStream());
    
                NodeList nodes = doc.getElementsByTagName(/*tag from xml file*/);
                for (int i = 0; i < nodes.getLength(); i++) {
                    Element element = (Element) nodes.item(i);
                    NodeList title = element.getElementsByTagName(/*item within the tag*/);
                    Element line = (Element) title.item(0);
                    phoneNumberList.add(line.getTextContent());
                }
            }
            catch (Exception e) {
                e.printStackTrace();
            }
    

    In my example, my XML file looks a little like:

    <numbers>
       <phone>
          <string name = "phonenumber1">555-555-5555</string>
       </phone>
       <phone>
          <string name = "phonenumber2">555-555-5555</string>
       </phone>
    </numbers>
    

    and I would replace /*tag from xml file*/ with "phone" and /*item within the tag*/ with "string".

    0 讨论(0)
  • 2021-02-10 16:20

    There are different types of parsing mechanisms available, one is SAX Here is SAX parsing example, second is DOM parsing Here is DOM Parsing example.. From your question it is not clear what you want, but these may be good starting points.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-10 16:24

    I would use the DOM parser, it is not as efficient as SAX, if the XML file is not too large, as it is easier in that case.

    I have made just one android App, that involved XML parsing. XML received from a SOAP web service. I used XmlPullParser. The implementation from Xml.newPullParser() had a bug where calls to nextText() did not always advance to the END_TAG as the documentation promised. There is a work around for this.

    0 讨论(0)
提交回复
热议问题