java基础之XML
生活随笔
收集整理的這篇文章主要介紹了
java基础之XML
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
目錄
- java基礎(chǔ)之XML
- 1. XML解析概述
- 2. DOM4J介紹
- 2.1 常用包
- 2.2 內(nèi)置元素
- 2.2 Element類
- 2.3 Attribute類
- 2.4 常用操作
- 3. 代碼演示
- 3.1 DOM4J讀取xml文件
- 3.2 DOM4J創(chuàng)建xml文件
- 3.2 DOM4J修改xml文件
java基礎(chǔ)之XML
XML是一種通用的數(shù)據(jù)交換格式,它的平臺無關(guān)性、語言無關(guān)性、系統(tǒng)無關(guān)性、給數(shù)據(jù)集成與交互帶來了極大的方便。本篇文章重點介紹DOM4J對XML文件的一些操作。
1. XML解析概述
??常見解析方式和解析器
優(yōu)點:元素與元素之間保留結(jié)構(gòu)關(guān)系,故可以進行增刪改查操作。
缺點:XML文檔過大,可能出現(xiàn)內(nèi)存溢出顯現(xiàn)。
優(yōu)點:處理速度快,可以處理大文件
缺點:只能讀,逐行后將釋放資源。
2. DOM4J介紹
2.1 常用包
| import org.dom4j.Document; | Document文檔類 |
| import org.dom4j.Element | 元素節(jié)點類 |
| import org.dom4j.QName; | 一個對元素名字的封裝類 |
| import org.dom4j.io.SAXReader; | sax讀取類 |
| import org.dom4j.io.XMLWriter | xml寫入類 |
| import org.dom4j.io.OutputFormat | 輸出格式 |
2.2 內(nèi)置元素
| Attribute | 定義了 XML 的屬性。 |
| Branch | 指能夠包含子節(jié)點的節(jié)點。如XML元素(Element)和文檔(Docuemnts)定義了一個公共的行為 |
| CDATA | 定義了 XML CDATA 區(qū)域 |
| CharacterData | 是一個標識接口,標識基于字符的節(jié)點。如CDATA,Comment, Text. |
| Comment | 定義了 XML 注釋的行為 |
| Document | 定義了XML 文檔 |
| DocumentType | 定義 XML DOCTYPE 聲明 |
| Element | 定義XML 元素 |
| ElementHandler | 定義了Element 對象的處理器 |
| ElementPath | 被 ElementHandler 使用,用于取得當前正在處理的路徑層次信息 |
| Entity | 定義 XML entity |
| Node | 為dom4j中所有的XML節(jié)點定義了多態(tài)行為 |
| NodeFilter | 定義了在dom4j 節(jié)點中產(chǎn)生的一個濾鏡或謂詞的行為(predicate) |
| ProcessingInstruction | 定義 XML 處理指令 |
| Text | 定義 XML 文本節(jié)點 |
| Visitor | 用于實現(xiàn) Visitor模式 |
| XPath | 在分析一個字符串后會提供一個 XPath 表達式 |
2.2 Element類
| getQName() | 元素的QName對象 |
| getNamespace() | 元素所屬的Namespace對象 |
| getNamespacePrefix() | 元素所屬的Namespace對象的prefix |
| getNamespaceURI() | 元素所屬的Namespace對象的URI |
| getName() | 元素的local name |
| getQualifiedName() | 元素的qualified name |
| getText() | 元素所含有的text內(nèi)容,如果內(nèi)容為空則返回一個空字符串而不是null |
| getTextTrim() | 元素所含有的text內(nèi)容,其中連續(xù)的空格被轉(zhuǎn)化為單個空格,該方法不會返回null |
| attributeIterator() | 元素屬性的iterator,其中每個元素都是 |
2.3 Attribute類
| attributeValue() | 元素的某個指定屬性所含的值 |
| elementIterator() | 元素的子元素的iterator,其中每個元素都是Element對象 |
| element() | 元素的某個指定(qualified name或者local name)的子元素 |
| elementText() | 元素的某個指定(qualified name或者local name)的子元素中的text信息 |
| getParent() | 元素的父元素 |
| getPath() | 元素的XPath表達式,其中父元素的qualified name和子元素的qualified name之間使用”/”分隔 |
| isTextOnly() | 是否該元素只含有text或是空元素 |
| isRootElement() | 是否該元素是XML樹的根節(jié)點 |
2.4 常用操作
- 讀取xml文件,獲得document對象.
- 解析XML形式的文本,得到document對象.
- 獲取根節(jié)點
- 取得某節(jié)點的單個子節(jié)點
- 獲取節(jié)點文字
- 取得某節(jié)點下名為"title"所有字節(jié)點并進行遍歷
- 在某節(jié)點下添加子節(jié)點.
- 設(shè)置節(jié)點文字.
- 刪除某節(jié)點.
- 取得某節(jié)點下的某屬性
- 設(shè)置某節(jié)點的屬性和文字.
- 設(shè)置屬性的文字
- 刪除某屬性
3. 代碼演示
3.1 DOM4J讀取xml文件
??test.xml
<?xml version="1.0" encoding="UTF-8"?> <bookstore><book id="1"><title>巴黎圣母院</title><author>雨果</author></book><book id="2"><title>飄</title><author>米切爾</author></book> </bookstore>??1. 使用List列表解析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 XmlDemo {public static void main(String[] args) throws Exception {SAXReader reader = new SAXReader();File file = new File("test.xml");Document document = reader.read(file);Element root = document.getRootElement();List<Element> childElements = root.elements();for (Element child : childElements) {//已知屬性名情況下System.out.println("--->id: " + child.attributeValue("id"));System.out.println("title:" + child.elementText("title"));System.out.println("author:" + child.elementText("author"));//未知屬性名情況下/*List<Attribute> attributeList = child.attributes();for (Attribute attr : attributeList) {System.out.println(attr.getName() + ": " + attr.getValue());}List<Element> elementList = child.elements();for (Element ele : elementList) {System.out.println(ele.getName() + ": " + ele.getText());}System.out.println();*/}} } //輸出結(jié)果: --->id: 1 title:巴黎圣母院 author:雨果 --->id: 2 title:飄 author:米切爾??2. 使用Iterator解析xml
public class XmlDemo {public static void main(String[] args) throws Exception {SAXReader reader = new SAXReader();Document document = reader.read(new File("test.xml"));Element root = document.getRootElement();Iterator<Element> it = root.elementIterator();while (it.hasNext()) {Element element = it.next();//未知屬性名稱情況下Iterator<Element> attrIt = element.attributeIterator();while (attrIt.hasNext()) {Attribute a = (Attribute) attrIt.next();System.out.println(a.getValue());}Iterator<Element> eleIt = element.elementIterator();while (eleIt.hasNext()) {Element e = eleIt.next();System.out.println(e.getName() + ": " + e.getText());}System.out.println();//已知元素名情況下 /*System.out.println("id: " + element.attributeValue("id"));System.out.println("title: " + element.elementText("title"));System.out.println("author: " + element.elementText("author"));System.out.println();*/}} } //輸出結(jié)果: id: 1 title:巴黎圣母院 author:雨果id: 2 title:飄 author:米切爾3.2 DOM4J創(chuàng)建xml文件
public class XmlDemo {public static void main(String[] args) throws Exception {Document doc = DocumentHelper.createDocument();//增加根節(jié)點Element books = doc.addElement("bookstore");//增加子元素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é)點添加屬性book1.addAttribute("id", "3");//為元素添加內(nèi)容title1.setText("戰(zhàn)爭與和平");author1.setText("列夫托爾斯泰");book2.addAttribute("id", "4");title2.setText("紅樓夢");author2.setText("曹雪芹");//實例化輸出格式對象OutputFormat format = OutputFormat.createPrettyPrint();//設(shè)置輸出編碼format.setEncoding("UTF-8");//創(chuàng)建需要寫入的File對象File file = new File("test2.xml");//生成XMLWriter對象,構(gòu)造函數(shù)中的參數(shù)為需要輸出的文件流和格式XMLWriter writer = new XMLWriter(new FileOutputStream(file), format);//開始寫入,write方法中包含上面創(chuàng)建的Document對象writer.write(doc);} }??運行結(jié)果(項目根目錄下):
<?xml version="1.0" encoding="UTF-8"?><bookstore><book id="3"><title>戰(zhàn)爭與和平</title><author>列夫托爾斯泰</author></book><book id="4"><title>紅樓夢</title><author>曹雪芹</author></book> </bookstore>3.2 DOM4J修改xml文件
public class XmlDeml {public static void main(String[] args) throws Exception {SAXReader reader = new SAXReader();File file = new File("test.xml");Document document = reader.read(file);Element root = document.getRootElement();Element nameElement = root.element("book").element("author");nameElement.setText("魯迅");//寫回XML文檔OutputFormat format = OutputFormat.createPrettyPrint();XMLWriter writer = new XMLWriter(new FileOutputStream("test.xml"), format);writer.write(document);writer.close();} }??運行結(jié)果(項目根目錄下):
<?xml version="1.0" encoding="UTF-8"?><bookstore> <book id="1"> <title>巴黎圣母院</title> <author>魯迅</author> </book> </bookstore>轉(zhuǎn)載于:https://www.cnblogs.com/huizhipeng/p/10100113.html
總結(jié)
以上是生活随笔為你收集整理的java基础之XML的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: RestFramework之认证组件
- 下一篇: ASP.NET MVC 自定义模型绑定1