0

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

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

由于某种原因,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 文件:package 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;
}
}

任何帮助都会很棒!

4

0 回答 0