Android XML Parser不起作用

时间:2012-04-15 12:59:10

标签: android xml parsing

我正在用XML解析器编写一个android应用程序。 我有一个曾经工作过的解析器,但是当我运行它时它没有做任何事情。 这是我的班级:

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输出毫无价值。 我没有改变代码,但它只是不再工作了。

1 个答案:

答案 0 :(得分:1)

有什么例外?

可能会发生一些事情。可能是您尝试从Web服务获取URL时。可能是因为你必须在AsyncTask中执行代码。 我建议您右键单击您怀疑的行并切换断点,然后在Package Explorer中继续右键单击它并转到“Debug As”然后“Debug Configuration”。检查正在调试正确的项目后单击“调试”按钮。

欢迎使用调试模式,只需尝试查看导致异常的行。