1.首先下载jar包
2.解析xml文档
import Java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class Dom4jParseXmlDemo {
public void parseXml01(){
try {
//将src下的xml文件转换为输入流(两种方法导入)
// InputStream inputStream = new FileInputStream(new File("D:\\Workspaces\\MyEclipse 10\\Test\\src\\test.xml")) ;
InputStream inputStream = this.getClass().getResourceAsStream("/test.xml") ;
//创建SAXReader对象,读取xml
SAXReader saxReader = new SAXReader() ;
Document document = saxReader.read(inputStream) ;
Element rootElement = document.getRootElement() ;
System.out.println("根节点名称: " + rootElement.getName());
System.out.println("根节点有多少属性: " + rootElement.attributeCount());
System.out.println("根节点id属性的值: " + rootElement.attributeValue("id"));
//如果xml里面有换行,显示出来的文本也会换行
System.out.println("根节点内文本: " + rootElement.getText());
//去掉的是标签与标签之间的tab键和换行符等等,不是内容前后的空格
System.out.println("根节点内文本1: " + rootElement.getTextTrim());
//返回当前节点递归所有子节点的文本信息。
System.out.println("根节点子节点文本内容: " + rootElement.getStringValue());
Element element = rootElement.element("message") ;
if(element!=null){
System.out.println("子节点的文本: " + element.getText());
}
//修改节点名称和文本内容
rootElement.setName("root") ;
System.out.println("修改后的节点名称: " + rootElement.getName());
rootElement.setText("text") ;
System.out.println("根节点修改之后的文本: " + rootElement.getText());
}catch(DocumentException e){
e.printStackTrace() ;
}
}
public static void main(String[] args) {
Dom4jParseXmlDemo demo = new Dom4jParseXmlDemo() ;
demo.parseXml01() ;
}
}
上面的类在src下面,下面的test.xml也在src下面
<?xml version="1.0" encoding="UTF-8"?>
<style id="2">
这是文本
<message>这是信息</message>
<body>
<message>这是信息2</message>
</body>
</style>
结果为:
3.获取document文档对象
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class Dom4jParseXmlDemo {
public void parseXml01(){
try {
//将src下的xml文件转换为输入流(两种方法导入)
// InputStream inputStream = new FileInputStream(new File("D:\\Workspaces\\MyEclipse 10\\Test\\src\\test.xml")) ;
InputStream inputStream = this.getClass().getResourceAsStream("/test.xml") ;
//创建SAXReader对象,读取xml
SAXReader saxReader = new SAXReader() ;
Document document = saxReader.read(inputStream) ;
Element rootElement = document.getRootElement() ;
Iterator<Element> it = rootElement.elements("body").iterator() ;
while(it.hasNext()){
Element bodyElement = it.next() ;
Element nameElement = bodyElement.element("name") ;
System.out.println(nameElement.getName() + ":" + nameElement.getText());
Element sexElement = bodyElement.element("sex") ;
System.out.println(sexElement.getName() + ":" + sexElement.getText());
Element addressElement = bodyElement.element("address") ;
System.out.println(addressElement.getName() + ":" + addressElement.getText());
}
}catch(DocumentException e){
e.printStackTrace() ;
}
}
public static void main(String[] args) {
Dom4jParseXmlDemo demo = new Dom4jParseXmlDemo() ;
demo.parseXml01() ;
}
}
下面是对应的xml文件
<?xml version="1.0" encoding="UTF-8"?>
<style id="2">
<body>
<name>wuhaixu</name>
<sex>nan</sex>
<address>beijing</address>
</body>
</style>
结果为:
4.获取复杂的xml文档
xml文件
<?xml version="1.0" encoding="UTF-8"?>
<style id="2">
<chingcloud>问鼎清云</chingcloud>
<chingcloud id="a">
<name>mazhiyan</name>
<address>beijing</address>
<descript>网络科技有限公司</descript>
</chingcloud>
<chingcloud id="b">
<count>10</count>
<money>100</money>
<chingcloud id="1">
<name>wuhaixu</name>
<sex>nan</sex>
<age>22</age>
</chingcloud>
<chingcloud id="2">
<name>wangshihao</name>
<sex>nan</sex>
<age>20</age>
</chingcloud>
</chingcloud>
</style>
因为他们的结构不一样,直接迭代的话 会报错:
java.lang.NullPointerException
所以这个时候需要小心使用了,每次都不能把元素直接放进去迭代。具体实现代码如下:
java文件:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class Dom4jParseXmlDemo {
public void parseXml01(){
try {
//将src下的xml文件转换为输入流(两种方法导入)
// InputStream inputStream = new FileInputStream(new File("D:\\Workspaces\\MyEclipse 10\\Test\\src\\test.xml")) ;
InputStream inputStream = this.getClass().getResourceAsStream("/test.xml") ;
//创建SAXReader对象,读取xml
SAXReader saxReader = new SAXReader() ;
Document document = saxReader.read(inputStream) ;
Element rootElement = document.getRootElement() ;
if(rootElement.elements("chingcloud") != null){
@SuppressWarnings("unchecked")
List<Element> elementList = rootElement.elements("chingcloud") ;
for(Element element:elementList){
if(!element.getTextTrim().equals("")){
System.out.println("【1】" + element.getTextTrim());
}else{
if(element.attributeValue("id").equals("a")){
Element nameElement = element.element("name") ;
System.out.println(" 【2】" + nameElement.getName() + ":" + nameElement.getText());
Element addressElement = element.element("address") ;
System.out.println(" 【2】" + addressElement.getName() + ":" + addressElement.getText());
Element descriptElement = element.element("descript") ;
System.out.println(" 【2】" + descriptElement.getName() + ":" + descriptElement.getText());
}else if(element.attributeValue("id").equals("b")){
Element countElement = element.element("count") ;
System.out.println(" 【3】" + countElement.getName() + ":" + countElement.getText());
Element moneyElement = element.element("money") ;
System.out.println(" 【3】" + moneyElement.getName() + ":" + moneyElement.getText());
@SuppressWarnings("unchecked")
List<Element> subElements = element.elements("chingcloud") ;
int len = subElements.size() ;
for(int i=0;i<len;i++){
Element subElement = subElements.get(i) ;
Element nameSubElement = subElement.element("name") ;
System.out.println(" 【" + (i+4) + "】" + nameSubElement.getName() + ":" + nameSubElement.getText()) ;
Element sexSubElement = subElement.element("sex") ;
System.out.println(" 【" + (i+4) + "】" + sexSubElement.getName() + ":" + sexSubElement.getText()) ;
Element ageSubElement = subElement.element("age") ;
System.out.println(" 【" + (i+4) + "】" + ageSubElement.getName() + ":" + ageSubElement.getText());
}
}
}
}
}
}catch(DocumentException e){
e.printStackTrace() ;
}
}
public static void main(String[] args) {
Dom4jParseXmlDemo demo = new Dom4jParseXmlDemo() ;
demo.parseXml01() ;
}
}
结果为:
5生成xml文档
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;
public class Dom4jBuildXmlDemo {
public void build01(){
Document document = DocumentHelper.createDocument() ;
Element rootElement = document.addElement("type") ;
rootElement.setText("这是type的文本信息") ;
Element subTypeElement = rootElement.addElement("subType") ;
Element nameElement = subTypeElement.addElement("name") ;
Element ageElement = subTypeElement.addElement("age") ;
Element sexElement = subTypeElement.addElement("sex") ;
nameElement.setText("张三") ;
ageElement.setText("22") ;
sexElement.setText("男") ;
nameElement.addAttribute("language", "java") ;
ageElement.addAttribute("language", "c#") ;
sexElement.addAttribute("language", "PHP") ;
System.out.println(document.asXML());
try {
Writer fileWriter = new FileWriter("d:\\test.xml") ;
XMLWriter xmlWriter = new XMLWriter(fileWriter) ;
xmlWriter.write(document) ;
xmlWriter.flush() ;
xmlWriter.close() ;
System.out.println("xml文档添加成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Dom4jBuildXmlDemo demo = new Dom4jBuildXmlDemo() ;
demo.build01() ;
}
}
结果为: