使用SAX解析XMl通用哟
[java]package com.popo.xml;
import java.io.IOException;
import java.io.StringBufferInputStream;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Set;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.dom4j.io.DOMReader;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class SAXTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SAXParserFactory sf = SAXParserFactory.newInstance();
MyDefaultHandler df = new MyDefaultHandler();
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<data>"+
"<grade gredenum='gd1'>" +
" <class classnum='cl1'>" +
" <std name='xiaohong' age='16' />" +
" <std name='xiaoming' age='18' > =========================================================</std>" +
" <teacher name='tch_li' cource='chemistry'/>" +
" <teacher name='tch_jia' cource='physics'/>"+
" </class> " +
"</grade >"+
"<grade gredenum='gd2'>" +
" <class classnum='cl1'>" +
" <std name='monkeysun' age='530' />" +
" <std name='bajiepig' age='360' />"+
" <std name='cleanwu' age='320' />"+
" </class> " +
" <class classnum='cl2'>" +
"aafafafa" +
"</class>" +
"</grade >"+
"</data>"
;
/**
* "<grade n='2'>" +
" <class n=1>" +
" <std name='2l' age='22' />" +
" <std name='23' age='23' />"+
" </class> " +
" <class n=2>" +
" <std name='2l' age='22' />" +
" <std name='23' age='23' />"+
" </class>"+
"</grade >" +
*/
try {
SAXParser sp = sf.newSAXParser();
StringBufferInputStream in = new StringBufferInputStream(xml);
sp.parse(in, df);
System.out.println();
Data data = df.root;
System.out.println(data);
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Au
补充:Web开发 , 其他 ,