第一:基本概念
1.第三种解析方式不是java官方提供的,而是第三方提供的,因此需要引入外部jar包。
因此dom4j要用的系统类都在外部jar包中,需要引入(前两种解析都是java官方的,系统类都在JRE System Libaray中)
2.解析的本质就是找到xml文件里面的内容饿锤biu的
3.dom4j的含义是从dom for java 意思是从dom解析到java。
dom解析把空格也当成节点,sax解析不能修改(增删改)文件。而dom4j解决这两个的缺点
4.Saxread是一个类(读),使用这个类创建一个Document对象,这个document是个接口,因此是创建了这个document接口的实现类(多肽)
通过document获得Element对象,Element也是个接口。
XmlWrite是写(输出),会用到outputFormat类,用于创建文档的时候规定格式。Saxread和XmlWrite是dom4j的主要类
5.DocumentHelp是个类,里面的方法都是静态的
6.dom4j能够改变物理文件,而以前的解析只是改变内存中的dom树
7.pcdata表示内容按照字符串进行解析。p是parse解析的意思,c是char是字符的意思
cdata:<![CDATA[]]>:按照文本解析,也就是按照普通字符串输出。也就是逃避解析
hot:fasle表示cdata的值是false
第二:读xml
package dom4jTest;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class TestRead {
public static void main(String[] args) {
SAXReader sr=new SAXReader();//创建dom4j的解析器
try{
Document doc=sr.read("src/book.xml");
Element rootEle=doc.getRootElement();//找到跟元素、
List<Element> elelist=rootEle.elements();//找到所有子元素,elelsit是个集合
Iterator<Element> it= elelist.iterator();//创建一个elelist的迭代器
while(it.hasNext()){
Element bookEle=it.next();
String name=bookEle.element("name").getTextTrim();//取出文本内容
String isbn=bookEle.attributeValue("isbn");//取出属性值
String author=bookEle.element("author").getTextTrim();
String price=bookEle.element("price").getTextTrim();
String pub=bookEle.element("publisher").getText();
System.out.println(name+"\t"+isbn+"\t"+author+"\t"+price+"\t"+pub);
}
}catch(Exception ex){
ex.printStackTrace();
}
}
}
第三:写xml
package dom4jTest;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Text;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class TestWrite {
public static void main(String[] args) {
//第一步:创建元素
//创建节点
Element bookEle=DocumentHelper.createElement("book");
Attribute a = DocumentHelper.createAttribute(bookEle, "isbn", "3333");
///DocumentHelper.createA
bookEle.add(a);
//创建几个子节点
Element nameEle=DocumentHelper.createElement("name");
Element authorEle=DocumentHelper.createElement("author");
Element priceEle=DocumentHelper.createElement("price");
Element pubEle=DocumentHelper.createElement("publisher");
//创建几个文本节点
Text nameText=DocumentHelper.createText("三毛流浪记");
Text authorText=DocumentHelper.createText("三毛");
Text priceText=DocumentHelper.createText("222");
//Text pubText=DocumentHelper.createText("出版社");
pubEle.setText("出版社");
//第二步:添加元素,把子元素添加到父元素中
//对上面常见的东西进行组装
nameEle.add(nameText);
authorEle.add(authorText);
priceEle.add(priceText);
//pubEle.add(pubText);
bookEle.add(nameEle);
bookEle.add(authorEle);
bookEle.add(priceEle);
bookEle.add(pubEle);
//第三步:输出(写入)元素:把刚才添加的元素在xml文件中进行显示输出
try{
SAXReader sr=new SAXReader();
Document doc=sr.read("src/book.xml");
Element rootEle=doc.getRootElement();
rootEle.add(bookEle);
OutputStream os=new FileOutputStream("src/book.xml");//要输出的文件
OutputFormat of=new OutputFormat();//要输出的格式
of.setNewlines(true);//换行
of.setIndent(true);//缩进
//of.setEncoding("GBK");
XMLWriter xw = new XMLWriter(os,of);
xw.write(doc);
xw.flush();
}catch(Exception ex){
ex.printStackTrace();
}
}
}
第四:dom4j增删改查过程
package homework;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
//dom4j完成增删改查
public class Dom2 {
public void add(){
SAXReader sr = new SAXReader();
try{
Document doc=sr.read(new File("src/book.xml"));
Element root=doc.getRootElement();
Element book=root.addElement("book");
List<Element> b=root.elements();
book.addAttribute("isbn", "11011"+b.size());
Element name=book.addElement("name");
Element author=book.addElement("author");
Element price=book.addElement("price");
Element publisher=book.addElement("publisher");
name.setText("小二流浪记");
author.setText("小二");
price.setText("1");
publisher.setText("天津出版社");
//写
OutputStream os= new FileOutputStream("src/book.xml");
OutputFormat of=new OutputFormat();
of.setNewlines(true);
of.setIndent(true);
of.setIndent("\t");
XMLWriter write = new XMLWriter(os,of);//往os文件里写,写的格式是of
write.write(doc);//写入document
write.close();
}catch (Exception e) {
e.printStackTrace();
}
}
public void search() {
SAXReader sr = new SAXReader();
//XMLWriter write = new XMLWriter();
try{
Document doc=sr.read(new File("src/book.xml"));
Element rootEle=doc.getRootElement();//bookes
List<Element> elelist=rootEle.elements();//book
Iterator<Element> it = elelist.iterator();
while(it.hasNext()){
Element bookEle=it.next();
String name=bookEle.element("name").getTextTrim();//取出文本内容
String isbn=bookEle.attributeValue("isbn");//取出属性值
String author=bookEle.element("author").getTextTrim();
String price=bookEle.element("price").getTextTrim();
String pub=bookEle.element("publisher").getText();
System.out.println(name+"\t"+isbn+"\t"+author+"\t"+price+"\t"+pub);
}
}catch(Exception ex){
ex.printStackTrace();
}
}
public void del(){//可传入参数修改指定对象的属性
SAXReader sr = new SAXReader();
//XMLWriter write = new XMLWriter();
try{
Document doc=sr.read(new File("src/book.xml"));
Element rootEle=doc.getRootElement();//bookes
List<Element> elelist=rootEle.elements();//book
for(int x=0;x<elelist.size();x++){
if(x>1){
Element ele=elelist.get(x);
//System.out.println("最后一个是"+ele.getName());
boolean falg = rootEle.remove(ele);
}
}
OutputStream os= new FileOutputStream("src/book.xml");
OutputFormat of=new OutputFormat();
of.setNewlines(true);
of.setIndent(true);
of.setIndent("\t");
XMLWriter write = new XMLWriter(os,of);//往os文件里写,写的格式是of
write.write(doc);//写入document
write.close();
this.search();
}catch(Exception ex){
ex.printStackTrace();
}
}
/* public void update(){//可传入参数。。。。
SAXReader sr = new SAXReader();
//XMLWriter write = new XMLWriter();
try{
Document doc=sr.read(new File("src/book.xml"));
Element rootEle=doc.getRootElement();//bookes
List<Element> elelist=rootEle.elements();//book
for(int x=0;x<elelist.size();x++){
if(x==0){
Element ele=elelist.get(x);
ele.element("name").setText("修改");
}
}
OutputStream os= new FileOutputStream("src/book.xml");
OutputFormat of=new OutputFormat();
of.setNewlines(true);
of.setIndent(true);
of.setIndent("\t");
XMLWriter write = new XMLWriter(os,of);//往os文件里写,写的格式是of
write.write(doc);//写入document
write.close();
}catch(Exception ex){
ex.printStackTrace();
}
}*/
public void update(String id,String value){
SAXReader sr = new SAXReader();
//XMLWriter write = new XMLWriter();
try{
Document doc=sr.read(new File("src/book.xml"));
Element rootEle=doc.getRootElement();//bookes
List<Element> elelist=rootEle.elements();//book
for(int x=0;x<elelist.size();x++){
Element ele=elelist.get(x);
if(ele.attributeValue("isbn").equals(id)){
ele.element("name").setText(value);
}
}
OutputStream os= new FileOutputStream("src/book.xml");
OutputFormat of=new OutputFormat();
of.setNewlines(true);
of.setIndent(true);
of.setIndent("\t");
XMLWriter write = new XMLWriter(os,of);//往os文件里写,写的格式是of
write.write(doc);//写入document
write.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
public static void main(String[] args) {
Dom2 d= new Dom2();
//d.add();
//d.del();
//d.search();
//d.update();
d.update("100100","修改操作");
}
}
第五:把jdbc的四大参数封装到xml里
public class JdbcXml {
public static void main(String[] args) {
//Element pro=DocumentHelper.createElement("pro");
Document doc=DocumentHelper.createDocument();
Element pro=doc.addElement("pro");
Element d=pro.addElement("driver");
Element u=pro.addElement("url");
Element um=pro.addElement("uname");
Element p=pro.addElement("pwd");
d.setText("oracle.jdbc.driver.OracleDriver");
u.setText("jdbc:oracle:thin:@localhost:1521:ORCL");
um.setText("chu1");
p.setText("1234");
try{
OutputStream os=new FileOutputStream("src/a.xml");
OutputFormat of=new OutputFormat();
of.setNewlines(true);
of.setIndent(true);
//OutputFormat of=OutputFormat.createPrettyPrint();
of.setEncoding("GBK");//默认编码格式是utf8
XMLWriter write = new XMLWriter(os,of);
write.write(doc);
write.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
}