<?xml version="1.0" encoding="GBK"?>
<books>
<!--—An XML Example--><?target text?>
<bookone title="XML study" publisher="angellove workers">
<article level="high" date="December-2007">
<title>Java configuration with XML</title>
<author>
<firstname>angel</firstname>
<lastname>free</lastname>
</author>
</article>
</bookone>
</books>
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.io.OutputFormat;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;
import java.io.*;
import org.dom4j.Attribute;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import java.util.List;
import java.util.Iterator;
import org.dom4j.QName;
public class dom4j {
public void readAttribute(String filename, String xpath, String attribute) {
SAXReader saxReader = new SAXReader();
try {
Document document = saxReader.read(new File(filename));
List list = document.selectNodes(xpath);
Iterator iter = list.iterator();
while (iter.hasNext()) {
Element element = (Element) iter.next();
}
} catch (DocumentException e) {
e.printStackTrace();
}
}
public static void main(String[] argv) {
dom4j ptree = new dom4j();
ptree.modifyXML();
ptree.mymodifXML(new File("books.xml.xml") );
}
// 创建一个xml文件
public void changeXML() {
Document document = DocumentHelper.createDocument();// 创建一个xml document对象
Element books = document.addElement("books");// 创建根节点元素
// 给books添加注释
books.addComment("—An XML Example");
// 使用addProcessingInstruction()方法增加一个处理指令
books.addProcessingInstruction("target", "text");
// 在当前元素后增加一个子元素
Element bookone = books.addElement("bookone");
// 设置当前元素的属性
bookone.addAttribute("title", "XML study");
bookone.addAttribute("publisher", "angellove workers");
Element article = bookone.addElement("article");
article.addAttribute("level", "high");
article.addAttribute("date", "December-2007");
Element titleElement = article.addElement("title");
// 设置当前元素的文本值,即是内容
titleElement.setText("Java configuration with XML");
Element authorElement = article.addElement("author");
Element firstNameElement = authorElement.addElement("firstname");
firstNameElement.setText("angel");
Element lastNameElement = authorElement.addElement("lastname");
lastNameElement.setText("free");
// XML 声明 <?xml version="1.0" encoding="UTF-8"?> 自动添加到 XML 文档中。
try {
// 创建一个xml写入流,将生成的xml文件写入指定的文件中
// 使用优雅的方式写入(一共有三种方式:普通方式,优雅方式和紧凑方式)
//紧凑方式写入format = OutputFormat.createCompactFormat();
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("GBK");
XMLWriter output = new XMLWriter(
new FileWriter(new File("books.xml")), format);
output.write(document);
output.close();
}
catch (IOException e) {
System.out.println(e.getMessage());
}
}
// 使用XPath表达式的查询
public void modifXML(File inputXml) {
try {//创建一个读取xml文件的对象
SAXReader saxReader = new SAXReader();//读取并获得xml文档
Document document = saxReader.read(inputXml);
//使用XPATH表达式从article元素中获得level节点列表
List list = document.selectNodes("//article/@level");
//创建一个迭代器.
Iterator iter = list.iterator();
while (iter.hasNext()) {
//获得level节点的属性
Attribute attribute = (Attribute) iter.next();
if (attribute.getValue().equals("high"))
//设置属性的值
attribute.setValue("low");
}
list = document.selectNodes("//article/@date");
iter = list.iterator();
while (iter.hasNext()) {
Attribute attribute = (Attribute) iter.next();
if (attribute.getValue().equals("December-2007"))
attribute.setValue("October-2006");
}
list = document.selectNodes("//article");
iter = list.iterator();
while (iter.hasNext()) {
Element element = (Element) iter.next();
Iterator iterator = element.elementIterator("title");
while (iterator.hasNext()) {
Element titleElement = (Element) iterator.next();
//获得属性的文本.
if (titleElement.getText().equals("Java configuration with XML"))
//重新设置属性的文本
titleElement.setText("good good study,day day up");
}
}
list = document.selectNodes("//article/author");
iter = list.iterator();
while (iter.hasNext()) {
Element element = (Element) iter.next();
Iterator iterator = element.elementIterator("firstname");
while (iterator.hasNext()) {
Element firstNameElement = (Element) iterator.next();
if (firstNameElement.getText().equals("angel"))
firstNameElement.setText("god");
}
}
list = document.selectNodes("//article/author");
iter = list.iterator();
while (iter.hasNext()) {
Element element = (Element) iter.next();
Iterator iterator = element.elementIterator("lastname");
while (iterator.hasNext()) {
Element lastNameElement = (Element) iterator.next();
if (lastNameElement.getText().equals("free"))
lastNameElement.setText("dark");
}
}
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("GBK");
XMLWriter output = new XMLWriter(new FileWriter(new File("modifyBooks.xml")), format);
output.write(document);
output.close();
}catch (DocumentException e) {
}catch (IOException e) {
}
}
//访问其中某一特定节点
public void modifyXML() {
try {
SAXReader sreader = new SAXReader();
Document document = sreader.read(new File("E:\\eagle\\jms\\exc1.xml")); // E:\\eagle\\jms\\exc1.xml
Element element = document.getRootElement();
//获得根元素的名字
String rootname = element.getName();
String textname = element.getText();
System.out.println("rootname=" + rootname);
//获得指定QName,prefix,uri
QName qname = element.getQName();
//获得当前元素指定属性的值
List list = element.elements();
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
Element node = (Element) iterator.next();
if ("InfStatus1".equals(node.attributeValue("RequestObject"))) {
//重新设置当前元素的名字
node.setName("InfEXC");
}
if ("Head".equals(node.getName())) {
node.setName("Head");
} else {
iterator = node.elementIterator();
}
}
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("utf-8");
XMLWriter output = new XMLWriter(new FileWriter(new File("E:\\eagle\\jms\\modifyExc.xml")), format);
output.write(document);
output.close();
} catch (DocumentException e) {
} catch (IOException e) {
}
}
}