http://blog.sina.com.cn
http://blog.sina.com.cn/lm/tech/
I just need to parse linkman child elements . The other is not resolved . Here is the code I wrote :
public class MySax extends DefaultHandler {. Seek solutions
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;
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if("linkman".equals(qName)){
this.all.add(this.man);
this.man=null;//准备保存下次的数据
this.elementName=null;//将元素名称清空
}
}
@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
this.all = new ArrayList<LinkMan>();// 开始解析,所以设置集合
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
if ("linkman".equals(qName)) {
this.man = new LinkMan(); // 实例化LinkMan对象
this.elementName = qName; // 保存元素名称
}
}
public List<LinkMan> getAll() {
return all;
}
}
------ Solution ------------------------------------- -------
looking for a long time and finally found it.
reasons are:
at this point
if ("linkman".equals(qName)) {
this.man = new LinkMan(); // 实例化LinkMan对象
this.elementName = qName; // 保存元素名称
}
you are when tag name is linkman is only set value, and the value has been the linkman. When the file is parsed into title tag , elementName value or linkman. So you are in characters () method in the judgment, two if condition is always false, so you do not get the value . You can this.elementName = qName; this code into the if statement top.
changed your code below:
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
this.elementName = qName; // 保存元素名称
if ("linkman".equals(qName)) {
this.man = new LinkMan(); // 实例化LinkMan对象
}
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if ("title".equals(this.elementName)) {
System.out.println(new String(ch, start, length));
} else if ("email".equals(this.elementName)) {
System.out.println(new String(ch, start, length));
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
this.elementName = qName;// 将元素名称清空
if ("linkman".equals(qName)) {
this.all.add(this.man);
this.man = null;// 准备保存下次的数据
}
}
------ For reference only ----------------------------------- ----
supplement , I parse when parsing all nulls.
------ For reference only -------------------------------------- -
find the next did not see the problem, I hope to see the next questions . Thank you. I first went to eat .
------ For reference only -------------------------------------- -
Oh, thank you very much . I also found out . Scores give you
------ For reference only ---------------------------------- -----
Oh, thank you very much . I also found out . Scores give you
没有评论:
发表评论