解析一个xml文件:
MP3ContentHandler.java:
输出结果:
把xml文件改成:
输出:
很明显把结束标签后的东西也传入char[] ch了。请问各位大神,如何解决??
<?xml version="1.0" encoding="ISO-8859-1"?>
<resources>
<resource>
<id>0001</id>0001
<mp3.name>a1.mp3</mp3.name>
<mp3.size>6,473,098</mp3.size>
<lrc.name>a1.lrc</lrc.name>
<lrc.size>2,124</lrc.size>
</resource>
</resources>
MP3ContentHandler.java:
public class MP3ContentHandler extends DefaultHandler{
public MP3ContentHandler(List<MP3Info> mp3Infos) {
this.mp3Infos = mp3Infos;
}
public List<MP3Info> getMp3Infos() {
return mp3Infos;
}
public void setMp3Infos(List<MP3Info> mp3Infos) {
this.mp3Infos = mp3Infos;
}
List <MP3Info> mp3Infos = null;
MP3Info mp3Info = null;
String tagName = null;
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
String temp = new String(ch,start,length);
if(temp != " "){
if(tagName.equals("id")){
mp3Info.setId(temp);
Log.d("yinan", temp);//
}
else if(tagName.equals("mp3.name")){
mp3Info.setMp3Name(temp);
Log.d("yinan", temp);
}
else if(tagName.equals("mp3.size")){
mp3Info.setMp3Size(temp);
Log.d("yinan", temp);
}
else if(tagName.equals("lrc.name")){
mp3Info.setLrcName(temp);
Log.d("yinan", temp);/
}
else if(tagName.equals("lrc.size")){
mp3Info.setLrcSize(temp);
Log.d("yinan", temp);/
}
}
}
@Override
public void endDocument() throws SAXException {
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if(qName.equals("resource")){
mp3Infos.add(mp3Info);
}
}
@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
super.startDocument();
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
tagName = localName; ///
if(tagName.equals("resource")){
mp3Info = new MP3Info();
}
}
}
输出结果:
把xml文件改成:
<?xml version="1.0" encoding="ISO-8859-1"?>
<resources>
<resource>
<id>0001</id>0001
<mp3.name>a1.mp3</mp3.name>a1.mp3
<mp3.size>6,473,098</mp3.size>6,473,098
<lrc.name>a1.lrc</lrc.name>a1.lrc
<lrc.size>2,124</lrc.size>2,124
</resource>
</resources>
输出:
很明显把结束标签后的东西也传入char[] ch了。请问各位大神,如何解决??