现在有一份这样的DOM的xml文件,解析放在list里边
a.xml
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<root xmlns="http://xxx.com/rootResponse">
<code>10</code>
<msg/>
<DATA>
<person_list>
<person>
<A>a</A>
<B>b</B>
<C>c</C>
<D>d</D>
<E>e</E>
<F>f</F>
<G>g</G>
<H>h</H>
<I>i</I>
</person>
<person>
<A>a</A>
<B>b</B>
<C>c</C>
<D>d</D>
<E>e</E>
<F>f</F>
<G>g</G>
<H>h</H>
<I>i</I>
</person>
</person_list>
</DATA>
</root>
</soapenv:Body>
</soapenv:Envelope>
解析代码:
package xx.utils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class XMLUtil extends DefaultHandler {
/**
* xml 解析结果
*/
public static List<Person> personList;
private Person person;
private String node;
private StringBuilder sb;
private boolean flag = false;
public void startDocument () throws SAXException {
//开始解析文档
super.startDocument();
personList = new ArrayList<Person>();
System.out.println("文档解析开始startDocument");
}
public void endDocument () throws SAXException {
//文档解析结束
super.endDocument();
System.out.println("文档解析结束endDocument");
for(Person p: personList) {
System.out.println("personLID:" + p.getA());
}
}
public void startElement (String uri, String localName, String qName, Attributes attributes) throws SAXException {
//开始解析节点
super.startElement(uri, localName, qName, attributes);
flag = true;
if( qName.equals("person")){
person = new Person();
}
node = qName;
sb = new StringBuilder();
System.out.println("节点解析开始startElement,内容:"+sb.toString());
}
public void characters (char[] ch, int start, int length) throws SAXException {
//保存节点内容
super.characters(ch, start, length);
if(!flag) {
return;
}
sb.append(new String(ch, start, length) );
System.out.println("节点解析characters,内容:"+sb.toString());
}
public void endElement (String uri, String localName, String qName) throws SAXException {
System.out.println("endElement中,内容:");
//结束解析节点
super.endElement(uri, localName, qName);
flag = false;
if( qName.equals("person")){
personList.add(person);
}
String s = sb.toString();
switch (node) {
case "A":
person.setA(s);
break;
case "B":
person.setB(s);
break;
case "C":
person.setC(s);
break;
case "D":
person.setD(s);
break;
case "E":
person.setE(s);
break;
case "F":
person.setF(s);
break;
case "G":
person.setG(s);
break;
case "H":
person.setH(s);
break;
case "I":
person.setI(s);
break;
default:
break;
}
}
class Person{
private String A;
private String B;
private String C;
private String D;
private String E;
private String F;
private String G;
private String H;
private String I;
public String getA() {
return A;
}
public void setA(String a) {
A = a;
}
public String getB() {
return B;
}
public void setB(String b) {
B = b;
}
public String getC() {
return C;
}
public void setC(String c) {
C = c;
}
public String getD() {
return D;
}
public void setD(String d) {
D = d;
}
public String getE() {
return E;
}
public void setE(String e) {
E = e;
}
public String getF() {
return F;
}
public void setF(String f) {
F = f;
}
public String getG() {
return G;
}
public void setG(String g) {
G = g;
}
public String getH() {
return H;
}
public void setH(String h) {
H = h;
}
public String getI() {
return I;
}
public void setI(String i) {
I = i;
}
}
public static void main(String args[]) {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser;
try {
saxParser = factory.newSAXParser();
try {
saxParser.parse("C:\\Users\\Administrator\\Desktop\\a.xml", new XMLUtil());
} catch (IOException e) {
e.printStackTrace();
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
}
}
解析过程,程序执行的先后顺序:
文档解析开始startDocument
节点解析开始startElement,内容:
节点解析characters,内容:
节点解析开始startElement,内容:
节点解析characters,内容:
节点解析开始startElement,内容:
节点解析characters,内容:
节点解析开始startElement,内容:
节点解析characters,内容:10
endElement中,内容:
节点解析开始startElement,内容:
endElement中,内容:
节点解析开始startElement,内容:
节点解析characters,内容:
节点解析开始startElement,内容:
节点解析characters,内容:
节点解析开始startElement,内容:
节点解析characters,内容:
节点解析开始startElement,内容:
节点解析characters,内容:a
endElement中,内容:
节点解析开始startElement,内容:
节点解析characters,内容:b
endElement中,内容:
节点解析开始startElement,内容:
节点解析characters,内容:c
endElement中,内容:
节点解析开始startElement,内容:
节点解析characters,内容:d
endElement中,内容:
节点解析开始startElement,内容:
节点解析characters,内容:e
endElement中,内容:
节点解析开始startElement,内容:
节点解析characters,内容:f
endElement中,内容:
节点解析开始startElement,内容:
节点解析characters,内容:g
endElement中,内容:
节点解析开始startElement,内容:
节点解析characters,内容:h
endElement中,内容:
节点解析开始startElement,内容:
节点解析characters,内容:i
endElement中,内容:
endElement中,内容:
节点解析开始startElement,内容:
节点解析characters,内容:
节点解析开始startElement,内容:
节点解析characters,内容:a
endElement中,内容:
节点解析开始startElement,内容:
节点解析characters,内容:b
endElement中,内容:
节点解析开始startElement,内容:
节点解析characters,内容:c
endElement中,内容:
节点解析开始startElement,内容:
节点解析characters,内容:d
endElement中,内容:
节点解析开始startElement,内容:
节点解析characters,内容:e
endElement中,内容:
节点解析开始startElement,内容:
节点解析characters,内容:f
endElement中,内容:
节点解析开始startElement,内容:
节点解析characters,内容:g
endElement中,内容:
节点解析开始startElement,内容:
节点解析characters,内容:h
endElement中,内容:
节点解析开始startElement,内容:
节点解析characters,内容:i
endElement中,内容:
endElement中,内容:
endElement中,内容:
endElement中,内容:
endElement中,内容:
endElement中,内容:
endElement中,内容:
文档解析结束endDocument
personLID:a
personLID:a
最后来个升级版XMLUtil
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.apache.log4j.Logger;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
* SAX解析DOM工具
* @author XXX
* @time 2017年3月10日 上午11:57:08
* @version:1.0
* @param <T>
*/
public class XMLUtil<T> extends DefaultHandler{
public List<T> elementList;
private T t;
private String elementTag;
private StringBuilder sb;
private boolean flag = false;
ElementDataListener<T> elementDataListener;
private Class<T> clazz;
/**
* 要解析的最终实体,例如EMP、Person,Student
*/
private String tagModel;
private static Logger LOG = Logger.getLogger(XMLUtil.class);
private static SAXParserFactory factory;
public XMLUtil(ElementDataListener<T> empDataListener,Class<T> clazz,String tagModel) {
super();
this.elementDataListener = empDataListener;
this.clazz = clazz;
this.tagModel = tagModel;
}
public static SAXParser getSaxParser(){
SAXParserFactory factory = getFactory();
SAXParser saxParser=null;
try {
saxParser = factory.newSAXParser();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
return saxParser;
}
public void startDocument () throws SAXException {
//开始解析文档
super.startDocument();
elementList = new ArrayList<T>();
}
public void endDocument () throws SAXException {
//文档解析结束
super.endDocument();
if (elementDataListener != null) {
elementDataListener.onGetElementData(elementList);
}
}
public void startElement (String uri, String localName, String qName, Attributes attributes) throws SAXException {
//开始解析节点
super.startElement(uri, localName, qName, attributes);
flag = true;
if( qName.equals(tagModel)){
try {
t = clazz.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
LOG.error("--", e);
} catch (Exception e) {
e.printStackTrace();
LOG.error("--", e);
}
}
elementTag = qName;
sb = new StringBuilder();
}
public void characters (char[] ch, int start, int length) throws SAXException {
//保存节点内容
super.characters(ch, start, length);
if(!flag) {
return;
}
sb.append(new String(ch, start, length) );
}
public void endElement (String uri, String localName, String qName) throws SAXException {
//结束解析节点
super.endElement(uri, localName, qName);
flag = false;
if( qName.equals(tagModel)){
elementList.add(t);
}
String elementValue = sb.toString();
elementDataListener.onSetElementDataToList(elementTag,elementValue,t);
}
interface ElementDataListener<T> {
/**
* 返回解析DOM结果
* @param empList 员工信息集合
*/
public void onGetElementData(List<T> elementList);
/**
* 节点解析对应模板,switch分支来解析
* 例如:
* switch(elementTag){
* case"name":
* t.setName(elementValue);
* break;
* case"Age":
* t.setAge(elementValue);
* break;
* }
*
* @param elementTag
* @param elementValue
* @param t
*/
public void onSetElementDataToList(String elementTag,String elementValue,T t);
}
public void setEmpDataListener(ElementDataListener<T> elementDataListener) {
this.elementDataListener = elementDataListener;
}
public static SAXParserFactory getFactory() {
return (factory == null) ? SAXParserFactory.newInstance():factory;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public static void main(String args[]) {
ElementDataListener eleListener = new ElementDataListener<xxx.Person>() {
@Override
public void onSetElementDataToList(String elementTag,
String elementValue, xxx.Person t) {
switch (elementTag) {
case "A":
t.setA(elementValue);
break;
case "B":
t.setB(elementValue);
break;
//....
default:
break;
}
}
@Override
public void onGetElementData(
List<xxx.Person> elementList) {
for (xxx.Person e : elementList) {
System.out.println("打印结果: \t" + e.getCONBGN_DT());
}
}
};
try {
if (XMLUtil.getSaxParser() != null)
XMLUtil.getSaxParser().parse(
"C:\\Users\\Administrator\\Desktop\\a.xml",
new XMLUtil(eleListener,
xxx.Person.class, "person"));
} catch (SAXException | IOException e) {
e.printStackTrace();
}
}
}
本文完
参考博文:http://blog.csdn.net/zhutulang/article/details/37736407