java通过dom4j创建xml文件
2012-06-11 15:13
281 查看
/**
* @param foldPath 各个子文件夹的xml的存放位置
* @param xmlPath 总xml创建后的存放位置
*/
public static void createDescriptionXML(String[] foldPath,String xmlPath){
Document document= DocumentHelper.createDocument();
Element root=document.addElement("polaroids");
root.addElement("color").setText("0x0099CC");
root.addElement("title").setText("description");
root.addElement("music").setText("music.mp3");
root.addElement("contact").setText("contact@contact.com");
root.addElement("galleries").setText("INFCN");
Element ele=root.addElement("galleries");
int p=0;
for(int i=0;i<foldPath.length;i++){
p=p+1;
ele.addElement("gallery").addAttribute("dir", foldPath[i]).setText("fold"+p);
}
doc2Xml(document,xmlPath);
}
//将document写入的xml文件
public static int doc2Xml(Document doc,String xmlPath){
OutputFormat format=OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
XMLWriter xmlWriter=null;
File file=new File(xmlPath);
int flag=0;
try {
if(!file.getParentFile().exists()){//目标文件夹不存在
file.getParentFile().mkdirs();
}
xmlWriter=new XMLWriter(new FileWriter(file),format);
xmlWriter.write(doc);
} catch (IOException e) {
// TODO Auto-generated catch block
flag=1;
e.printStackTrace();
}finally{
if(xmlWriter!=null){
try {
xmlWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return flag;
}
* @param foldPath 各个子文件夹的xml的存放位置
* @param xmlPath 总xml创建后的存放位置
*/
public static void createDescriptionXML(String[] foldPath,String xmlPath){
Document document= DocumentHelper.createDocument();
Element root=document.addElement("polaroids");
root.addElement("color").setText("0x0099CC");
root.addElement("title").setText("description");
root.addElement("music").setText("music.mp3");
root.addElement("contact").setText("contact@contact.com");
root.addElement("galleries").setText("INFCN");
Element ele=root.addElement("galleries");
int p=0;
for(int i=0;i<foldPath.length;i++){
p=p+1;
ele.addElement("gallery").addAttribute("dir", foldPath[i]).setText("fold"+p);
}
doc2Xml(document,xmlPath);
}
//将document写入的xml文件
public static int doc2Xml(Document doc,String xmlPath){
OutputFormat format=OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
XMLWriter xmlWriter=null;
File file=new File(xmlPath);
int flag=0;
try {
if(!file.getParentFile().exists()){//目标文件夹不存在
file.getParentFile().mkdirs();
}
xmlWriter=new XMLWriter(new FileWriter(file),format);
xmlWriter.write(doc);
} catch (IOException e) {
// TODO Auto-generated catch block
flag=1;
e.printStackTrace();
}finally{
if(xmlWriter!=null){
try {
xmlWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return flag;
}
相关文章推荐
- Java通过Dom4j来解析和创建XML文件
- java--通过DOM方式创建xml文件
- JAVA创建XML文件(三)---DOM4J方式创建XML
- 如何通过Java DOM4J读取xml文件
- JAVA--通过DOM4J的方式读取xml文件简单实例
- java类——使用dom4j创建xml文件
- Java-创建和解析XML文件(之)使用Dom4j
- 通过Dom4J操作(创建、读取和修改)XML文件
- 番外 01:Spring IoC 实现原理简析,Java的反射机制,通过类名创建对象
- Java读取、创建xml(通过dom方式)
- java中使用dom4j解析XML文件的方法教程
- Java操作XML文件 dom4j 篇
- JAVA通过继承Thread来创建线程
- 在java代码中创建xml文件
- java解析xml文件(三种方式-(dom ,jdom ,dom4j)
- Java操作XML文件 dom4j 篇
- 通过代码模拟dom4j解析spring的ApplicationContext XML文件
- JAVA使用Dom4j实现字符串和xml文件相互转换
- java--通过DOM4J方式生成rss文件简单实例
- 通过Eclipse创建一个Project ,Java Project 和Tomcat Project 生成的目录和文件