问题描述
我有一个自定义的ContentHandler(称为XMLHandler),我去过很多通过谷歌和计算器网站,详细说明如何设置起来。
我不明白的是如何使用它。
Xml.parse(...,...)返回任何内容,因为它是一个无效的方法。
我如何访问我已解析的XML数据?
我意识到这个问题可能是微不足道的,但我一直在寻找(直译)小时,没有发现任何解决方案。
请帮忙。
= fetchData(doesntmatter)字符串结果;
Xml.parse(结果,新XMLHandler());
下面是一个例子,我希望这将是有益的理解的SAXParser
包test.example;
进口java.io.BufferedReader中;
进口的java.io.InputStream;
进口java.io.InputStreamReader中;
进口的java.net.URL;
进口javax.xml.parsers.SAXParser中;
进口javax.xml.parsers.SAXParserFactory中;
进口org.xml.sax.InputSource中;
进口org.xml.sax.SAXException;
进口org.xml.sax.XMLReader中;
进口org.xml.sax.helpers.DefaultHandler中;
进口android.app.Activity;
进口android.os.Bundle;
进口android.util.Log;
进口android.widget.TextView;
公共类XMLParsingDemo延伸活动{
私人最终字符串MY_DEBUG_TAG =WeatherForcaster;
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包冰柱){
super.onCreate(冰柱);
/ *创建一个新的TextView后,显示parsingresult。 * /
TextView的电视=新的TextView(本);
尝试 {
/ *创建我们希望加载某些XML数据的URL。 * /
DefaultHttpClient HC =新DefaultHttpClient();
ResponseHandler的<字符串> RES =新BasicResponseHandler();
HttpPost的PostMethod =新HttpPost(http://www.anddev.org/images/tut/basic/parsingxml/example.xml);
字符串响应= hc.execute(后方法,RES);
/ *获取从一个SAXParserFactory的SAXParser的。 * /
的SAXParserFactory SPF = SAXParserFactory.newInstance();
的SAXParser藻= spf.newSAXParser();
/ *获取我们创建的SAXParser的XMLReader的。 * /
XMLReader的XR = sp.getXMLReader();
/ *创建一个新的ContentHandler,并将其应用到XML阅读器* /
ExampleHandler myExampleHandler =新ExampleHandler();
xr.setContentHandler(myExampleHandler);
/ *从我们的网址解析XML数据。 * /
的InputSource的InputSource =新的InputSource();
inputSource.setEncoding(UTF-8);
inputSource.setCharacterStream(新StringReader(响应));
/ *从我们的网址解析XML数据。 * /
xr.parse(InputSource的);
/ *解析完成。 * /
/ *我们ExampleHandler现在提供所分析的数据给我们。 * /
ParsedExampleDataSet parsedExampleDataSet = myExampleHandler.getParsedData();
/ *设置的结果显示在我们的GUI。 * /
tv.setText(响应+\ñ\ñ\ñ************************************* **+ parsedExampleDataSet.toString());
}赶上(例外五){
/ *显示任何错误的图形用户界面。 * /
tv.setText(错误:+ e.getMessage());
Log.e(MY_DEBUG_TAG,WeatherQueryError,E);
}
/ *显示TextView的。 * /
this.setContentView(电视);
}
公共类ExampleHandler扩展的DefaultHandler {
// ================================================ ===========
//领域
// ================================================ ===========
私人布尔in_outertag = FALSE;
私人布尔in_innertag = FALSE;
私人布尔in_mytag = FALSE;
私人ParsedExampleDataSet myParsedExampleDataSet =新ParsedExampleDataSet();
// ================================================ ===========
//吸气和放大器;二传手
// ================================================ ===========
公共ParsedExampleDataSet getParsedData(){
返回this.myParsedExampleDataSet;
}
// ================================================ ===========
//方法
// ================================================ ===========
@覆盖
公共无效startDocument()抛出的SAXException {
this.myParsedExampleDataSet =新ParsedExampleDataSet();
}
@覆盖
公共无效调用endDocument()抛出的SAXException {
// 没事做
}
/ **获取被要求打开标签,如:
*<标签>
*可提供的属性(S),当XML是这样的:
*<标签属性=的AttributeValue> * /
@覆盖
公共无效的startElement(URI字符串,字符串的localName,字符串QNAME,org.xml.sax.Attributes的ATT)抛出的SAXException {
super.startElement(URI,localName创建,QNAME,的ATT);
如果(localName.equals(outertag)){
this.in_outertag = TRUE;
}
否则如果(localName.equals(innertag)){
串attrValue = atts.getValue(sampleattribute);
myParsedExampleDataSet.setExtractedString(attrValue);
this.in_innertag = TRUE;
}
否则如果(localName.equals(MyTag的)){
this.in_mytag = TRUE;
}
否则如果(localName.equals(tagwithnumber)){
//提取的属性
串attrValue = atts.getValue(数量写);
INT I =的Integer.parseInt(attrValue);
myParsedExampleDataSet.setExtractedInt(ⅰ);
}
}
/ **获取被要求关闭标签,如:
*< /标签> * /
@覆盖
公共无效的endElement(字符串的namespaceURI,字符串的localName,字符串QNAME)
抛出的SAXException {
如果(localName.equals(outertag)){
this.in_outertag = FALSE;
}否则,如果(localName.equals(innertag)){
this.in_innertag = FALSE;
}否则,如果(localName.equals(MyTag的)){
this.in_mytag = FALSE;
}否则,如果(localName.equals(tagwithnumber)){
// 在这儿无事可做
}
}
/ **获取被称为上的结构如下:
*<标签>字符< /标签> * /
@覆盖
公共无效字符(字符CH [],诠释开始,诠释长度){
如果(this.in_mytag){
myParsedExampleDataSet.setExtractedString(新的字符串(CH));
}
}
}
公共类ParsedExampleDataSet {
私人字符串extractedString = NULL;
私人诠释extractedInt = 0;
公共字符串getExtractedString(){
返回extractedString;
}
公共无效setExtractedString(字符串extractedString){
this.extractedString = extractedString;
}
公众诠释getExtractedInt(){
返回extractedInt;
}
公共无效setExtractedInt(INT extractedInt){
this.extractedInt = extractedInt;
}
公共字符串的toString(){
回归\ñ\ñ\ nExtractedString =+ this.extractedString
+\ñ\ñ\ nExtractedInt =+ this.extractedInt;
}
}
}
I've got a custom contentHandler (called XMLHandler), I've been to a lot of sites via Google and StackOverflow that detail how to set that up.
What I do not understand is how to USE it.
Xml.parse(...,...) returns nothing, because it is a void method.
How do I access my parsed XML data?
I realize this question is probably trivial, but I've been searching for (literally) hours and have found no solution.
Please help.
String result = fetchData(doesntmatter);
Xml.parse(result, new XMLHandler());
Here is one example i hope it will be usefull to understand "SAXParser"
package test.example;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class XMLParsingDemo extends Activity {
private final String MY_DEBUG_TAG = "WeatherForcaster";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
/* Create a new TextView to display the parsingresult later. */
TextView tv = new TextView(this);
try {
/* Create a URL we want to load some xml-data from. */
DefaultHttpClient hc = new DefaultHttpClient();
ResponseHandler <String> res = new BasicResponseHandler();
HttpPost postMethod = new HttpPost("http://www.anddev.org/images/tut/basic/parsingxml/example.xml");
String response=hc.execute(postMethod,res);
/* Get a SAXParser from the SAXPArserFactory. */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
/* Get the XMLReader of the SAXParser we created. */
XMLReader xr = sp.getXMLReader();
/* Create a new ContentHandler and apply it to the XML-Reader*/
ExampleHandler myExampleHandler = new ExampleHandler();
xr.setContentHandler(myExampleHandler);
/* Parse the xml-data from our URL. */
InputSource inputSource = new InputSource();
inputSource.setEncoding("UTF-8");
inputSource.setCharacterStream(new StringReader(response));
/* Parse the xml-data from our URL. */
xr.parse(inputSource);
/* Parsing has finished. */
/* Our ExampleHandler now provides the parsed data to us. */
ParsedExampleDataSet parsedExampleDataSet = myExampleHandler.getParsedData();
/* Set the result to be displayed in our GUI. */
tv.setText(response + "\n\n\n***************************************" + parsedExampleDataSet.toString());
} catch (Exception e) {
/* Display any Error to the GUI. */
tv.setText("Error: " + e.getMessage());
Log.e(MY_DEBUG_TAG, "WeatherQueryError", e);
}
/* Display the TextView. */
this.setContentView(tv);
}
public class ExampleHandler extends DefaultHandler {
// ===========================================================
// Fields
// ===========================================================
private boolean in_outertag = false;
private boolean in_innertag = false;
private boolean in_mytag = false;
private ParsedExampleDataSet myParsedExampleDataSet = new ParsedExampleDataSet();
// ===========================================================
// Getter & Setter
// ===========================================================
public ParsedExampleDataSet getParsedData() {
return this.myParsedExampleDataSet;
}
// ===========================================================
// Methods
// ===========================================================
@Override
public void startDocument() throws SAXException {
this.myParsedExampleDataSet = new ParsedExampleDataSet();
}
@Override
public void endDocument() throws SAXException {
// Nothing to do
}
/** Gets be called on opening tags like:
* <tag>
* Can provide attribute(s), when xml was like:
* <tag attribute="attributeValue">*/
@Override
public void startElement(String uri, String localName, String qName, org.xml.sax.Attributes atts) throws SAXException {
super.startElement(uri, localName, qName, atts);
if (localName.equals("outertag")) {
this.in_outertag = true;
}
else if (localName.equals("innertag")) {
String attrValue = atts.getValue("sampleattribute");
myParsedExampleDataSet.setExtractedString(attrValue);
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);
}
}
/** Gets be called on closing tags like:
* </tag> */
@Override
public void endElement(String namespaceURI, String localName, String qName)
throws SAXException {
if (localName.equals("outertag")) {
this.in_outertag = false;
}else if (localName.equals("innertag")) {
this.in_innertag = false;
}else if (localName.equals("mytag")) {
this.in_mytag = false;
}else if (localName.equals("tagwithnumber")) {
// Nothing to do here
}
}
/** Gets be called on the following structure:
* <tag>characters</tag> */
@Override
public void characters(char ch[], int start, int length) {
if(this.in_mytag){
myParsedExampleDataSet.setExtractedString(new String(ch));
}
}
}
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 "\n\n\nExtractedString = " + this.extractedString
+ "\n\n\nExtractedInt = " + this.extractedInt;
}
}
}
这篇关于安卓:解析从字符串问题XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!