2012-04-20 123 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");