package saxExample.
import java.util.HashMap. import org.xml.sax.*. import org.xml.sax.helpers.DefaultHandler.
/** * 继承DefaultHandler类,用SAX实现对xml的遍历 * @author cuiweibing * @since 2007.8.8 */
public class SAXHandler extends DefaultHandler { //存放所有的节点(这里的节点等于原来的节点 编号)以及它所对应的值 private HashMap hashMap = new HashMap(). //目前的节点 private String currentElement = null. //目前节点所对应的值 private String currentValue = null. //用于节点编号(具体到person) private static int i=-1. public HashMap getHashMap() { return hashMap. } public void characters(char[] ch, int start, int length) throws SAXException { //取出目前节点对应的值 currentValue = new String(ch, start, length). } public void startElement(String uri, String localName, String qName, Attributes attr) throws SAXException { if(qName.equalsIgnoreCase("student")){ //currentElement= "". }else if (qName.equalsIgnoreCase("person")){ i . //currentElement= "". String age=attr.getValue("age"). if(age!=null){ hashMap.put(qName "-age" i, age). }else{ hashMap.put(qName "-age" i, "20"). } }else if (qName.equalsIgnoreCase("college")){ currentElement= qName. String leader=attr.getValue("leader"). if(leader!=null){ hashMap.put(qName "-leader" i, leader). }else{ hashMap.put(qName "-leader" i, "leader"). } }else{ currentElement= qName. } } public void endElement(String uri, String localName, String qName) throws SAXException { if (qName.equalsIgnoreCase("student")){ // hashMap.put(currentElement, currentValue). }else if (qName.equalsIgnoreCase("person")){ }else{ currentElement =i. hashMap.put(currentElement, currentValue). } } }
|