各位好。现在有一个从网上直接下载放入程序中的XML文件,下面代码是一个简单的例子:
<addresslist>
<title>
<![CDATA[IT-新浪博客]]>
</title>
<image>
<title>
<![CDATA[新浪博客频道]]>
</title>
<link>http://blog.sina.com.cn</link>
<url>http://www.sinaimg.cn/blog/main/index2010/blog_logo.png</url>
</image>
<description>
<![CDATA[IT博客列表]]>
</description>
<link>http://blog.sina.com.cn/lm/tech/</link>
<language>zh-cn</language>
<generator>WWW.SINA.COM.CN</generator>
<ttl>5</ttl>
<copyright>
<![CDATA[Copyright 1996 - 2013 SINA Inc. All Rights Reserved]]>
</copyright>
<pubDate>Tue, 17 Sep 2013 09:39:34 GMT</pubDate>
<category>
<![CDATA[]]>
</category>
<linkman>
<title> lixinghua</title>
<email>a1169949235a@163.com</email>
</linkman>
<linkman>
<title> liuwei</title>
<email>b1169949235b@163.com</email>
</linkman>
</addresslist>
我只需要解析linkman元素的子元素。其他的不解析。下面是我写的代码:
public class MySax extends DefaultHandler {
private List<LinkMan> all = null;// 保存数据
private LinkMan man = null;
private String elementName = null;
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
if (this.elementName != null) {
String data = new String(ch, start, length);
if ("title".equals(this.elementName)) {
this.man.setName(data);
} else if ("email".equals(this.elementName)) {
this.man.setEmail(data);
}
}
}
public LinkMan getMan() {
return man;
}
public void setMan(LinkMan man) {
this.man = man;
}
public String getElementName() {
return elementName;
}
public void setElementName(String elementName) {
this.elementName = elementName;