日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

java基础之XML

發(fā)布時間:2023/12/13 asp.net 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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解析概述

??常見解析方式和解析器

  • DOM:要求解析器把整個XML文檔裝載到內(nèi)存,并解析成一個Document對象。
    優(yōu)點:元素與元素之間保留結(jié)構(gòu)關(guān)系,故可以進行增刪改查操作。
    缺點:XML文檔過大,可能出現(xiàn)內(nèi)存溢出顯現(xiàn)。
  • SAX:是一種速度更快,更有效的方法。它逐行掃描文檔,一邊掃描一邊解析。并以事件驅(qū)動的方式進行具體解析,每執(zhí)行一行,都將觸發(fā)對應(yīng)的事件。
    優(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.XMLWriterxml寫入類
    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對象.
    SAXReader reader = new SAXReader(); Document document = reader.read(new File("***.xml"));
    • 解析XML形式的文本,得到document對象.
    String text = "<members></members>"; Document document = DocumentHelper.parseText(text);
    • 獲取根節(jié)點
    Element root = dom.getRootElement();
    • 取得某節(jié)點的單個子節(jié)點
    Element memberElm=root.element("title");
    • 獲取節(jié)點文字
    String text=memberElm.getText();
    • 取得某節(jié)點下名為"title"所有字節(jié)點并進行遍歷
    List list = rootElm.elements("member"); Iterator<Element> it = list.iterator(); while(it.hasNext()){Element elm = it.next();// do something... }
    • 在某節(jié)點下添加子節(jié)點.
    Element ageElm = newMemberElm.addElement("age");
    • 設(shè)置節(jié)點文字.
    ageElm.setText("29");
    • 刪除某節(jié)點.
    parentElm.remove(childElm);
    • 取得某節(jié)點下的某屬性
    Element root=document.getRootElement(); Attribute attribute=root.attribute("id");
    • 設(shè)置某節(jié)點的屬性和文字.
    newMemberElm.addAttribute("name", "sitinspring");
    • 設(shè)置屬性的文字
    Attribute attribute=root.attribute("name"); attribute.setText("sitinspring");
    • 刪除某屬性
    Attribute attribute=root.attribute("size");// 屬性名name root.remove(attribute);

    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)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。