Sax解析Xml

对于解析大型的xml,可能使用整个document或整个文件都读到内存中是不合适的。
以下只是一个用例,sax解析是无状态的,也就是说他不知道上一次解析了什么,或下一次解析什么,他只知道解析的是当前行,所以这是流水线的,不会占大内存.

package org.frame.base.xml.jdk;

import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

/**
* foreigner 's xml paser is interesting.
*
* @author ycl
* @version 1.0 2012-12-10 上午10:50:53
* @since 1.0
*
*/
public class JDKxmlSax {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
StringBuffer xml = new StringBuffer("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
xml.append("<contact>");

xml.append("<item>");
xml.append("<uic>1</uic>");
xml.append("<fullName>ycl1</fullName>");
xml.append("</item>");

xml.append("<item>");
xml.append("<uic>2</uic>");
xml.append("<fullName>ycl2</fullName>");
xml.append("</item>");

xml.append("</contact>");


InputSource is = new InputSource(new StringReader(xml.toString()));
ContactListXmlHandler handler = parse(is);
System.out.println(handler.getContactNameList());
}

public static ContactListXmlHandler parse(InputSource input){
ContactListXmlHandler handler = new ContactListXmlHandler();
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser sp = factory.newSAXParser();
XMLReader xr = sp.getXMLReader();

xr.setContentHandler(handler);
xr.parse(input);
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return handler;
}

}




package org.frame.base.xml.jdk;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class ContactListXmlHandler extends DefaultHandler implements Resources {

private List<ContactName> contactNameList = new ArrayList<ContactName>();

private ContactName contactItem;

private StringBuffer sb;

public List<ContactName> getContactNameList() {
return contactNameList;
}

@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
super.startDocument();
sb = new StringBuffer();
}

@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
super.startElement(uri, localName, qName, attributes);
if (qName.equals(CONTACT_ITEM)) {
contactItem = new ContactName();
}
sb.setLength(0);
}

@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
try {
super.characters(ch, start, length);
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sb.append(ch, start, length);
}

@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
super.endDocument();
}

@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
// TODO Auto-generated method stub
if (contactItem != null) {
if (qName.equalsIgnoreCase(ITEM_UIC)) {
contactItem.setUid(sb.toString());

} else if (qName.equalsIgnoreCase(ITEM_FULLNAME)) {
contactItem.setFullName(sb.toString());

} else if (qName.equalsIgnoreCase(CONTACT_ITEM)) {
contactNameList.add(contactItem);
}

sb.setLength(0);
}
}

}




package org.frame.base.xml.jdk;

import net.sf.json.JSONObject;

public class ContactName {

private String uid;
private String fullName;
private String width;

public String getWidth() {
return width;
}
public void setWidth(String width) {
this.width = width;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
@Override
public String toString() {
return "uid:"+uid+",fullName:"+fullName+",width:"+width;
}


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值