dom4j生成xml文件
2016-02-01 11:40
453 查看
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 ;
}
}
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 ;
}
}
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序