Java Dom解析的三個執行個體,javadom解析執行個體

來源:互聯網
上載者:User

Java Dom解析的三個執行個體,javadom解析執行個體
概述

Dom解析的文章已經爛大街了。在這裡,只舉三個情境下如何編碼的例子:

  • xml內有多個同名節點,擷取這多個同名節點
  • 已擷取到某個節點,產生這個節點的xml描述
  • 修改節點某屬性的值

詳細的講解看官請自行百度。
dom4j包下載連結為:點擊下載dom4j

擷取多個同名節點
  • xml執行個體

    <model name="model">    <operator name="kafkaInput">        <parameter  name="inputTopicName"  value="department1_tn1"/>    </operator>    <operator name="addFielduuid">        <parameter  name="parameter1" value="p1"/>    </operator>    <operator name="kafkaOutput">        <parameter  name="outTopicName" value="department1_tn1"/>    </operator>    <connect from="kafkaInput.outport1" to="addFielduuid.inport1"/>    <connect from="addFielduuid.outport1" to="kafkaOutput.inport1"/>    <ouput path="/home"/></model>
  • 如上xml執行個體中,model下有3個operator節點
  • 擷取多個同名節點不僅需要dom4j包,還需要Jaxen包。
  • Jaxen包下載連結為:點擊下載Jaxen
  • 擷取這三個節點的代碼為:

    publicstaticvoidparsingSameNameNode(String xmlPath) {File file = new File(argsPath);// 建立saxReader對象SAXReader reader = new SAXReader();// 通過read方法讀取一個檔案 轉換成Document對象Document document;try {    document = reader.read(file);    List<Element> operators = document.selectNodes("/model/operator");    for(Element operator:operators){        //do something...    }} catch (DocumentException e) {    e.printStackTrace();}}
    • 通過介面selectNodes(String arg0)
    • 介面參數為:"/model/operator"。意思是:擷取model節點下所有的operator節點。Element可以理解為節點的資訊
    • 注意解析前讀取xml內容、構建Document執行個體的代碼:

      File file = new File(argsPath);// 建立saxReader對象SAXReader reader = new SAXReader();// 通過read方法讀取一個檔案 轉換成Document對象Document document;try {document = reader.read(file);//do something...} catch (DocumentException e) {e.printStackTrace();}
節點的xml描述
  • dom解析得到某節點,將節點轉為String,即節點的xml描述。
  • 將節點轉化為String格式,介面為asXML()。
  • 轉碼如下:

    // 擷取根節點元素對象Element rootNode = document.getRootElement();// 擷取context節點所有文字內容String context = rootNode.element("context").asXML();
  • 上面代碼邏輯為:
    • 根據document拿到dom樹的根節點rooNode
    • 擷取rootNode節點下的context節點
    • 使用asXML()介面,將節點轉化為String格式。
修改屬性值
  • 有兩種修改屬性值的方法
    • 先擷取到該屬性,使用屬性的介面setValue(),修改屬性值
    • 先擷取到該屬性所在節點,使用介面setAttributeValue()修改屬性值
使用屬性的setValue()介面,修改屬性值
  • 代碼邏輯為:
    • 擷取到該屬性所在節點
    • 擷取該節點的屬性,即要修改值的屬性
    • 屬性對象調用介面setValue(),介面參數為:屬性值
    • 例上面xml中,output節點的path屬性值修改為:/home/BYRans,代碼如下:

      Element rootNode = document.getRootElement();Element node = rootNode.element("/model/output");Attribute path = node.attribute("path");path.setValue("/home/BYRans");
使用節點的setAttributeValue介面,修改屬性值
  • 代碼邏輯為:
    • 擷取到該屬性所在節點
    • 節點對象調用介面使用節點的setAttributeValue(),介面參數為:屬性名稱和屬性值
    • 注意此介面已不推薦使用,setAttributeValue()
    • 例上面xml中,output節點的path屬性值修改為:/home/BYRans,代碼如下:

      Element rootNode = document.getRootElement();Element node = rootNode.element("/model/output");node.setAttributeValue("path","/home/BYRans");

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.