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

«September 2025»
123456
78910111213
14151617181920
21222324252627
282930


公告

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

        QQ: 3159671

http://greenboy.javaeye.com/

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


我的分类(专题)

日志更新

最新评论

留言板

链接

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




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

tone 发表于 2007/2/28 11:34:36

http://www.1to2.us/dom4j-a138845.htm (一)创建Document的基本操作 /**  * xml基本操作  */ public void BaseOperation(){  //创建一个document  Document document=DocumentHelper.createDocument();  //创建根结点  Element root=document.addElement("root");  //为根结点添加一个book节点  Element book1=root.addElement("book");  //为book1添加属性type  book1.addAttribute("type","science");  //为book1添加name子节点  Element name1=book1.addElement("Name");  //并设置其name为"Java"  name1.setText("Java");  //为book1创建一个price节点,并设其价格为100  book1.addElement("price").setText("100");    //为根结点添加第二个book节点,并设置该book节点的type属性  Element book2=root.addElement("book").addAttribute("type","science");  //为book1添加name子节点  Element name2=book2.addElement("Name");  //并设置其name为"Oracle"  name2.setText("Oracle");  //为book1创建一个price节点,并设其价格为200  book2.addElement("price").setText("200");    //输出xml  System.out.println(document.asXML()); } 调用BaseOperation,输出结果为: <?xml version="1.0" encoding="UTF-8"?><root> <book type="science">  <Name>Java</Name>  <price>100</price> </book> <book type="science">  <Name>Oracle</Name>  <price>200</price> </book></root> (二)根据一个符合Document格式的字符串来生成一个Document  /**将字符串转化为Document  * @param str  输入的字符串  * @return  生成的document  * @throws DocumentException  */ public Document parserStrtoDocument(String str) throws DocumentException{  Document document=DocumentHelper.parseText(str);  return document; } 调用示例: String str="<root><book type='science'><Name>Java</Name><price>100</price></book></root>";   Document document = parserStrtoDocument(str);  System.out.println(document.asXML()); 输出结果为: <?xml version="1.0" encoding="UTF-8"?><root> <book type="science">  <Name>Java</Name>  <price>100</price> </book></root> (三)取得xml节点属性的基本方法  /**  * 取得xml的节点和属性的值  * @throws DocumentException   */ public void getBaseInfofromDocument() throws DocumentException{  String str="<root><book type='science'><Name>Java</Name><price>100</price></book></root>";  //生成一个Document  Document document = DocumentHelper.parseText(str);  //取得根结点  Element root=document.getRootElement();  //取得book节点  Element book=root.element("book");  //取得book节点的type属性的值  String type=book.attributeValue("type");  //取得Name节点  Element name=book.element("Name");  //取得书名  String bookname=name.getText();  //取得书的价钱  int price=Integer.parseInt(book.element("price").getText());    //输出书目信息  System.out.println("书名:"+bookname);  System.out.println("所属类别:"+type);  System.out.println("价格:"+price); } 调用getBaseInfofromDocument,输出结果为: 书名:Java所属类别:science价格:100 (四)利用迭代,xpath取得节点及其属性值  /**利用迭代,xpath取得xml的节点及其属性值  * @throws DocumentException  */ public void getComplexInfofromDocument() throws DocumentException{   String str="<root><book type='science'><Name>Java</Name><price>100</price></book>"   +"<book type='science'><Name>Oracle</Name><price>120</price></book>"   +"<book type='society'><Name>Society security</Name><price>130</price></book>"   +"<author><name>chb</name></author></root>";  //生成一个Document  Document document = DocumentHelper.parseText(str);    //提取类型为"society"的书  //此处需要添加支持xpath的jar包,详细见备注  Element society_book=(Element)document.selectSingleNode("/root/book[@type='society']");  System.out.println(society_book.asXML());    //提取价格节点的列表  System.out.println("-----------价格列表-------------");  List price=document.selectNodes("//price");  for(int i=0;i<price.size();i++){   Element elem_price=(Element)price.get(i);   System.out.println(elem_price.getText());  }    //循环根结点下的所有节点,若当前节点为book,则输出这本书的详细信息  System.out.println("-------------书目详情------------");  System.out.println("书名\t\t类别\t\t价格");  Element root=document.getRootElement();  Iterator iterator=root.elementIterator();  while(iterator.hasNext()){   Element element=(Element)iterator.next();   if(element.getName().equals("book")){    System.out.print(element.element("Name").getText()+"\t");    System.out.print(element.attributeValue("type")+"\t\t");    System.out.print(element.element("price").getText()+"\n");   }  }   //查找作者姓名  Element author=(Element)document.selectSingleNode("//author");  System.out.println("---------"+author.element("name").getText()+"----------");  //提取作者的所有书目名称  Iterator iterator_book=root.elementIterator("book");    while(iterator_book.hasNext()){   Element book=(Element)iterator_book.next();   System.out.print(book.element("Name").getText()+"\t");  }    //属性迭代  System.out.println("\n-------属性迭代--------");  String str1="<book type='science' name='Java' price='100'/>";  Document document1=DocumentHelper.parseText(str1);  //开始迭代  Iterator iterator_attribute=document1.getRootElement().attributeIterator();  while(iterator_attribute.hasNext()){   //提取当前属性   Attribute attribute=(Attribute)iterator_attribute.next();   System.out.println(attribute.getName()+":"+attribute.getValue());  } } 调用getComplexInfofromDocument,输出结果为: <book type="society"><Name>Society security</Name><price>130</price></book>-----------价格列表-------------100120130-------------书目详情------------书名  类别  价格Java science  100Oracle science  120Society security society  130---------chb----------Java Oracle Society security -------属性迭代--------type:sciencename:Javaprice:100 备注:调用该方法之前,应该先向工程中添加支持xpath的jar包,否则,会出现以下错误: java.lang.NoClassDefFoundError: org/jaxen/JaxenException at org.dom4j.DocumentFactory.createXPath(DocumentFactory.java:230) at org.dom4j.tree.AbstractNode.createXPath(AbstractNode.java:207) at org.dom4j.tree.AbstractNode.selectSingleNode(AbstractNode.java:183) at xml_chb.dom4j_chb.getComplexInfofromDocument(dom4j_chb.java:82) at xml_chb.dom4j_chb.main(dom4j_chb.java:92)Exception in thread "main" 只需要引入jaxen包就行了,我使用的是hibernate包中的jaxen-1.1-beta-7.jar包。


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



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



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

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