使用xml进行解析

时间:2012-04-20 16:26:15

标签: android xml-parsing

我试图从xml文件中获取一些信息,我尝试过不同的链接,但是当我通过这个url =“http://www.w3schools.com/xml/simple.xml”它会捕获SAXexception 这是我的代码

public class XMLfunctions {

    public Document getDomElement(String xml){
        Document doc = null;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {

            DocumentBuilder db = dbf.newDocumentBuilder();

            InputSource is = new InputSource();
                is.setCharacterStream(new StringReader(xml));
                doc = db.parse(is);

            } catch (ParserConfigurationException e) {
                Log.e("Error: ", e.getMessage());
                return null;
            } catch (SAXException e) {
                Log.e("Error: ", e.getMessage());
                return null;
            } catch (IOException e) {
                Log.e("Error: ", e.getMessage());
                return null;
            }
                // return DOM
            return doc;
    }

它捕获(SAXException e) 任何人都可以帮助我如何避免这个问题??

1 个答案:

答案 0 :(得分:0)

使用此

 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 DocumentBuilder db = dbf.newDocumentBuilder();
 Document doc = db.parse(new InputSource(new StringReader(response)));
// normalize the document
doc.getDocumentElement().normalize();
 // get the root node
NodeList nodeList = doc.getElementsByTagName("your tag name");
相关问题