package com.xml.csdn;
import java.io.File;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
public class SAXTest1 {
public static void main(String[] args) throws Exception, SAXException {
SAXParserFactory factory=SAXParserFactory.newInstance();//得到解析器工厂
SAXParser sp=factory.newSAXParser();//得到解析器
/*XMLReader sreader=sp.getXMLReader(); //得到读取器
sreader.setContentHandler(new shuiguohandler()); //设置ContentHandler
sreader.parse("src/shuiguo.xml"); //设置解析文件
*/
sp.parse(new File("src/TestName.xml"), new shuiguohandler1());
}
}
class shuiguohandler1 extends DefaultHandler{
private boolean isok;
private int count=0;
private int pos=2;
public void characters(char[] ch, int start, int length)
throws SAXException {
super.characters(ch, start, length);
if(isok&&count==pos){
System.out.println(" characters()解析内容:"+new String(ch,start,length));}
}
public void endElement(String uri, String localName, String qName)
throws SAXException {
super.endElement(uri, localName, qName);
if(qName.equals("梨")){
if(count==pos){
System.out.println("endElement()结束元素:"+qName);}
isok=false;
}
}
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
super.startElement(uri, localName, qName, attributes);
if(qName.equals("梨")){
isok=true;
count++;
if(count==pos){
System.out.println("startElement()开始元素:"+qName);
}
}
}
}
package com.xml.csdn;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class DomTest3 {
public static void main(String[] args) throws Exception {
SAXReader reader = new SAXReader();
Document doc = reader.read(new File("src/TestName.xml")); // 得到文档的dom模型
Element root = doc.getRootElement(); // 获取文档的根节点
// System.out.println(root.getName()+root.getText()+root.getNodeTypeName());
/*
* Element apple=((Element)(root.elements("水果").get(1))).element("苹果");
* //用element()或elements()获取自己想要的节点 System.out.println(apple.getText());
*/
//addTest(root, doc);
//updateTest(root,doc);
//deleteTest(root,doc);
listTest(root);
}
public static void listTest(Element root) {
System.out.println(root.getName());
List<Element> list=root.elements();
for(Element child:list){
listTest(child);
}
}
public static void deleteTest(Element root, Document doc) {
Element grape=root.element("水果").element("葡萄");
grape.getParent().remove(grape);
writerFile(doc);
}
public static void updateTest(Element root, Document doc) {
Element grape=root.element("水果").element("葡萄");
grape.setText("葡萄");
writerFile(doc);
}
public static void addTest(Element root, Document doc){
List list = root.element("水果").elements(); // 找到要添加节点的位置
Element grape = DocumentHelper.createElement("葡萄"); // 创建要添加的元素
grape.setText("grapegrapegrape"); // 设置元素内容
list.add(0, grape); // 将元素添加到指定位置中
writerFile(doc);
}
public static void writerFile(Document doc) {
//不设置字符集的写入方法(建议全部是英文字母的文档使用)
/* try {
XMLWriter writer=new XMLWriter(new FileWriter("src/shuiguo.xml"));//创建xmlwriter对象
writer.write(doc); //利用writer对象的write(document)方法写入xml文档
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}*/
//设置字符集,(有中文的文档使用)
OutputFormat format=OutputFormat.createPrettyPrint();
format.setEncoding("gb2312");
try {
XMLWriter writer=new XMLWriter(new FileWriter("src/TestName.xml"),format);//创建xmlwriter对象
writer.write(doc); //利用writer对象的write(document)方法写入xml文档
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}