2011-02-01 58 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文件夾中時,它工作正常。