我正在通过Sax解析器在web服务的android中创建一个小应用程序我的链接是
(http://www.anddev.org/images/tut/basic/parsingxml/example.xml)I能够显示****< ** innertag>,******的值但是无法显示文本视图中的值
这是我的代码
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
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 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;
String attrValue = atts.getValue("sampleattribute");
myParsedExampleDataSet.setExtractedString(attrValue);
}else if (localName.equals("mytag")) {
//this.in_mytag = true;
String attrValue = atts.getValue("mytag");
myParsedExampleDataSet.setExtractedString1(attrValue);
}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.setExtractedString1(new String(ch, start, length));
}
}
}![enter image description here][1]
答案 0 :(得分:1)
您在startElement()中注释了this.in_mytag = true
。因此,设置ExtractedString1的characters()函数中的代码块未执行,因为in_mytag为false。
处理mytag的开头时另外一件事:String attrValue = atts.getValue("mytag");
是不必要的。它应该在characters()函数中处理(我怀疑你只是出于调试目的)。