SAX解析不起作用 - 错误 -

时间:2013-02-13 09:13:32

标签: android xml parsing sax

我试图制作一个sax解析器,但我遇到了错误。我尝试了一个教程,当运行它工作正常,但当我把代码放在我自己的Android应用程序中时,它会出错。

有人可以告诉我这是什么问题吗?

这里我有我的代码:

活动:

package nl.appyourservice.ronaldgoedemondt;

import java.net.URL;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SpeellijstActivity extends Activity {

XMLGettersSetters data;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_speellijst);

    /** 
     * Get the view of the layout within the main layout, so that we can add     TextViews.
     **/
    View layout = findViewById(R.id.layout);

    /**  
     * Create TextView Arrays to add the retrieved data to.
     **/
    TextView title[];

    TextView artist[];

    TextView country[];

    TextView company[];

    TextView price[];

    TextView year[];

    try {

        /**
         * Create a new instance of the SAX parser
         **/
        SAXParserFactory saxPF = SAXParserFactory.newInstance();
        SAXParser saxP = saxPF.newSAXParser();
        XMLReader xmlR = saxP.getXMLReader();


        URL url = new     URL("http://www.xmlfiles.com/examples/cd_catalog.xml"); // URL of the XML

        /** 
         * Create the Handler to handle each of the XML tags. 
         **/
        XMLHandler myXMLHandler = new XMLHandler();
        xmlR.setContentHandler(myXMLHandler);
        xmlR.parse(new InputSource(url.openStream()));

    } catch (Exception e) {
        System.out.println(e);
    }

    data = XMLHandler.data;

    /** 
     * Makes the TextView length the size of the TextView arrays by getting the size of the 
     **/
    title = new TextView[data.getTitle().size()];
    artist = new TextView[data.getArtist().size()];
    country = new TextView[data.getCountry().size()];
    company = new TextView[data.getCompany().size()];
    price = new TextView[data.getPrice().size()];
    year = new TextView[data.getYear().size()];


    /** 
     * Run a for loop to set All the TextViews with text until 
     * the size of the array is reached.
     * 
     **/
    for (int i = 0; i < data.getTitle().size(); i++) {

        title[i] = new TextView(this);
        title[i].setText("Title = "+data.getTitle().get(i));

        artist[i] = new TextView(this);
        artist[i].setText("Artist = "+data.getArtist().get(i));

        country[i] = new TextView(this);
        country[i].setText("Country = "+data.getCountry().get(i));

        company[i] = new TextView(this);
        company[i].setText("Company = "+data.getCompany().get(i));

        price[i] = new TextView(this);
        price[i].setText("Price = "+data.getPrice().get(i));

        year[i] = new TextView(this);
        year[i].setText("Year = "+data.getYear().get(i));

        ((ViewGroup) layout).addView(title[i]);
        ((ViewGroup) layout).addView(artist[i]);
        ((ViewGroup) layout).addView(country[i]);
        ((ViewGroup) layout).addView(company[i]);
        ((ViewGroup) layout).addView(price[i]);
        ((ViewGroup) layout).addView(year[i]);
    }

    setContentView(layout);

}
}

xml处理程序:

package nl.appyourservice.ronaldgoedemondt;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class XMLHandler extends DefaultHandler {

String elementValue = null;
Boolean elementOn = false;
public static XMLGettersSetters data = null;

public static XMLGettersSetters getXMLData() {
    return data;
}

public static void setXMLData(XMLGettersSetters data) {
    XMLHandler.data = data;
}

/** 
 * This will be called when the tags of the XML starts.
 **/
@Override
public void startElement(String uri, String localName, String qName,
        Attributes attributes) throws SAXException {

    elementOn = true;

    if (localName.equals("CATALOG"))
    {
        data = new XMLGettersSetters();
    } else if (localName.equals("CD")) {
        /** 
         * We can get the values of attributes for eg. if the CD tag had an     attribute( <CD attr= "band">Akon</CD> ) 
         * we can get the value "band". Below is an example of how to achieve this.
         * 
         * String attributeValue = attributes.getValue("attr");
         * data.setAttribute(attributeValue);
         * 
         * */
    }
}

/** 
 * This will be called when the tags of the XML end.
 **/
@Override
public void endElement(String uri, String localName, String qName)
        throws SAXException {

    elementOn = false;

    /** 
     * Sets the values after retrieving the values from the XML tags
     * */ 
    if (localName.equalsIgnoreCase("title"))
        data.setTitle(elementValue);
    else if (localName.equalsIgnoreCase("artist"))
        data.setArtist(elementValue);
    else if (localName.equalsIgnoreCase("country"))
        data.setCountry(elementValue);
    else if (localName.equalsIgnoreCase("company"))
        data.setCompany(elementValue);
    else if (localName.equalsIgnoreCase("price"))
        data.setPrice(elementValue);
    else if (localName.equalsIgnoreCase("year"))
        data.setYear(elementValue);
}

/** 
 * This is called to get the tags value
 **/
@Override
public void characters(char[] ch, int start, int length)
        throws SAXException {

    if (elementOn) {
        elementValue = new String(ch, start, length);
        elementOn = false;
    }

}

}

xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/layout">

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:id="@+id/layout_string"/>

</LinearLayout>

xml getter和setter:

package nl.appyourservice.ronaldgoedemondt;

import java.util.ArrayList;

import android.util.Log;

/**
 *  This class contains all getter and setter methods to set and retrieve data.
 *  
 **/
public class XMLGettersSetters {

private ArrayList<String> title = new ArrayList<String>();
private ArrayList<String> artist = new ArrayList<String>();
private ArrayList<String> country = new ArrayList<String>();
private ArrayList<String> company = new ArrayList<String>();
private ArrayList<String> price = new ArrayList<String>();
private ArrayList<String> year = new ArrayList<String>();

public ArrayList<String> getCompany() {
    return company;
}

public void setCompany(String company) {
    this.company.add(company);
    Log.i("This is the company:", company);
}

public ArrayList<String> getPrice() {
    return price;
}

public void setPrice(String price) {
    this.price.add(price);
    Log.i("This is the price:", price);
}

public ArrayList<String> getYear() {
    return year;
}

public void setYear(String year) {
    this.year.add(year);
    Log.i("This is the year:", year);
}

public ArrayList<String> getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title.add(title);
    Log.i("This is the title:", title);
}

public ArrayList<String> getArtist() {
    return artist;
}

public void setArtist(String artist) {
    this.artist.add(artist);
    Log.i("This is the artist:", artist);
}

public ArrayList<String> getCountry() {
    return country;
}

public void setCountry(String country) {
    this.country.add(country);
    Log.i("This is the country:", country);
}

}

和清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="nl.appyourservice.ronaldgoedemondt"
android:versionCode="1"
android:versionName="1.0" 
>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<application        
    android:debuggable='true'
    android:allowBackup="true"
    android:icon="@drawable/icon_2x"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.NoTitleBar">
    <activity android:name="nl.appyourservice.ronaldgoedemondt.SplashActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="nl.appyourservice.ronaldgoedemondt.MainActivity"></activity>
    <activity android:name="nl.appyourservice.ronaldgoedemondt.BiografieActivity"></activity>
    <activity android:name="nl.appyourservice.ronaldgoedemondt.SpeellijstActivity"></activity>
    <activity android:name="nl.appyourservice.ronaldgoedemondt.SoundboardActivity"></activity>
    <activity android:name="nl.appyourservice.ronaldgoedemondt.InfoActivity"></activity>
</application>

如果你知道我做错了什么,请告诉我!〜(我刚接触到这个)

0 个答案:

没有答案