在java中使用dom4j解析xml(示例代碼)
雖然Java中已經(jīng)有了Dom和Sax這兩種標(biāo)準(zhǔn)解析方式
但其操作起來(lái)并不輕松,對(duì)于我這么一個(gè)初學(xué)者來(lái)說(shuō),其中部分代碼是活生生的惡心
為此,偉大的第三方開(kāi)發(fā)組開(kāi)發(fā)出了Jdom和Dom4j等工具
鑒于目前的趨勢(shì),我們這里來(lái)講講Dom4j的基本用法,不涉及遞歸等復(fù)雜操作
Dom4j的用法很多,官網(wǎng)上的示例有那么點(diǎn)兒晦澀,這里就不寫(xiě)了
首先我們需要出創(chuàng)建一個(gè)xml文檔,然后才能對(duì)其解析
xml文檔:
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book id="001">
<title>Harry Potter</title>
<author>J K. Rowling</author>
</book>
<book id="002">
<title>Learning XML</title>
<author>Erik T. Ray</author>
</book>
</books>
示例一:用List列表的方式來(lái)解析xml
import java.io.File;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class Demo {
public static void main(String[] args) throws Exception {
SAXReader reader = new SAXReader();
File file = new File("books.xml");
Document document = reader.read(file);
Element root = document.getRootElement();
List<Element> childElements = root.elements();
for (Element child : childElements) {
//未知屬性名情況下
/*List<Attribute> attributeList = child.attributes();
for (Attribute attr : attributeList) {
System.out.println(attr.getName() + ": " + attr.getValue());
}*/
//已知屬性名情況下
System.out.println("id: " + child.attributeValue("id"));
//未知子元素名情況下
/*List<Element> elementList = child.elements();
for (Element ele : elementList) {
System.out.println(ele.getName() + ": " + ele.getText());
}
System.out.println();*/
//已知子元素名的情況下
System.out.println("title" + child.elementText("title"));
System.out.println("author" + child.elementText("author"));
//這行是為了格式化美觀而存在
System.out.println();
}
}
}
示例二:使用Iterator迭代器的方式來(lái)解析xml
import java.io.File;
import java.util.Iterator;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class Demo {
public static void main(String[] args) throws Exception {
SAXReader reader = new SAXReader();
Document document = reader.read(new File("books.xml"));
Element root = document.getRootElement();
Iterator it = root.elementIterator();
while (it.hasNext()) {
Element element = (Element) it.next();
//未知屬性名稱(chēng)情況下
/*Iterator attrIt = element.attributeIterator();
while (attrIt.hasNext()) {
Attribute a = (Attribute) attrIt.next();
System.out.println(a.getValue());
}*/
//已知屬性名稱(chēng)情況下
System.out.println("id: " + element.attributeValue("id"));
//未知元素名情況下
/*Iterator eleIt = element.elementIterator();
while (eleIt.hasNext()) {
Element e = (Element) eleIt.next();
System.out.println(e.getName() + ": " + e.getText());
}
System.out.println();*/
//已知元素名情況下
System.out.println("title: " + element.elementText("title"));
System.out.println("author: " + element.elementText("author"));
System.out.println();
}
}
}
運(yùn)行結(jié)果:
示例三:創(chuàng)建xml文檔并輸出到文件
import java.io.File;
import java.io.FileOutputStream;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
public class Demo {
public static void main(String[] args) throws Exception {
Document doc = DocumentHelper.createDocument();
//增加根節(jié)點(diǎn)
Element books = doc.addElement("books");
//增加子元素
Element book1 = books.addElement("book");
Element title1 = book1.addElement("title");
Element author1 = book1.addElement("author");
Element book2 = books.addElement("book");
Element title2 = book2.addElement("title");
Element author2 = book2.addElement("author");
//為子節(jié)點(diǎn)添加屬性
book1.addAttribute("id", "001");
//為元素添加內(nèi)容
title1.setText("Harry Potter");
author1.setText("J K. Rowling");
book2.addAttribute("id", "002");
title2.setText("Learning XML");
author2.setText("Erik T. Ray");
//實(shí)例化輸出格式對(duì)象
OutputFormat format = OutputFormat.createPrettyPrint();
//設(shè)置輸出編碼
format.setEncoding("UTF-8");
//創(chuàng)建需要寫(xiě)入的File對(duì)象
File file = new File("D:" + File.separator + "books.xml");
//生成XMLWriter對(duì)象,構(gòu)造函數(shù)中的參數(shù)為需要輸出的文件流和格式
XMLWriter writer = new XMLWriter(new FileOutputStream(file), format);
//開(kāi)始寫(xiě)入,write方法中包含上面創(chuàng)建的Document對(duì)象
writer.write(doc);
}
}
運(yùn)行結(jié)果:
相關(guān)文章
Java類(lèi)庫(kù)BeanUtils組件使用方法及實(shí)例詳解
這篇文章主要介紹了Java類(lèi)庫(kù)BeanUtils組件使用方法級(jí)實(shí)例詳解,需要的朋友可以參考下2020-02-02一文學(xué)習(xí)Java NIO的ByteBuffer工作原理
很多網(wǎng)友說(shuō)JDK又在寫(xiě)B(tài)ug!下面通過(guò)通過(guò)本文學(xué)習(xí)下為何Java NIO的ByteBuffer這么垃圾,涉及到ByteBuf API 的優(yōu)點(diǎn)及工作原理解析,感興趣的朋友跟隨小編一起看看吧2021-05-05JDK 5 提供的注解:Target、Inherited和Documented的區(qū)別
這篇文章主要介紹了JDK 5 提供的注解:Target、Inherited和Documented的區(qū)別,需要的朋友可以參考下2016-02-02springboot使用kafka推送數(shù)據(jù)到服務(wù)端的操作方法帶認(rèn)證
在使用Kafka進(jìn)行數(shù)據(jù)推送時(shí),遇到認(rèn)證問(wèn)題導(dǎo)致連接失敗,本文詳細(xì)介紹了Kafka的認(rèn)證配置過(guò)程,包括配置文件的引入和參數(shù)設(shè)置,實(shí)際測(cè)試表明,需要正確配置sasl.jaas.config以及其他認(rèn)證參數(shù),探討了配置文件是否可以同時(shí)存在多個(gè)配置塊的可能性,并提出了實(shí)際操作中的注意事項(xiàng)2024-11-11javaweb圖書(shū)商城設(shè)計(jì)之圖書(shū)模塊(4)
這篇文章主要介紹了javaweb圖書(shū)商城設(shè)計(jì)之圖書(shū)模塊的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11