sax解析xml求助 直接在characters方面里面打印不要判断,应该是有值得,主要是判断有问题 把有值得数据覆盖了。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 characters()这个方法 ,每个在标签开始时会触发一次,读到结尾时会触发一次, 比如 <TITLE>EmpireBurlesque</TITLE> 在读TITLE的时候会触发一次,</TITLE>的时候会再触发一次,第二次的值为""把第一次读的值给覆盖掉了, 所以在 characters()这个方法里面应该加个判断 boolean isStartElement,判断是 startElement(isStartElement =true;) 状态还是 endElement (isStartElement =false;)package cn.touchmedia.sax;import org.xml.sax.SAXException;import org.xml.sax.helpers.DefaultHandler;public class MyContentHandler extends DefaultHandler { boolean isStartElement =false; String tagName; String title; String artist; String country; String company; String price; String year; @Override public void startDocument() throws SAXException { // TODO Auto-generated method stub System.out.println("startDocument"); super.startDocument(); } @Override public void endDocument() throws SAXException { // TODO Auto-generated method stub System.out.println("endDocument"); super.endDocument(); } @Override public void startElement(String uri, String localName, String qName, org.xml.sax.Attributes attributes) throws SAXException { // TODO Auto-generated method stub super.startElement(uri, localName, qName, attributes); tagName = qName; isStartElement =true; } @Override public void endElement(String uri, String localName, String qName) throws SAXException { // TODO Auto-generated method stub super.endElement(uri, localName, qName); if (qName=="CD") { this.printout(); } isStartElement=false; } @Override public void characters(char[] ch, int start, int length) throws SAXException { // TODO Auto-generated method stub if(isStartElement){ if (tagName == "TITLE"){ title = new String(ch,start,length).trim(); }else if (tagName=="ARTIST") artist = new String(ch, start, length).trim(); else if (tagName=="COUNTRY") country = new String(ch, start, length).trim(); else if (tagName=="COMPANY") company = new String(ch, start, length).trim(); else if (tagName=="PRICE") price = new String(ch, start, length).trim(); else if (tagName=="YEAR") year = new String(ch, start, length).trim(); } } private void printout(){ System.out.println(title+","+artist+","+company+","+country+","+price+","+year); }} 按照您说的修改了一下,的确OK了但我看MARS老师的视频,他都没有做判断,为何可以正常解析呢 http://www.iteye.com/topic/763895 去看下这个.. Android重力感应之谈 如何以rect为中心,来旋转它. 如果想要屏蔽掉界面的状态栏,如时间和联网状态那一栏,需要怎么改? 为什么android通过http与servlet通讯乱码的帖子没人回复?(回复即有分) 如何将android设备作成服务端 android fragment界面的添加 求助:android 如何在系统设置中添加自己的设置项(不改变系统设置源码) 现在要做一个android分屏异显,没有思路,求指点 求助!!listview中根据button显示不同列表 Android Studio Android如何实现表格分页 求几书android开发方面的书箱。
import org.xml.sax.helpers.DefaultHandler;public class MyContentHandler extends DefaultHandler {
boolean isStartElement =false;
String tagName;
String title;
String artist;
String country;
String company;
String price;
String year; @Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
System.out.println("startDocument");
super.startDocument();
} @Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
System.out.println("endDocument");
super.endDocument();
} @Override
public void startElement(String uri, String localName, String qName,
org.xml.sax.Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
super.startElement(uri, localName, qName, attributes);
tagName = qName;
isStartElement =true;
} @Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
// TODO Auto-generated method stub
super.endElement(uri, localName, qName);
if (qName=="CD") {
this.printout();
}
isStartElement=false;
} @Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
if(isStartElement){
if (tagName == "TITLE"){
title = new String(ch,start,length).trim();
}else if (tagName=="ARTIST")
artist = new String(ch, start, length).trim();
else if (tagName=="COUNTRY")
country = new String(ch, start, length).trim();
else if (tagName=="COMPANY")
company = new String(ch, start, length).trim();
else if (tagName=="PRICE")
price = new String(ch, start, length).trim();
else if (tagName=="YEAR")
year = new String(ch, start, length).trim();
}
}
private void printout(){
System.out.println(title+","+artist+","+company+","+country+","+price+","+year);
}
}
按照您说的修改了一下,的确OK了
但我看MARS老师的视频,他都没有做判断,为何可以正常解析呢