目錄
一、使用 dom4j 修改數據
1、導入 jar 包
使用 dom4j-1.6.1.jar包
鏈接:https://pan.baidu.com/s/18IaiY30xwZbGKrQDYp4xKQ
提取碼:3jkx
2、獲取 Document 對象
- 創建解析器對象:SAXReader saxReader = new SAXReader();
- 解析 xml 文件獲取 document 對象:Document document = saxReader.read(url);
3、獲取對應的標籤 Element 對象
- 得到根元素:Element root = document.getRootElement();
4、獲取數據
- element(qname):獲取指定標籤下面第一個字標籤,參數qname表示標籤的名
- elements(qname):獲取指定標籤下面所有的子標籤(一層的標籤)
- elements():獲取標籤下面的所有一層子標籤
5、末尾添加數據
- setText("文本內容"):添加文本
- 回寫 xml
- 格式化:OutputFormat format = OutputFormat.createPrettyPrint(); //縮進xml
- 使用 XMLWriter 類:XMLWriter write = new XMLWriter(new FileOutputStream("./src/myDom4j/person.xml"),format);
6、指定位置添加數據
- 創建元素:Element school = DocumentHelper.createElement("school");
- 創建文本:school.addText("光明小學");
- 指定位置添加:list.add(1,school);
- 回寫 xml:和在末尾添加一樣
7、獲取屬性值
- attributeValue():獲取屬性值
public static void main(String[] args) throws DocumentException, IOException {
//查詢 xml 文本
//selectName();
//在末尾添加標籤
//addSex();
//在指定位置添加
//addInsert();
//獲取屬性值
getType();
}
//查詢 xml 文本
public static void selectName() throws DocumentException {
/*
1.獲取Document對象
2.獲取根元素Element對象
3.獲取P1
4.獲取P1下的name
5.獲取name裏面的文本
*/
//1.獲取Document對象
SAXReader reader = new SAXReader();
Document document =reader.read("./src/myDom4j/person.xml");
//2.獲取根元素Element對象
Element root = document.getRootElement();
//3.獲取P1
List<Element> list = root.elements("p1");
for(Element name : list){
//4.獲取P1下的name
Element name1 = name.element("name");
//5.獲取name裏面的文本
System.out.println(name1.getText());
}
}
//在末尾添加標籤
public static void addSex() throws IOException, DocumentException {
/*
1.獲取Document對象
2.獲取根元素Element對象
3.獲取P1
4.在P1下面添加元素
5.在添加的元素裏面添加文本
6.回寫xml
*/
//1.獲取Document對象
SAXReader reader = new SAXReader();
Document document =reader.read("./src/myDom4j/person.xml");
//2.獲取根元素Element對象
Element root = document.getRootElement();
//3.獲取P1
Element P1 = root.element("p1");
//4.在P1下面添加元素
Element sex = P1.addElement("Sex");
//5.在添加的元素裏面添加文本
sex.addText("男");
//6.回寫xml
OutputFormat format = OutputFormat.createPrettyPrint(); //縮進xml
XMLWriter write = new XMLWriter(new FileOutputStream("./src/myDom4j/person.xml"),format);
write.write(document);
write.close();
}
//在指定位置添加
private static void addInsert() throws DocumentException, IOException {
/*
1.獲取Document對象
2.獲取根元素Element對象
3.獲取第一個P1
4.獲取P1下面所有元素
5.創建元素
6.創建文本
7.在指定位置添加
8.回寫xml
*/
//1.獲取Document對象
SAXReader reader = new SAXReader();
Document document =reader.read("./src/myDom4j/person.xml");
//2.獲取根元素Element對象
Element root = document.getRootElement();
//3.獲取第一個P1
Element P1 = root.element("p1");
//4.獲取P1下面所有元素
List<Element> list = P1.elements();
//5.創建元素
Element school = DocumentHelper.createElement("school");
//6.創建文本
school.addText("光明小學");
//7.在指定位置添加
list.add(1,school);
//8.回寫xml
OutputFormat format = OutputFormat.createPrettyPrint(); //縮進xml
XMLWriter write = new XMLWriter(new FileOutputStream("./src/myDom4j/person.xml"),format);
write.write(document);
write.close();
}
//獲取屬性值
public static void getType() throws DocumentException {
/*
1.獲取Document對象
2.獲取根元素Element對象
3.獲取第一個P1
4.獲取P1裏面的屬性
*/
//1.獲取Document對象
SAXReader reader = new SAXReader();
Document document =reader.read("./src/myDom4j/person.xml");
//2.獲取根元素Element對象
Element root = document.getRootElement();
//3.獲取第一個P1
Element P1 = root.element("p1");
//4.獲取P1裏面的屬性
String value = P1.attributeValue("id");
System.out.println(value);
}
二、使用 dom4j 支持 xpath 操作
1、獲取元素常見形式
使用 xpath 操作可以直接獲取到某個元素,常見的一些獲取形式有:
- /AAA/DDD/BBB:表示一層一層的獲取
- //BBB:表示和這個名稱相同,只要名稱是 BBB 都可以獲取到
- /*:表示所有元素
- BBB[1]:表示第一個 BBB 元素
- BBB[last()]:表示 BBB 的最後一個元素
- //BBB[@id]:表示只要 BBB 元素上面有 id 屬性,都可以獲取
- //BBB[@id='b1']:表示元素名稱是 BBB,在 BBB 上面有 id 屬性,並且 id 的屬性值是 b1
2、xpath 開發具體操作
【1】導入 jar 包
使用 jaxen-1.1-beta-6.jar 包
鏈接:https://pan.baidu.com/s/1mSq4r6Jkz6flqdO-JT3Y-w
提取碼:kl1y
【2】常用方法
- selectNodes("xpath表達式"):獲取多個節點
- selectStringleNode("xpath表達式"):獲取一個節點
public static void main(String[] args) throws DocumentException {
//查詢 xml 中的name
selectName();
//獲取第一個p1下面的name值
selectFirName();
}
//查詢 xml 中的name
public static void selectName() throws DocumentException {
//1.獲取Document對象
SAXReader reader = new SAXReader();
Document document =reader.read("./src/myDom4j/person.xml");
//2.使用selectNode()方法獲取所有節點
List<Node> list= document.selectNodes("//name");
for(Node name : list){
System.out.println(name.getText());
}
}
//獲取第一個p1下面的name值
public static void selectFirName() throws DocumentException {
//1.獲取Document對象
SAXReader reader = new SAXReader();
Document document =reader.read("./src/myDom4j/person.xml");
//2.使用selectNode()方法獲取所有節點
Node name = document.selectSingleNode("//p1[@id='pid']/name");
System.out.println(name.getText());
}