2013年9月17日星期二

java in SAX parsing XML files network problems

Hello, everyone . There is now a program from the Internet to download directly into the XML file, the following code is a simple example:

<br /> <! [CDATA [IT- Sina blog ] ] > <br />

<br /> <! [CDATA [ Sina blog Channel ] ] > <br />
http://blog.sina.com.cn
http://www.sinaimg.cn/blog/main/index2010/blog_logo.png




http://blog.sina.com.cn/lm/tech/
zh-cn
WWW.SINA.COM.CN
5



Tue, 17 Sep 2013 09:39:34 GMT




lixinghua
a1169949235a@163.com


liuwei
b1169949235b@163.com




I just need to parse linkman child elements . The other is not resolved . Here is the code I wrote :
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;
}



@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;
}
}
. Seek solutions
------ 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

没有评论:

发表评论