从资源加载和解析xml的问题

时间:2011-02-01 15:14:06

标签: java android xml resources local

我编写了一个解析器,它解析来自HttpURLConnection的xml文件。这很好用。

问题:我需要重写这个,以便从本地资源而不是从互联网加载xml文件,但我不能让它工作......只是为了让你知道原始网络解析器的外观: / p>

InputStream in=null;

URLConnection connection = url.openConnection(); 
HttpURLConnection httpConnection = (HttpURLConnection)connection; 
int responseCode = httpConnection.getResponseCode(); 

if (responseCode == HttpURLConnection.HTTP_OK) { 


   in = httpConnection.getInputStream(); 
   DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();  
   DocumentBuilder db = dbf.newDocumentBuilder();

   Document dom = db.parse(in);     
   Element docEle = dom.getDocumentElement();
   NodeList nl = docEle.getChildNodes();
   for (int i = 0; i < nl.getLength(); i++) {
    Node node = nl.item(i);

    //START PARSING......

现在是我用来尝试从位于resources文件夹中的xml / myfile.xml的本地资源解析的代码:

InputStream in=null;
in = mParentActivity.getResources().openRawResource(R.xml.myfile);

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();

Document dom = builder.parse(in); // THIS IS WHERE I GET AN SAXParseException

Element root = dom.getDocumentElement();
NodeList nl = root.getChildNodes();

for (int i = 0; i < nl.getLength(); i++) {
    Node node = nl.item(i);

 //START PARSING......

本地xml文件和网络文件完全相同......如果有人会看一眼:http://agens.no/Eniro/Android/SEWheel/Category2.plist.xml

继承人堆栈跟踪: 02-01 16:08:45.546:WARN / System.err(19703):org.xml.sax.SAXParseException:name expected(position:START_TAG @ 1:309 in java.io.InputStreamReader@47668728)

为所有人提供帮助:)

1 个答案:

答案 0 :(得分:1)

找到答案..当文件是我的res / xml文件夹时,输入流显示了很多无效字符。当我把它放在res / raw文件夹中时它工作正常。