📄 testsax.java
字号:
package com.xml;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.Locator;
import org.xml.sax.InputSource;
import org.xml.sax.helpers.DefaultHandler;
import com.PropertyUtil;
import com.SysException;
import com.User;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
class TestSAX extends DefaultHandler {
private StringBuffer buf;
private PropertyUtil pc;
private Object obj;
private List result;
private final String root = "xmlbody";
public TestSAX() {
super();
}
public TestSAX(Object obj) {
super();
// TODO Auto-generated constructor stub
this.obj = obj;
pc = new PropertyUtil();
}
public void setDocumentLocator(Locator locator) {
}
public void startDocument() throws SAXException {
buf = new StringBuffer();
result = new ArrayList();
// System.out.println("*******开始解析文档*******");
}
public void endDocument() throws SAXException {
// System.out.println("*******文档解析结束*******");
//System.out.println("size:"+this.result.size());
}
public void startPrefixMapping(String prefix, String uri) {
System.out.println(" 前缀映射: " + prefix + " 开始!" + " 它的URI是:" + uri);
}
public void endPrefixMapping(String prefix) {
System.out.println(" 前缀映射: " + prefix + " 结束!");
}
public void processingInstruction(String target, String instruction)
throws SAXException {
}
public void ignorableWhitespace(char[] chars, int start, int length)
throws SAXException {
}
public void skippedEntity(String name) throws SAXException {
}
public void startElement(String namespaceURI, String localName,
String qName, Attributes atts) {
System.out.println("namespaceURI = " + namespaceURI +"; localName = " + localName + "; qName = " + qName);
buf.delete(0, buf.length());
//for (int i = 0; i < atts.getLength(); i++) { System.out.println("元素名" +
//atts.getQName(i) + "属性值" + atts.getValue(i)); }
}
public void endElement(String namespaceURI, String localName, String qName)
throws SAXException {
try {
String className = "";
if (this.obj != null) {
className = pc.getClassName(obj.getClass());
}
if (!qName.equals(this.root)) {
if (qName.equals(className)) {
System.out.println();
this.result.add(obj);
//pc.toString(obj);
} else {
pc.setValue(obj,qName,buf.toString());
}
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public void characters(char[] chars, int start, int length)
throws SAXException {
// 将元素内容累加到StringBuffer中
buf.append(chars, start, length);
}
public List copy(HttpServletRequest request)
throws SysException {
try {
SAXParserFactory sf = SAXParserFactory.newInstance();
SAXParser sp = sf.newSAXParser();
//TestSAX testsax = new TestSAX(new User(),null);
sp.parse(new InputSource("D:\\user.xml"), this);
//sp.parse(request.getInputStream(),this);
return this.getResult();
} catch (IOException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return new ArrayList();
}
public static void main(String args[]) {
try {
//SAXParserFactory sf = SAXParserFactory.newInstance();
//SAXParser sp = sf.newSAXParser();
TestSAX testsax = new TestSAX(new User());
//sp.parse(new InputSource("D:\\user.xml"), testsax);
testsax.copy(null);
} catch (Exception e) {
e.printStackTrace();
}
}
public List getResult() {
return result;
}
public void setResult(List result) {
this.result = result;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -