?? saxdataset.java
字號:
package com;
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.Date;
import java.util.List;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
//使用DefaultHandler的好處 是 不必陳列出所有方法,
public class SaxDataSet extends DefaultHandler {
private StringBuffer buf;
private PropertyUtil pc;
private Object obj;
private List result;
private final String root = "xmlbody";
private Date start = new Date();
public SaxDataSet() {
super();
}
public SaxDataSet(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());
long tmp = new Date().getTime() - start.getTime();
System.out.println("sax耗時:" + tmp + " ms");
}
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) {
buf.delete(0, buf.length());
/*
* for (int i = 0; i < atts.getLength(); i++) { System.out.println("元素名" +
* atts.getLocalName(i) + "屬性值" + atts.getValue(i)); }
*/
}
public void endElement(String namespaceURI, String localName, String qName)
throws SAXException {
//System.out.println(qName);
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);
ServletInputStream in = request.getInputStream();
sp.parse(in, this);
return this.getResult();
} catch (IOException e) {
throw new SysException("SYS-0016", "SYS", "網絡繁忙,請稍后再試!", this
.getClass().getName()
+ ":objectToObject()", null, e);
} catch (SAXException e) {
e.printStackTrace();
throw new SysException("SYS-0016", "SYS", "網絡繁忙,請稍后再試!", this
.getClass().getName()
+ ":objectToObject()", null, e);
} catch (Exception e) {
throw new SysException("SYS-0016", "SYS", "網絡繁忙,請稍后再試!", this
.getClass().getName()
+ ":objectToObject()", null, e);
}
}
public static void main(String args[]) {
try {
// SAXParserFactory sf = SAXParserFactory.newInstance();
// SAXParser sp = sf.newSAXParser();
SaxDataSet testsax = new SaxDataSet(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 + -