30、编程用java解析xml的方式
用sax方式解析xml,xml如下:
小明
信息学院
6258113
男,1955,博士,94年调入海南大学
事件回调类SAXHandler.java
import java.io.*; import java.util.Hashtable; import org.xml.sax.*; public class SAXHandler extends HandlerBase { private Hashtable table = new Hashtable(); private String currentElement = null; private String currentValue = null; public void setTable(Hashtable table) { this.table = table; } public Hashtable getTable() { return table; } public void startElement(String tag, AttributeList attrs) throws SAXException { currentElement = tag; } public void characters(char[] ch, int start, int length) throws SAXException { currentValue = new String(ch, start, length); } public void endElement(String name) throws SAXException { if (currentElement.equals(name)) table.put(currentElement, currentValue); } } |
延伸阅读
文章来源于领测软件测试网 https://www.ltesting.net/