在android中使用SAXParser时出错

时间:2013-08-07 04:13:23

标签: java android xml-parsing sax saxparser

在尝试在anrdroid中编写基本的XML解析程序时,我遇到了一个奇怪的错误。

代码在xr.parse的MainActivity.java文件中崩溃(new InputSource(url.openStream()));

由于某种原因,LogCat也没有工作,也不会打印出异常错误。

这是我的MainActivity.java文件:

package com.chsnews;

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.widget.TextView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_main);
    TextView tv = new TextView(this);
    try{
        URL url = new     URL("http://www.anddev.org/images/tut/basic/parsingxml/example.xml");
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();
        ExampleHandler myExampleHandler = new ExampleHandler();
        xr.setContentHandler(myExampleHandler);
        xr.parse(new InputSource(url.openStream()));
        ParsedExampleDataSet parsedExampleDataSet = myExampleHandler.getParsedData();
        Log.d("Debug", parsedExampleDataSet.toString());
        tv.setText(parsedExampleDataSet.toString());
    } catch(Exception e){
        tv.setText(e.getMessage());
        Log.d("DEBUG", e.getMessage());
    }
    this.setContentView(tv);
}

}

这是我的示例处理程序类:

package com.chsnews;

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

public class ExampleHandler extends DefaultHandler {
private boolean in_outertag = false;
private boolean in_innertag = false;
private boolean in_mytag = false;

private ParsedExampleDataSet myParsedExampleDataSet = new ParsedExampleDataSet();

public ParsedExampleDataSet getParsedData() {
    return this.myParsedExampleDataSet;
}

public void startDocument() throws SAXException {
    this.myParsedExampleDataSet = new ParsedExampleDataSet();
}

public void endDocument() throws SAXException {

}

public void startElement(String namespaceURI, String localName,
        String qName, Attributes atts) throws SAXException {
    if (localName.equals("outertag")) {
        this.in_outertag = true;
    } else if (localName.equals("innertag")) {
        this.in_innertag = true;
    } else if (localName.equals("mytag")) {
        this.in_mytag = true;
    } else if (localName.equals("tagwithnumber")) {
        // Extract an Attribute
        String attrValue = atts.getValue("thenumber");
        int i = Integer.parseInt(attrValue);
        myParsedExampleDataSet.setExtractedInt(i);
    }
}

public void characters(char ch[], int start, int length) {
    if (this.in_mytag) {
        myParsedExampleDataSet.setExtractedString(new String(ch, start,
                length));
    }
}
}

这是我的ParsedExampleDataSet文件:     包com.chsnews;

public class ParsedExampleDataSet {
private String extractedString = null;
private int extractedInt = 0;

public String getExtractedString() {
        return extractedString;
}
public void setExtractedString(String extractedString) {
        this.extractedString = extractedString;
}

public int getExtractedInt() {
        return extractedInt;
}
public void setExtractedInt(int extractedInt) {
        this.extractedInt = extractedInt;
}

public String toString(){
        return "ExtractedString = " + this.extractedString
                        + "nExtractedInt = " + this.extractedInt;
}
}

任何帮助都会很棒!

0 个答案:

没有答案