java 对xml进行sax解析



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

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

public class SaxHandler extends DefaultHandler {
/* 此方法有三个参数
arg0是传回来的字符数组,其包含元素内容
arg1和arg2分别是数组的开始位置和结束位置 */
@Override
public void characters(char[] arg0, int arg1, int arg2) throws SAXException {
String content = new String(arg0, arg1, arg2);
System.out.println(content);
super.characters(arg0, arg1, arg2);
}

@Override
public void endDocument() throws SAXException {
System.out.println("\n…………结束解析文档…………");
super.endDocument();
}

/* arg0是名称空间
arg1是包含名称空间的标签,如果没有名称空间,则为空
arg2是不包含名称空间的标签 */
@Override
public void endElement(String arg0, String arg1, String arg2)
throws SAXException {
System.out.println("结束解析元素 " + arg2);
super.endElement(arg0, arg1, arg2);
}

@Override
public void startDocument() throws SAXException {
System.out.println("…………开始解析文档…………\n");
super.startDocument();
}

/*arg0是名称空间
arg1是包含名称空间的标签,如果没有名称空间,则为空
arg2是不包含名称空间的标签
arg3很明显是属性的集合 */
@Override
public void startElement(String arg0, String arg1, String arg2,
Attributes arg3) throws SAXException {
System.out.println("开始解析元素 " + arg2);
if (arg3 != null) {
for (int i = 0; i < arg3.getLength(); i++) {
// getQName()是获取属性名称,
System.out.print(arg3.getQName(i) + "=\"" + arg3.getValue(i) + "\"");
}
}
System.out.print(arg2 + ":");
super.startElement(arg0, arg1, arg2, arg3);
}

}


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

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

public class SaxHandler extends DefaultHandler {
private HashMap<String, String> map = null;
private List<HashMap<String, String>> list = null;
/**
* 正在解析的元素的标签
*/
private String currentTag = null;
/**
* 正在解析的元素的值
*/
private String currentValue = null;
private String nodeName = null;

public List<HashMap<String, String>> getList(){
return list;
}

public SaxHandler(String nodeName) {
this.nodeName = nodeName;
}

@Override
public void startDocument() throws SAXException {
// TODO 当读到一个开始标签的时候,会触发这个方法
list = new ArrayList<HashMap<String,String>>();
}

@Override
public void startElement(String uri, String localName, String name,
Attributes attributes) throws SAXException {
// TODO 当遇到文档的开头的时候,调用这个方法
if(name.equals(nodeName)){
map = new HashMap<String, String>();
}
if(attributes != null && map != null){
for(int i = 0; i < attributes.getLength();i++){
map.put(attributes.getQName(i), attributes.getValue(i));
}
}
currentTag = name;
}

@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO 这个方法用来处理在XML文件中读到的内容
if(currentTag != null && map != null){
currentValue = new String(ch, start, length);
if(currentValue != null && !currentValue.trim().equals("") && !currentValue.trim().equals("\n")){
map.put(currentTag, currentValue);
}
}
currentTag=null;
currentValue=null;
}

@Override
public void endElement(String uri, String localName, String name)
throws SAXException {
// TODO 在遇到结束标签的时候,调用这个方法
if(name.equals(nodeName)){
list.add(map);
map = null;
}
super.endElement(uri, localName, name);
}


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值