2012-04-15 10 views
2

私はXMLパーサーでAndroidアプリケーションを作成しています。 私は動作していたパーサを持っていますが、実行すると何もしません。 これは私のクラスである:Android XML Parser is not working

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

いつどこで作業するのに使用しますか? –

答えて

1

例外は何ですか?

何かが起こる可能性があります。 WebサービスからURLを取得しようとしている可能性があります。 AsyncTaskでコードを実行する必要があるかもしれません。 疑わしい行とトグルブレークポイントを右クリックし、パッケージエクスプローラでクラスを右クリックし、「デバッグ」、「デバッグ構成」に進みます。正しいプロジェクトをデバッグしていることを確認したら、[デバッグ]ボタンをクリックします。

デバッグモードにようこそ、どの行が例外を引き起こしているかを確認してみてください。