Parsing XML from website to an Android device

后端 未结 5 1378
猫巷女王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) { }
    

提交回复
热议问题