DOM4J实例

来源:互联网 发布:mysql source失败 编辑:程序博客网 时间:2024/06/03 21:26

dom4j生成xml文件

 

import java.io.File;
import java.io.FileWriter;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;

 

public class createXml {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
 /* holen.xml 
     <?xml version="1.0" encoding="UTF-8"?>
    <books>
     <!--This is a test for dom4j, holen, 2004.9.11-->
     <book show="yes">
    <title>Dom4j Tutorials</title>
    </book>
    <book show="yes">
    <title>Lucene Studing</title>
     </book>
    <book show="no">
    <title>Lucene in Action</title>
    </book>
     <owner>O'Reilly</owner>
    </books> */
   createXMLFile();

 }
 public static void createXMLFile(){
  Document document = DocumentHelper.createDocument();
  Element booksElement = document.addElement("books");
  booksElement.addComment("This is a test for dom4j, holen, 2004.9.11");
  Element bookElement = booksElement.addElement("book");
  bookElement.addAttribute("show","yes");
  Element titleElement = bookElement.addElement("title");
  titleElement.setText("Dom4j Tutorials");
  bookElement = booksElement.addElement("book");
  bookElement.addAttribute("show","yes");
  titleElement = bookElement.addElement("title");
  titleElement.setText("Lucene Studing");
  bookElement = booksElement.addElement("book");
  bookElement.addAttribute("show","no");
  titleElement = bookElement.addElement("title");
  titleElement.setText("Lucene in Action");
  Element ownerElement = booksElement.addElement("owner");
  ownerElement.setText("O'Reilly");
  try{
   XMLWriter writer = new XMLWriter(new FileWriter(new File("e://jwp.xml")));
   writer.write(document);
   writer.close();
   }catch(Exception ex){
    ex.printStackTrace();
   }
  /*
   建立一个XML文档
      *//**
     * 建立一个XML文档,文档名由输入属性决定
     * @param filename 需建立的文件名
      * @return 返回操作结果, 0表失败, 1表成功
     *//*
     public int createXMLFile(String filename){
     *//** 返回操作结果, 0表失败, 1表成功 *//*
      int returnValue = 0;
     *//** 建立document对象 *//*
     Document document = DocumentHelper.createDocument();
     *//** 建立XML文档的根books *//*
     Element booksElement = document.addElement("books");
      *//** 加入一行注释 *//*
     booksElement.addComment("This is a test for dom4j, holen, 2004.9.11");
     *//** 加入第一个book节点 *//*
      Element bookElement = booksElement.addElement("book");
     *//** 加入show属性内容 *//*
     bookElement.addAttribute("show","yes");
      *//** 加入title节点 *//*
     Element titleElement = bookElement.addElement("title");
     *//** 为title设置内容 *//*
      titleElement.setText("Dom4j Tutorials");
     *//** 类似的完成后两个book *//*
      bookElement = booksElement.addElement("book");
      bookElement.addAttribute("show","yes");
     titleElement = bookElement.addElement("title");
     titleElement.setText("Lucene Studing");
     bookElement = booksElement.addElement("book");
      bookElement.addAttribute("show","no");
     titleElement = bookElement.addElement("title");
     titleElement.setText("Lucene in Action");
     *//** 加入owner节点 *//*
     Element ownerElement = booksElement.addElement("owner");
      ownerElement.setText("O'Reilly");
     try{
     *//** 将document中的内容写入文件中 *//*
     XMLWriter writer = new XMLWriter(new FileWriter(new File(filename)));
     writer.write(document);
      writer.close();
     *//** 执行成功,需返回1 *//*
     returnValue = 1;
      }catch(Exception ex){
     ex.printStackTrace();
     }
      return returnValue;
     } 
     说明:
     Document document = DocumentHelper.createDocument();
     通过这句定义一个XML文档对象。
      Element booksElement = document.addElement("books");
     通过这句定义一个XML 元素,这里添加的是根节点。
     Element有几个重要的方法:
     l addComment:添加注释
      l addAttribute:添加属性
     l addElement:添加子元素
     最后通过XMLWriter生成物 理文件,默认生成的XML文件排版格式比较乱,可以通过OutputFormat类的createCompactFormat()方法或 createPrettyPrint()方法格式化输出,默认采用createCompactFormat()方法,显示比较紧凑,这点将在后面详细谈 到。
     生成后的holen.xml文件内容如下:
     <?xml version="1.0" encoding="UTF-8"?>
     <books><!--This is a test for dom4j, holen, 2004.9.11--><book show="yes"><title>Dom4j Tutorials</title></book><book show="yes"><title>Lucene Studing</title></book><book show="no"><title>Lucene in Action</title></book><owner>O'Reilly</owner></books> 
*/
   return ;
 }
 
}

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 我考驾照过期了怎么办 驾照过了审验期怎么办 驾照过期3年了怎么办 如果学车过期了怎么办 行驶证过期了3年怎么办 行驶证过期没审怎么办 行驶证过期两年怎么办 驾驶证过期7年了怎么办 换驾照过一个月怎么办? 行驶证正本掉了怎么办 车的行驶证丢了怎么办 三星s6 屏幕坏了怎么办 手机摔成黑屏了怎么办 三星屏幕漏液了怎么办 三星s8屏幕漏液怎么办 屏幕紫色漏液了怎么办 华为手机屏碎了怎么办 小米2s按键失灵怎么办 魅蓝屏幕摔花了怎么办 小米手机屏碎了怎么办 厦门医保卡坏了怎么办 医保卡丢了北京怎么办 重庆社保卡丢了怎么办 沈阳医保卡丢了怎么办 小孩医保卡丢了怎么办 少儿医保卡丢了怎么办 孩子医保卡丢了怎么办 医保卡存折丢了怎么办 济宁医保卡丢了怎么办 大连医保卡丢了怎么办 职工医保卡丢了怎么办 太原医保卡丢了怎么办 南京医保卡丢了怎么办 苏州医保卡丢了怎么办 我的医保卡丢了怎么办 医保卡丢了怎么办出院 住院医保卡丢了怎么办 社会医保卡丢了怎么办 医保本丢了,住院怎么办 临时医保卡丢了怎么办 没带医保卡看病怎么办