xml:在src目录下名字是color.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<colors>
<color>
<code>660000</code>
<name>黄色</name>
<like>0</like>
</color>
<color>
<code>888888</code>
<name>no</name>
<like>1</like>
</color>
</colors>
用的是DefaultHandler,这个用的比较多,不用其他的handler组装,相对简单,尽管比dom难些
package xmlParser;
import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class SAXDefaultHandler extends DefaultHandler {
/**
* 要点:qName和localName,比如说sina:blog
* qName是sina:blog
* localName是blog
* 有的把localName当做qName了,所以是null,也怪不得一直出bug
*/
private List<Color> list;
private Color color;
private String elementTag=null;
private String data;
@Override
public void startDocument() throws SAXException {
System.out.println("startDocument");
list=new ArrayList<Color>();
}
@Override
public void endDocument() throws SAXException {
System.out.println("endDocument");
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
System.out.println("startElement");
if(qName.equals("color")){
color=new Color();
//attributes.getValue(0);//可以获取属性值
}
elementTag=qName;
System.out.println("qName="+qName);
System.out.println("localName="+localName);
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
System.out.println("characters");
data=new String(ch, start, length);
System.out.println("data="+data);
//这个也是关键,data有时候没有数值,什么都不打印
if(data!=null&&color!=null&&!data.equals("")&&elementTag!=null){
System.out.println("elementTag="+elementTag);
if(elementTag.equals("code")){
color.setCode(data);
}else if(elementTag.equals("name")){
color.setName(data);
}else if(elementTag.equals("like")){
color.setLike(data);
}
}
elementTag=null;
data=null;
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
System.out.println("endElement");
if(qName.equals("color")){
list.add(color);
color=null;
}
}
public List<Color> getColorList(){
System.out.println("getcolorList");
return this.list;
}
}
用到的Color类:
package xmlParser;
public class Color {
String code;
String name;
String like;
public Color() {
}
public Color(String code, String name, String like) {
this.code = code;
this.name = name;
this.like = like;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLike() {
return like;
}
public void setLike(String like) {
this.like = like;
}
@Override
public String toString() {
return this.code+this.name+this.like;
}
}
在main里面调用:
public class WriteXML {
private static DocumentBuilderFactory factory;
private static DocumentBuilder builder;
private static Document document;
private static TransformerFactory tff;
private static Transformer tf;
public static void main(String[] args) throws IOException {
initToolClass();
// addColor("888888", "no", "1");
// deleteColor("006600");
// updateColor("660000", "黄色", "0");
List<Color> list = readAllColor();
if (list == null) {
sop("list=null");
} else {
sop("list!=null");
}
for(Color c:list){
sop(c.toString());
}
}
public static void initToolClass() {
try {
factory = DocumentBuilderFactory.newInstance();
builder = factory.newDocumentBuilder();
document = builder.parse("src/color.xml");
tff = TransformerFactory.newInstance();
tf = tff.newTransformer();
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
}
public static List<Color> readAllColor() {
SAXParserFactory factory = SAXParserFactory.newInstance();
List<Color> list = null;
try {
SAXParser parser = factory.newSAXParser();
InputStream in = new FileInputStream("src/color.xml");
SAXDefaultHandler handler = new SAXDefaultHandler();
parser.parse(in, handler);
list = handler.getColorList();
} catch (ParserConfigurationException | SAXException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
源码下载链接
http://download.csdn.net/detail/u014449046/9133745,里面还包含了dom的增删改