【问题标题】:Parsing using the xml使用xml解析
【发布时间】:2012-04-20 16:26:15
【问题描述】:

我正在尝试从 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) 谁能帮助我如何避免这个问题?

【问题讨论】:

    标签: android xml-parsing


    【解决方案1】:

    使用这个

     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");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-27
      • 1970-01-01
      • 2015-10-09
      • 2012-01-20
      • 1970-01-01
      • 2011-04-30
      相关资源
      最近更新 更多