本站首页    管理页面    写新日志    退出

«October 2025»
1234
567891011
12131415161718
19202122232425
262728293031


公告

  如果你忍了,欺负你的人将来可能就进监狱了。如果你反击,欺负你的人将来可能就获选十大杰出青年了。

        QQ: 3159671

http://greenboy.javaeye.com/

http://blog.sina.com.cn/u/1278341164 小鸟吹烟


我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:小鸟吹烟
日志总数:157
评论数量:424
留言数量:-1
访问次数:1261200
建立时间:2006年10月23日




[XML]dom4j学习总结(二)
文章收藏,  网上资源

tone 发表于 2007/2/28 11:25:32

http://www.1to2.us/dom4j-a138834.htm dom4j API ---- http://www.dom4j.org/apidocs/index.html (一)移除节点及属性 500)this.width=500'>500)this.width=500'>    /** *//**移除节点和属性的操作500)this.width=500'>     * @throws DocumentException500)this.width=500'>     */500)this.width=500'>500)this.width=500'>    public void RemoveOperator() throws DocumentException...{500)this.width=500'>        //待生成xml的字符串500)this.width=500'>        String str="<root><book type='science'><Name>Java</Name><price>100</price></book>"500)this.width=500'>            +"<book type='society'><Name>Society security</Name><price>130</price></book>"500)this.width=500'>            +"<author><name>chb</name><sex>boy</sex></author></root>";500)this.width=500'>        //生成一个Document500)this.width=500'>        Document document = DocumentHelper.parseText(str);500)this.width=500'>        500)this.width=500'>        Element root=document.getRootElement();500)this.width=500'>        //删除类型为society的book节点500)this.width=500'>        Element book_society=(Element)document.selectSingleNode("//book[@type='society']");500)this.width=500'>        root.remove(book_society);500)this.width=500'>        System.out.println("1。正确的删除了类型为society的book节点");500)this.width=500'>        System.out.println(document.asXML());500)this.width=500'>        500)this.width=500'>        //删除sex节点500)this.width=500'>        Element sex=(Element)root.selectSingleNode("//sex");500)this.width=500'>        500)this.width=500'>        //从root节点删除500)this.width=500'>        root.remove(sex);500)this.width=500'>        System.out.println("2。这样是不能删除sex节点的");500)this.width=500'>        System.out.println(document.asXML());500)this.width=500'>        500)this.width=500'>        //从author节点删除500)this.width=500'>        root.element("author").remove(sex);500)this.width=500'>        System.out.println("3。这样就可以正确删除sex节点");500)this.width=500'>        System.out.println(document.asXML());500)this.width=500'>        500)this.width=500'>        //删除属性500)this.width=500'>        Attribute type=root.element("book").attribute("type");500)this.width=500'>        root.element("book").remove(type);500)this.width=500'>        System.out.println("4。正确删除book节点的type属性");500)this.width=500'>        System.out.println(document.asXML());500)this.width=500'>    } 输出结果为: 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 removepublic 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()方法添加 500)this.width=500'>500)this.width=500'>public void CombineDocument() throws DocumentException...{500)this.width=500'>        //待生成两个Document的字符串500)this.width=500'>        String str_book="<root><book type='science'><Name>Java</Name><price>100</price></book>"500)this.width=500'>            +"<book type='society'><Name>Society security</Name><price>130</price></book>"500)this.width=500'>            +"</root>";500)this.width=500'>        String str_author="<root><author><name>chb</name><sex>boy</sex></author></root>";500)this.width=500'>        500)this.width=500'>        //生成两个Document500)this.width=500'>        Document doc_book=DocumentHelper.parseText(str_book);500)this.width=500'>        Document doc_author=DocumentHelper.parseText(str_author);500)this.width=500'>        500)this.width=500'>        //取出doc_author的author节点,添加到doc_book的根结点500)this.width=500'>        Element author=(Element)doc_author.selectSingleNode("//author");500)this.width=500'>        doc_book.getRootElement().add(author);500)this.width=500'>        System.out.println(doc_book.asXML());500)this.width=500'>    } 调用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: appendContentpublic 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("//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)另一种可行的方法 500)this.width=500'>500)this.width=500'>public void CombineDocument() throws DocumentException...{500)this.width=500'>        //待生成两个Document的字符串500)this.width=500'>        String str_book="<root><book type='science'><Name>Java</Name><price>100</price></book>"500)this.width=500'>            +"<book type='society'><Name>Society security</Name><price>130</price></book>"500)this.width=500'>            +"</root>";500)this.width=500'>        String str_author="<root><author><name>chb</name><sex>boy</sex></author></root>";500)this.width=500'>        500)this.width=500'>        //生成两个Document500)this.width=500'>        Document doc_book=DocumentHelper.parseText(str_book);500)this.width=500'>        Document doc_author=DocumentHelper.parseText(str_author);500)this.width=500'>        500)this.width=500'>        //新生成一个Document500)this.width=500'>        Element author=DocumentHelper.createElement("author");500)this.width=500'>        author.appendContent((Element)doc_author.selectSingleNode("//author"));500)this.width=500'>        //当前author尚无父节点,所以可以使用add方法添加500)this.width=500'>        doc_book.getRootElement().add(author);500)this.width=500'>500)this.width=500'>        System.out.println(doc_book.asXML());500)this.width=500'>    }


阅读全文(2129) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.047 second(s), page refreshed 144788852 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号