2011-02-01 60 views
1

我已经写了一个解析器来解析来自HttpURLConnection的xml文件。这工作正常。从资源加载和解析XML问题

问题:我需要重写这个,以便xml文件是从本地资源加载而不是从互联网上加载的,但是我无法得到这个工作...只是想给你一个想法原始web分析器的外观:

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...... 

现在继承人我使用的尝试从资源文件夹放置在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:预期名称(位置:START_TAG @ 1:309在java中。 [email protected]

Preciate所有侑帮助:)

+0

这表明XML无效。你是否试过将资源读入一个字符串并打印出来,看看它在读什么? – 2011-02-01 15:18:47

回答

1

找到了答案。当文件被我的RE/XML文件夹,InputStream中表现出了极大的无效字符。当我把它放在res/raw文件夹中时,它工作正常。