Android XML Parser isnt working

Posted by Bram on Stack Overflow See other posts from Stack Overflow or by Bram
Published on 2012-04-15T12:59:09Z Indexed on 2012/06/05 22:40 UTC
Read the original article Hit count: 435

Filed under:
|
|

I am writing an android application with a XML parser. I have a parser that used to work but when I run it it isnt doing anything. This is my class:

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);

}
}

And my manifest:

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

And the logcat output is worthless. I didnt change the code but its just not working anymore.

© Stack Overflow or respective owner

Related posts about android

Related posts about Xml