<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    ?dom4j學習總結(二)

    (一)移除節點及屬性

    ???? /** 移除節點和屬性的操作
    ?????*?
    @throws ?DocumentException
    ?????
    */

    ????
    public ? void ?RemoveOperator()? throws ?DocumentException {
    ????????
    // 待生成xml的字符串
    ????????String?str = " <root><book?type='science'><Name>Java</Name><price>100</price></book> "
    ????????????
    + " <book?type='society'><Name>Society?security</Name><price>130</price></book> "
    ????????????
    + " <author><name>chb</name><sex>boy</sex></author></root> " ;
    ????????
    // 生成一個Document
    ????????Document?document? = ?DocumentHelper.parseText(str);
    ????????
    ????????Element?root
    = document.getRootElement();
    ????????
    // 刪除類型為society的book節點
    ????????Element?book_society = (Element)document.selectSingleNode( " //book[@type='society'] " );
    ????????root.remove(book_society);
    ????????System.out.println(
    " 1。正確的刪除了類型為society的book節點 " );
    ????????System.out.println(document.asXML());
    ????????
    ????????
    // 刪除sex節點
    ????????Element?sex = (Element)root.selectSingleNode( " //sex " );
    ????????
    ????????
    // 從root節點刪除
    ????????root.remove(sex);
    ????????System.out.println(
    " 2。這樣是不能刪除sex節點的 " );
    ????????System.out.println(document.asXML());
    ????????
    ????????
    // 從author節點刪除
    ????????root.element( " author " ).remove(sex);
    ????????System.out.println(
    " 3。這樣就可以正確刪除sex節點 " );
    ????????System.out.println(document.asXML());
    ????????
    ????????
    // 刪除屬性
    ????????Attribute?type = root.element( " book " ).attribute( " type " );
    ????????root.element(
    " book " ).remove(type);
    ????????System.out.println(
    " 4。正確刪除book節點的type屬性 " );
    ????????System.out.println(document.asXML());
    ????}

    輸出結果為:

    1。正確的刪除了類型為society的book節點
    <?xml version="1.0" encoding="UTF-8"?>
    <root><book type="science"><Name>Java</Name><price>100</price></book><author><name>chb</name><sex>boy</sex></author></root>
    2。這樣是不能刪除sex節點的
    <?xml version="1.0" encoding="UTF-8"?>
    <root><book type="science"><Name>Java</Name><price>100</price></book><author><name>chb</name><sex>boy</sex></author></root>
    3。這樣就可以正確刪除sex節點
    <?xml version="1.0" encoding="UTF-8"?>
    <root><book type="science"><Name>Java</Name><price>100</price></book><author><name>chb</name></author></root>
    4。正確刪除book節點的type屬性
    <?xml version="1.0" encoding="UTF-8"?>
    <root><book><Name>Java</Name><price>100</price></book><author><name>chb</name></author></root>

    分析:

    第二個輸出結果不能刪除sex節點,我們需要看dom4j的API

    remove

    public boolean remove(Element?element)
    Removes the given Element if the node is an immediate child of this branch. If the given node is not an immediate child of this branch then the Node.detach()method should be used instead.

    Parameters:
    element - is the element to be removed
    Returns:
    true if the element was removed

    從中我們可以看出,remove只能用在它自己的直接孩子節點上,不能用在孫子節點上,因為sex節點不是root節點的直接孩子節點,所以不能刪除;而sex節點卻是author節點的直接孩子節點,所以第三個輸出可以刪除。

    (二)將兩個Document合并為一個Document

    先看一個錯誤的情況

    (1)使用add()方法添加

    public ? void ?CombineDocument()? throws ?DocumentException {
    ????????
    // 待生成兩個Document的字符串
    ????????String?str_book = " <root><book?type='science'><Name>Java</Name><price>100</price></book> "
    ????????????
    + " <book?type='society'><Name>Society?security</Name><price>130</price></book> "
    ????????????
    + " </root> " ;
    ????????String?str_author
    = " <root><author><name>chb</name><sex>boy</sex></author></root> " ;
    ????????
    ????????
    // 生成兩個Document
    ????????Document?doc_book = DocumentHelper.parseText(str_book);
    ????????Document?doc_author
    = DocumentHelper.parseText(str_author);
    ????????
    ????????
    // 取出doc_author的author節點,添加到doc_book的根結點
    ????????Element?author = (Element)doc_author.selectSingleNode( " //author " );
    ????????doc_book.getRootElement().add(author);
    ????????System.out.println(doc_book.asXML());
    ????}

    調用CombineDocument函數,會出現以下錯誤:

    org.dom4j.IllegalAddException: The node "org.dom4j.tree.DefaultElement@17bd6a1 [Element: <author attributes: []/>]" could not be added to the element "root" because: The Node already has an existing parent of "root"
    ?at org.dom4j.tree.AbstractElement.addNode(AbstractElement.java:1521)
    ?at org.dom4j.tree.AbstractElement.add(AbstractElement.java:1002)
    ?at xml_chb.dom4j_chb.CombineDocument(dom4j_chb.java:189)
    ?at xml_chb.dom4j_chb.main(dom4j_chb.java:199)
    Exception in thread "main"

    即提示author節點已經有一個root節點了,不能再添加到另一個節點上去。

    (2)使用appendContent()方法

    即將doc_book.getRootElement().add(author);

    改為:doc_book.getRootElement().appendContent(author);

    輸出結果為:

    <?xml version="1.0" encoding="UTF-8"?>
    <root>
    <book type="science"><Name>Java</Name><price>100</price></book>
    <book type="society"><Name>Society security</Name><price>130</price></book>
    <name>chb</name><sex>boy</sex>
    </root>

    可以看出,缺少了author節點,只是把author節點的子節點添加上去了,但是由此可見,appendContent方法是有希望的。

    我們看一下dom4j的API:

    appendContent

    public void appendContent(Branch?branch)
    Appends the content of the given branch to this branch instance. This method behaves like the Collection.addAll(java.util.Collection) method.

    Parameters:
    branch - is the branch whose content will be added to me.

    (3)使用正確的appendContent方法

    將:Element author=(Element)doc_author.selectSingleNode("http://author");

    doc_book.getRootElement().appendContent(author);

    改為:doc_book.getRootElement().appendContent(doc_author.getRootElement());

    輸出:

    <?xml version="1.0" encoding="UTF-8"?>
    <root>
    <book type="science"><Name>Java</Name><price>100</price></book>
    <book type="society"><Name>Society security</Name><price>130</price></book>
    <author><name>chb</name><sex>boy</sex></author>
    </root>

    是正確結果

    (4)另一種可行的方法

    public ? void ?CombineDocument()? throws ?DocumentException {
    ????????
    // 待生成兩個Document的字符串
    ????????String?str_book = " <root><book?type='science'><Name>Java</Name><price>100</price></book> "
    ????????????
    + " <book?type='society'><Name>Society?security</Name><price>130</price></book> "
    ????????????
    + " </root> " ;
    ????????String?str_author
    = " <root><author><name>chb</name><sex>boy</sex></author></root> " ;
    ????????
    ????????
    // 生成兩個Document
    ????????Document?doc_book = DocumentHelper.parseText(str_book);
    ????????Document?doc_author
    = DocumentHelper.parseText(str_author);
    ????????
    ????????
    // 新生成一個Document
    ????????Element?author = DocumentHelper.createElement( " author " );
    ????????author.appendContent((Element)doc_author.selectSingleNode(
    " //author " ));
    ????????
    // 當前author尚無父節點,所以可以使用add方法添加
    ????????doc_book.getRootElement().add(author);

    ????????System.out.println(doc_book.asXML());
    ????}
    posted on 2006-09-24 23:41 揚州夢 閱讀(262) 評論(0)  編輯  收藏 所屬分類: java

    統計

    主站蜘蛛池模板: 黄网站色视频免费观看45分钟| 亚洲视频在线免费播放| 亚洲精品无码久久久久牙蜜区| 免费人成在线观看播放a| 免费看的黄色大片| 亚洲最大成人网色香蕉| 中文字幕无码视频手机免费看| 亚洲乱人伦精品图片| 最近免费中文字幕视频高清在线看 | 亚洲一区二区三区自拍公司| 免费无遮挡无码视频在线观看 | 亚洲中文字幕乱码熟女在线| 无人影院手机版在线观看免费| 77777午夜亚洲| 午夜影视在线免费观看| 国产亚洲综合久久| 亚洲精品第一国产综合境外资源 | 成人片黄网站A毛片免费| 亚洲AV无码片一区二区三区| 可以免费观看的一级毛片| 丰满妇女做a级毛片免费观看| 亚洲尤码不卡AV麻豆| 7x7x7x免费在线观看| 一本色道久久88亚洲精品综合 | 国产亚洲情侣一区二区无| 3344在线看片免费| 亚洲国产精品yw在线观看| 卡一卡二卡三在线入口免费| 男女猛烈无遮掩视频免费软件| 国产亚洲精aa成人网站| 午夜不卡久久精品无码免费| 精品亚洲成在人线AV无码| 免费一级毛片在线播放| 免费播放在线日本感人片| 亚洲精品在线播放| 国产婷婷高清在线观看免费| 中文字幕av免费专区| 无码人妻久久一区二区三区免费| 亚洲国产视频久久| 亚洲熟妇av一区二区三区| 老司机在线免费视频|