2007年10月6日星期六

使用Java自带SAX工具解析XML | 站长资讯

studentInfo.xml


%26lt;?xml version=%26quot;1.0%26quot; encoding=%26quot;gb2312%26quot;?%26gt;
%26lt;student%26gt;
%26lt;person age=%26quot;25%26quot;%26gt;%26lt;!--Èç¹ûûÓÐageÊôÐÔ£¬Ä¬ÈϵÄΪ20--%26gt;
%26lt;name%26gt;´ÞÎÀ±ø%26lt;/name%26gt;
%26lt;college%26gt;PCѧԺ%26lt;/college%26gt;
%26lt;telephone%26gt;62354666%26lt;/telephone%26gt;
%26lt;notes%26gt;ÄÐ,1982ÄêÉú,˶ʿ£¬Ï־ͶÁÓÚ±±¾©Óʵç´óѧ%26lt;/notes%26gt;
%26lt;/person%26gt;
%26lt;person%26gt;
%26lt;name%26gt;cwb%26lt;/name%26gt;
%26lt;college leader=%26quot;leader1%26quot;%26gt;PCѧԺ%26lt;/college%26gt;%26lt;!--Èç¹ûûÓÐleaderÊôÐÔ£¬Ä¬

ÈϵÄΪleader--%26gt;
%26lt;telephone%26gt;62358888%26lt;/telephone%26gt;
%26lt;notes%26gt;ÄÐ,1987ÄêÉú,˶ʿ£¬Ï־ͶÁÓÚÖйúũҵ´óѧ%26lt;/notes%26gt;
%26lt;/person%26gt;
%26lt;person age=%26quot;45%26quot;%26gt;
%26lt;name%26gt;xxxxx%26lt;/name%26gt;
%26lt;college leader=%26quot;ѧԺÁìµ¼%26quot;%26gt;xxxѧԺ%26lt;/college%26gt;
%26lt;telephone%26gt;66666666%26lt;/telephone%26gt;
%26lt;notes%26gt;×¢ÊÓÖУ¬×¢ÊÍÖÐ%26lt;/notes%26gt;
%26lt;/person%26gt;
%26lt;/student%26gt;

SAXHandler.java

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%26lt;String,String%26gt; hashMap = new HashMap%26lt;String,String%26gt;();
//ĿǰµÄ½Úµã
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(%26quot;student%26quot;)){
//currentElement= %26quot;%26quot;;
}else if (qName.equalsIgnoreCase(%26quot;person%26quot;)){
i++;
//currentElement= %26quot;%26quot;;
String age=attr.getValue(%26quot;age%26quot;);
if(age!=null){
hashMap.put(qName+%26quot;-age%26quot;+i, age);
}else{
hashMap.put(qName+%26quot;-age%26quot;+i, %26quot;20%26quot;);
}
}else if (qName.equalsIgnoreCase(%26quot;college%26quot;)){
currentElement= qName;
String leader=attr.getValue(%26quot;leader%26quot;);
if(leader!=null){
hashMap.put(qName+%26quot;-leader%26quot;+i, leader);
}else{
hashMap.put(qName+%26quot;-leader%26quot;+i, %26quot;leader%26quot;);
}
}else{
currentElement= qName;
}

}

public void endElement(String uri, String localName, String qName) throws SAXException {
if (qName.equalsIgnoreCase(%26quot;student%26quot;)){
// hashMap.put(currentElement, currentValue);
}else if (qName.equalsIgnoreCase(%26quot;person%26quot;)){

}else{
currentElement+=i;
hashMap.put(currentElement, currentValue);
}
}
}

TestSAXHandler.java

package saxExample;

import java.io.File;
import java.util.HashMap;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;

/**
* ½âÎöÖ÷Àà
* @author cuiweibing
* @since 2007.8.8
*/


public class TestSAXHandler {
public TestSAXHandler() {
}

public static void main(String[] args) {
try{
//³õʼ»¯Óë½âÎö
SAXHandler handler = new SAXHandler();
SAXParserFactory saxparserfactory = SAXParserFactory.newInstance();
SAXParser saxparser = saxparserfactory.newSAXParser();
saxparser.parse(new File(%26quot;studentInfo.xml%26quot;), handler);

//½âÎöÍêºó»ñÈ¡½âÎöÐÅÏ¢
HashMap hashMap = handler.getHashMap();
System.out.println(%26quot;ÐÕÃû\tÄêÁä\tѧԺ\tѧԺÁìµ¼\tµç»°\t\t±¸×¢%26quot;);
for(int i=0;i%26lt;hashMap.size();i+=6){
int j=i/6;
System.out.print(hashMap.get(%26quot;name%26quot;+j)+%26quot;\t%26quot;);
System.out.print(hashMap.get(%26quot;person-age%26quot;+j)+%26quot;\t%26quot;);
System.out.print(hashMap.get(%26quot;college%26quot;+j)+%26quot;\t%26quot;);
System.out.print(hashMap.get(%26quot;college-leader%26quot;+j)+%26quot;\t%26quot;);
System.out.print(hashMap.get(%26quot;telephone%26quot;+j)+%26quot;\t%26quot;);
System.out.println(hashMap.get(%26quot;notes%26quot;+j)+%26quot;\t%26quot;);
}
}catch(Exception ex){
ex.printStackTrace();
}
}
}

没有评论: