2012-04-20 107 views
0

我想从xml文件中获取一些信息我尝试过不同的链接,但它工作正常,但是当我通过这个url =“http://www.w3schools.com/xml/simple.xml”它捕获的SAXException 这是我的代码使用xml解析

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) 谁能帮助我如何才能避免这个问题?

回答

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