Maven Repository-Dom4J:https://mvnrepository.com/artifact/dom4j/dom4j
Maven Repository-Jaxen:https://mvnrepository.com/artifact/jaxen/jaxen
<车辆清单>
<汽车>
<车牌 生产时间="2019">奥迪车牌>
<产地>北京产地>
汽车>
<汽车>
<车牌 生产时间="2020">奔驰车牌>
<产地>上海产地>
汽车>
车辆清单>
package com.example.test;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.junit.Test;
import java.io.*;
import java.util.List;
public class Dom4jParseTest {
private File file = new File(this.getClass().getClassLoader().getResource("car.xml").getPath());
@Test
public void dom4jRetrieve(){
try {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(file);
//显示根元素的名称
Element rootElement = document.getRootElement();
System.out.println(rootElement.getName());
//取得根元素下的所有子元素
List elements = rootElement.elements();
System.out.println("根元素下子元素数量:" + elements.size());
for (Element element:elements){
//取得子元素的文本
System.out.println("车牌:" + element.elementText("车牌"));
System.out.println("产地:" + element.elementText("产地"));
//取得子元素的属性
System.out.println("生产时间:" + element.element("车牌").attribute("生产时间").getValue());
}
} catch (DocumentException e) {
e.printStackTrace();
}
}
}
package com.example.test;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.junit.Test;
import java.io.*;
public class Dom4jParseTest {
private File file = new File(this.getClass().getClassLoader().getResource("car.xml").getPath());
@Test
public void dom4jAdd(){
try {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(file);
//取得根元素的第一个子元素
Element firstElement = (Element)document.getRootElement().elements().get(0);
//添加子元素“单价”并设置文本“30”
firstElement.addElement("单价").setText("30");
//写入XML文件
OutputStream outputStream = new FileOutputStream(file);
OutputFormat outputFormat = OutputFormat.createPrettyPrint();
XMLWriter xmlWriter = new XMLWriter(outputStream, outputFormat);
xmlWriter.write(document);
xmlWriter.close();
} catch (DocumentException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
<车辆清单>
<汽车>
<车牌 生产时间="2019">奥迪车牌>
<产地>北京产地>
<单价>30单价>
汽车>
<汽车>
<车牌 生产时间="2020">奔驰车牌>
<产地>上海产地>
汽车>
车辆清单>
package com.example.test;
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;
import org.junit.Test;
import java.io.*;
import java.util.List;
public class Dom4jParseTest {
private File file = new File(this.getClass().getClassLoader().getResource("car.xml").getPath());
@Test
public void dom4jAdd(){
try {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(file);
//取得根元素下的所有子元素
List elements = document.getRootElement().elements();
//创建新元素
Element element = DocumentHelper.createElement("汽车");
element.setText("指定位置增加元素");
//指定位置增加元素
elements.add(1, element);
//写入XML文件
OutputStream outputStream = new FileOutputStream(file);
OutputFormat outputFormat = OutputFormat.createPrettyPrint();
XMLWriter xmlWriter = new XMLWriter(outputStream, outputFormat);
xmlWriter.write(document);
xmlWriter.close();
} catch (DocumentException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
<车辆清单>
<汽车>
<车牌 生产时间="2019">奥迪车牌>
<产地>北京产地>
汽车>
<汽车>指定位置增加元素汽车>
<汽车>
<车牌 生产时间="2020">奔驰车牌>
<产地>上海产地>
汽车>
车辆清单>
package com.example.test;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.junit.Test;
import java.io.*;
public class Dom4jParseTest {
private File file = new File(this.getClass().getClassLoader().getResource("car.xml").getPath());
@Test
public void dom4jUpdate(){
try {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(file);
//取得根元素的第一个子元素
Element firstElement = (Element)document.getRootElement().elements().get(0);
//修改子元素文本为“深圳”
firstElement.element("产地").setText("深圳");
//写入XML文件
OutputStream outputStream = new FileOutputStream(file);
OutputFormat outputFormat = OutputFormat.createPrettyPrint();
XMLWriter xmlWriter = new XMLWriter(outputStream, outputFormat);
xmlWriter.write(document);
xmlWriter.close();
} catch (DocumentException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
<车辆清单>
<汽车>
<车牌 生产时间="2019">奥迪车牌>
<产地>深圳产地>
汽车>
<汽车>
<车牌 生产时间="2020">奔驰车牌>
<产地>上海产地>
汽车>
车辆清单>
package com.example.test;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.junit.Test;
import java.io.*;
public class Dom4jParseTest {
private File file = new File(this.getClass().getClassLoader().getResource("car.xml").getPath());
@Test
public void dom4jDelete(){
try {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(file);
//取得根元素的第一个子元素
Element firstElement = (Element)document.getRootElement().elements().get(0);
//删除子元素
firstElement.remove(firstElement.element("产地"));
//写入XML文件
OutputStream outputStream = new FileOutputStream(file);
OutputFormat outputFormat = OutputFormat.createPrettyPrint();
XMLWriter xmlWriter = new XMLWriter(outputStream, outputFormat);
xmlWriter.write(document);
xmlWriter.close();
} catch (DocumentException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
<车辆清单>
<汽车>
<车牌 生产时间="2019">奥迪车牌>
汽车>
<汽车>
<车牌 生产时间="2020">奔驰车牌>
<产地>上海产地>
汽车>
车辆清单>
package com.example.test;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.junit.Test;
import java.io.*;
public class Dom4jParseTest {
private File file = new File(this.getClass().getClassLoader().getResource("car.xml").getPath());
@Test
public void dom4jCreate(){
try {
//创建空的XML
Document document = DocumentHelper.createDocument();
//添加根元素
document.addElement("root").setText("根元素");
//写入XML文件
OutputStream outputStream = new FileOutputStream(file);
OutputFormat outputFormat = OutputFormat.createPrettyPrint();
XMLWriter xmlWriter = new XMLWriter(outputStream, outputFormat);
xmlWriter.write(document);
xmlWriter.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
根元素
package com.example.test;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.junit.Test;
import java.io.*;
public class Dom4jParseTest {
private File file = new File(this.getClass().getClassLoader().getResource("car.xml").getPath());
@Test
public void dom4jStringToXML(){
try {
String text = "<产地>上海产地>";
//String->XML
Document document = DocumentHelper.parseText(text);
//写入XML文件
OutputStream outputStream = new FileOutputStream(file);
OutputFormat outputFormat = OutputFormat.createPrettyPrint();
XMLWriter xmlWriter = new XMLWriter(outputStream, outputFormat);
xmlWriter.write(document);
xmlWriter.close();
} catch (DocumentException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
<产地>上海产地>
package com.example.test;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.junit.Test;
import java.io.*;
public class Dom4jParseTest {
private File file = new File(this.getClass().getClassLoader().getResource("car.xml").getPath());
@Test
public void dom4jXMLToString(){
try {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(file);
//取得根元素
Element rootElement = document.getRootElement();
//XML-String
String xmlStr = rootElement.asXML();
System.out.println(xmlStr);
} catch (DocumentException e) {
e.printStackTrace();
}
}
}
package com.example.test;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.junit.Test;
import java.io.*;
import java.util.List;
public class Dom4jParseTest {
private File file = new File(this.getClass().getClassLoader().getResource("car.xml").getPath());
@Test
public void dom4jXMLToString(){
try {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(file);
//取得所有汽车的产地
String xpath = "//产地";
List elements = document.selectNodes(xpath);
for (Element element:elements){
System.out.println(element.getText());
}
//取得第一辆汽车的产地
Element firstElement = (Element) document.selectSingleNode(xpath);
System.out.println(firstElement.getText());
//取得生产时间为2020的车的车牌
String xpath2020 = "//车牌[@生产时间='2020']";
Element secondElement = (Element) document.selectSingleNode(xpath2020);
System.out.println(secondElement.getText());
} catch (DocumentException e) {
e.printStackTrace();
}
}
}