如何解析xml数据 实现xml文件解析三种方式
实现xml文件解析三种方式
方式
复制代码 代码如下:
/**
* 使用sax解析
*/
public class SaxParse{
/**
* sax解析器
*/
private SAXParser parser;
public SaxParse{
try {
SAXParserFactory f = Instance;
parser = SAXParser;
} catch (ParserConfigurationException e) {
ntStackTrace;
} catch (Exception e) {
ntStackTrace;
}
}
public ListdoParse(InputStream is) {
try {
XmlHandler h = new XmlHandler;
se(is,h);
return persons;
} catch (Exception e) {
ntStackTrace;
}
return null;
}
/**
* 处理器
*/
class XmlHandler extends DefaultHandler{
Listpersons = null ;
Person person = null ;
//当前元素名称
private String currEleName;
/**
* 文本节点触发该方法
*/
public void characters(char[] ch, int start, int length)throws SAXException {
String str = new String(ch,start,length);
//name
if("name".equals(currEleName)){
e = str ;
}
else if("age".equals(currEleName)){
= seInt(str);
}
}
public void endDocument throws SAXException {
}
/**
* 元素结束
*/
public void endElement(String uri, String localName, String qName)
throws SAXException {
if("person".equals(localName)){
(person);
}
//将当前元素置空
else if(("name".equals(currEleName)) || ("age".equals(currEleName))){
rEleName = "" ;
}
}
/**
* 文档开始事件
*/
public void startDocument throws SAXException {
persons = new ArrayList;
}
/**
* 元素开始事件
* localName:本地名
* uri:名字空间
* qName:限定名,前缀 + 本地名
*/
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
//实例化person对象
if("person".equals(localName)){
person = new Person;
= seInt(Value(0));
}
//name元素
else if("name".equals(localName)){
rEleName = "name" ;
}
//name元素
else if("age".equals(localName)){
rEleName = "age" ;
}
}
public Listgetpersons{
return persons ;
}
}
}
方式
复制代码 代码如下:
/**
* DOM解析
*/
public class DomParse{
//
private DocumentBuilder builder;
public DomParse{
try {
DocumentBuilderFactory f = Instance;
lder = DocumentBuilder;
} catch (Exception e) {
ntStackTrace;
}
}
public ListdoParse(InputStream is) {
Listpersons = new ArrayList;
Person person = null ;
try {
Document doc = se(is);
NodeList list = ElementsByTagName("person");
Element ele = null ;
for(int i = 0 ; i < Length ; i ++){
ele = (Element) m(i);
person = new Person;
= seInt(Attribute("id"));
e = getSubElementTextContent(ele,"name");
= seInt(getSubElementTextContent(ele,"age"));
(person);
}
} catch (Exception e) {
ntStackTrace;
}
return persons;
}
/**
* 得到指定的资源中中间的文本内容
*/
private String getSubElementTextContent(Element ele, String tagName) {
NodeList list = ElementsByTagName(tagName);
Element e = (Element) m(0);
//得到中间的文本节点
return TextContent;
}
}
l方式
复制代码 代码如下:
/**
* pull解析,拉模式,可以手动控制下一个事件是否触发.
*/
public class PullParse{
public ListdoParse(InputStream is) {
Listpersons = null ;
Person person = null ;
try {
XmlPullParser parser = PullParser;
//设置解析数据源
Input(is, "utf-8");
//取得事件的'类型
int eventType = EventType;
String eleName = null ;
while(eventType != _DOCUMENT){
switch(eventType){
//文档开始
case RT_DOCUMENT:
persons = new ArrayList;
break ;
//元素开始
case RT_TAG:
eleName = Name;
if("person".equals(eleName)){
person = new Person;
= seInt(AttributeValue(0));
}
else if("name".equals(eleName)){
e = tText;
}
else if("age".equals(eleName)){
= seInt(tText);
}
break ;
//标记结束
case _TAG:
eleName = Name;
if("person".equals(eleName)){
(person);
}
break ;
}
//手动激活下个事件的触发
eventType = t;
}
} catch (Exception e) {
ntStackTrace;
}
return persons;
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 1156092664@qq.com 举报,一经查实,本站将立刻删除。