2012-04-15 77 views
2

我正在用XML解析器編寫一個android應用程序。 我有一個解析器,曾經工作,但是當我運行它時,它沒有做任何事情。 這是我的課:Android XML解析器不工作

import java.net.URL; 

import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 

import org.w3c.dom.Document; 
import org.w3c.dom.Element; 
import org.w3c.dom.Node; 
import org.w3c.dom.NodeList; 
import org.xml.sax.InputSource; 

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.LinearLayout; 
import android.widget.TextView; 
import android.widget.Toast; 

public class XMLParsingUsingDomeActivity extends Activity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 


LinearLayout layout = new LinearLayout(this); 
layout.setOrientation(1); 


TextView ID[]; 
TextView vraag[]; 
TextView category[]; 
TextView a1[]; 
TextView p1[]; 
TextView a2[]; 
TextView p2[]; 
TextView a3[]; 
TextView p3[]; 

try { 

URL url = new URL(
"http://128.140.217.126/vragen.xml"); 
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
DocumentBuilder dbu= dbf.newDocumentBuilder(); 
Document doc = dbu.parse(new InputSource(url.openStream())); 
doc.getDocumentElement().normalize(); 

NodeList nodeList = doc.getElementsByTagName("item"); 

ID = new TextView[nodeList.getLength()]; 
vraag = new TextView[nodeList.getLength()]; 
category = new TextView[nodeList.getLength()]; 
a1 = new TextView[nodeList.getLength()]; 
p1 = new TextView[nodeList.getLength()]; 
a2 = new TextView[nodeList.getLength()]; 
p2 = new TextView[nodeList.getLength()]; 
a3 = new TextView[nodeList.getLength()]; 
p3 = new TextView[nodeList.getLength()]; 
for (int i = 0; i < nodeList.getLength(); i++) { 

Node node = nodeList.item(i); 

ID[i] = new TextView(this); 
vraag[i] = new TextView(this); 
category[i] = new TextView(this); 
a1[i] = new TextView(this); 
p1[i] = new TextView(this); 
a2[i] = new TextView(this); 
p2[i] = new TextView(this); 
a3[i] = new TextView(this); 
p3[i] = new TextView(this); 


Element fstElmnt = (Element) node; 
NodeList nameList = fstElmnt.getElementsByTagName("ID"); 
Element nameElement = (Element) nameList.item(0); 
nameList = nameElement.getChildNodes(); 
ID[i].setText(((Node) nameList.item(0)).getNodeValue()); 


NodeList vraagList = fstElmnt.getElementsByTagName("vraag"); 
Element vraagElement = (Element) vraagList.item(0); 
vraagList = vraagElement.getChildNodes(); 
vraag[i].setText(((Node) vraagList.item(0)).getNodeValue()); 

NodeList a1List = fstElmnt.getElementsByTagName("a1"); 
Element a1Element = (Element) a1List.item(0); 
a1List = a1Element.getChildNodes(); 
a1[i].setText(((Node) a1List.item(0)).getNodeValue()); 

NodeList p1List = fstElmnt.getElementsByTagName("p1"); 
Element p1Element = (Element) p1List.item(0); 
p1List = p1Element.getChildNodes(); 
p1[i].setText(((Node) p1List.item(0)).getNodeValue()); 

NodeList a2List = fstElmnt.getElementsByTagName("a2"); 
Element a2Element = (Element) a2List.item(0); 
a2List = a2Element.getChildNodes(); 
a2[i].setText(((Node) a2List.item(0)).getNodeValue()); 

NodeList p2List = fstElmnt.getElementsByTagName("p2"); 
Element p2Element = (Element) p2List.item(0); 
p2List = p2Element.getChildNodes(); 
p2[i].setText(((Node) p2List.item(0)).getNodeValue()); 

NodeList a3List = fstElmnt.getElementsByTagName("a3"); 
Element a3Element = (Element) a3List.item(0); 
a3List = a3Element.getChildNodes(); 
a3[i].setText(((Node) a3List.item(0)).getNodeValue()); 

NodeList p3List = fstElmnt.getElementsByTagName("p3"); 
Element p3Element = (Element) p3List.item(0); 
p3List = p3Element.getChildNodes(); 
p3[i].setText(((Node) p3List.item(0)).getNodeValue()); 



layout.addView(category[i]); 
Toast.makeText(this, 
     "ID: " + i + "\n" + 
     "Vraag: " + ((Node) vraagList.item(0)).getNodeValue() + "\n" + 
     "A1: " + ((Node) a1List.item(0)).getNodeValue() + "\n" + 
     "P2: " + ((Node) p1List.item(0)).getNodeValue() + "\n" + 
     "A2: " + ((Node) a2List.item(0)).getNodeValue() + "\n" + 
     "P2: " + ((Node) p2List.item(0)).getNodeValue() + "\n" + 
     "A3: " + ((Node) a3List.item(0)).getNodeValue() + "\n" + 
     "P3: " + ((Node) p3List.item(0)).getNodeValue(), 
     Toast.LENGTH_LONG).show(); 

} 
} catch (Exception e) { 
System.out.println("XML Pasing Excpetion = " + e); 
} 

/** Set the layout view to display */ 
setContentView(layout); 

} 
} 

我的清單:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="your.pace.namace" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk android:minSdkVersion="10" /> 
    <uses-permission android:name="android.permission.INTERNET"></uses-permission> 

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <activity 
      android:name=".XMLParsingUsingDomeActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

而且logcat的輸出是毫無價值的。 我沒有改變代碼,但它只是不工作了。

+0

用於何時何地工作? – 2012-04-15 13:40:30

回答

1

什麼是例外?

幾件事情可能會發生。這可能是您嘗試從Web服務獲取URL的原因。可能是因爲你必須在AsyncTask中執行你的代碼。 我建議你右鍵單擊你懷疑的一行並切換斷點,然後在Package Explorer中右鍵單擊它並轉到「Debug As」,然後選擇「Debug Configuration」。檢查您正在調試正確的項目後,單擊調試按鈕。

歡迎使用調試模式,試試看哪條線路導致異常。