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

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

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

    ?dom4j學(xué)習(xí)總結(jié)(二)

    (一)移除節(jié)點(diǎn)及屬性

    ???? /** 移除節(jié)點(diǎn)和屬性的操作
    ?????*?
    @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> " ;
    ????????
    // 生成一個(gè)Document
    ????????Document?document? = ?DocumentHelper.parseText(str);
    ????????
    ????????Element?root
    = document.getRootElement();
    ????????
    // 刪除類型為society的book節(jié)點(diǎn)
    ????????Element?book_society = (Element)document.selectSingleNode( " //book[@type='society'] " );
    ????????root.remove(book_society);
    ????????System.out.println(
    " 1。正確的刪除了類型為society的book節(jié)點(diǎn) " );
    ????????System.out.println(document.asXML());
    ????????
    ????????
    // 刪除sex節(jié)點(diǎn)
    ????????Element?sex = (Element)root.selectSingleNode( " //sex " );
    ????????
    ????????
    // 從root節(jié)點(diǎn)刪除
    ????????root.remove(sex);
    ????????System.out.println(
    " 2。這樣是不能刪除sex節(jié)點(diǎn)的 " );
    ????????System.out.println(document.asXML());
    ????????
    ????????
    // 從author節(jié)點(diǎn)刪除
    ????????root.element( " author " ).remove(sex);
    ????????System.out.println(
    " 3。這樣就可以正確刪除sex節(jié)點(diǎn) " );
    ????????System.out.println(document.asXML());
    ????????
    ????????
    // 刪除屬性
    ????????Attribute?type = root.element( " book " ).attribute( " type " );
    ????????root.element(
    " book " ).remove(type);
    ????????System.out.println(
    " 4。正確刪除book節(jié)點(diǎn)的type屬性 " );
    ????????System.out.println(document.asXML());
    ????}

    輸出結(jié)果為:

    1。正確的刪除了類型為society的book節(jié)點(diǎn)
    <?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節(jié)點(diǎn)的
    <?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節(jié)點(diǎn)
    <?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節(jié)點(diǎn)的type屬性
    <?xml version="1.0" encoding="UTF-8"?>
    <root><book><Name>Java</Name><price>100</price></book><author><name>chb</name></author></root>

    分析:

    第二個(gè)輸出結(jié)果不能刪除sex節(jié)點(diǎn),我們需要看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只能用在它自己的直接孩子節(jié)點(diǎn)上,不能用在孫子節(jié)點(diǎn)上,因?yàn)閟ex節(jié)點(diǎn)不是root節(jié)點(diǎn)的直接孩子節(jié)點(diǎn),所以不能刪除;而sex節(jié)點(diǎn)卻是author節(jié)點(diǎn)的直接孩子節(jié)點(diǎn),所以第三個(gè)輸出可以刪除。

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

    先看一個(gè)錯(cuò)誤的情況

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

    public ? void ?CombineDocument()? throws ?DocumentException {
    ????????
    // 待生成兩個(gè)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> " ;
    ????????
    ????????
    // 生成兩個(gè)Document
    ????????Document?doc_book = DocumentHelper.parseText(str_book);
    ????????Document?doc_author
    = DocumentHelper.parseText(str_author);
    ????????
    ????????
    // 取出doc_author的author節(jié)點(diǎn),添加到doc_book的根結(jié)點(diǎn)
    ????????Element?author = (Element)doc_author.selectSingleNode( " //author " );
    ????????doc_book.getRootElement().add(author);
    ????????System.out.println(doc_book.asXML());
    ????}

    調(diào)用CombineDocument函數(shù),會(huì)出現(xiàn)以下錯(cuò)誤:

    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節(jié)點(diǎn)已經(jīng)有一個(gè)root節(jié)點(diǎn)了,不能再添加到另一個(gè)節(jié)點(diǎn)上去。

    (2)使用appendContent()方法

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

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

    輸出結(jié)果為:

    <?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節(jié)點(diǎn),只是把a(bǔ)uthor節(jié)點(diǎn)的子節(jié)點(diǎn)添加上去了,但是由此可見,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>

    是正確結(jié)果

    (4)另一種可行的方法

    public ? void ?CombineDocument()? throws ?DocumentException {
    ????????
    // 待生成兩個(gè)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> " ;
    ????????
    ????????
    // 生成兩個(gè)Document
    ????????Document?doc_book = DocumentHelper.parseText(str_book);
    ????????Document?doc_author
    = DocumentHelper.parseText(str_author);
    ????????
    ????????
    // 新生成一個(gè)Document
    ????????Element?author = DocumentHelper.createElement( " author " );
    ????????author.appendContent((Element)doc_author.selectSingleNode(
    " //author " ));
    ????????
    // 當(dāng)前author尚無父節(jié)點(diǎn),所以可以使用add方法添加
    ????????doc_book.getRootElement().add(author);

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

    統(tǒng)計(jì)

    主站蜘蛛池模板: 中文字幕人成无码免费视频| 亚洲av片在线观看| 久久精品亚洲视频| 亚洲国产一区视频| 免费国内精品久久久久影院| 啦啦啦在线免费视频| 嫩草影院在线免费观看| 无码精品A∨在线观看免费| 久久大香香蕉国产免费网站| 免费人成在线观看视频高潮| 中文字幕无码免费久久9一区9 | 亚洲A∨精品一区二区三区| 免费国产成人高清在线观看麻豆 | 无码国产精品一区二区免费3p| 免费精品一区二区三区第35| 久久久久久免费一区二区三区| a毛片免费播放全部完整| 免费观看一区二区三区| 一级毛片在线免费看| 中文字幕视频免费| 国产h视频在线观看免费| 成人啪精品视频免费网站| 国产美女无遮挡免费视频网站| 又大又硬又爽免费视频| 亚洲七七久久精品中文国产| 久久久久亚洲av成人无码电影| 亚洲AV无码成人精品区天堂| 久久久亚洲欧洲日产国码aⅴ | 亚洲日韩国产一区二区三区| 国产亚洲色视频在线| 亚洲va国产va天堂va久久| 亚洲国产成人综合| 亚洲国产成人久久精品软件| 一级特级aaaa毛片免费观看| 特级精品毛片免费观看| 在线观看无码AV网站永久免费| 免费人成视频x8x8入口| 国产V亚洲V天堂无码久久久| 亚洲国产亚洲片在线观看播放| 亚洲av无码专区在线观看下载| 99久久免费国产特黄|