0

我正在尝试从 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)任何人都可以帮助我如何避免这个问题?

4

1 回答 1

0

use this

 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");
于 2012-04-20T16:36:48.267 回答