dom4j生成xml方法:
import java.io.*;
import org.dom4j.*;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
public class XmlTest {
/**
* @param args
*/
public static void main(String[] args) {
try {
File file=new File("D:/");
File[] files=file.listFiles();
File xmlfile=new File("D:/fileInfo.xml");
if(xmlfile.exists()){
xmlfile.createNewFile();
}
FileOutputStream fos = new FileOutputStream(xmlfile);
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("gbk");
XMLWriter writer = new XMLWriter(fos,format);
Document doc = DocumentHelper.createDocument();
Element rootElement = DocumentHelper.createElement("root");
rootElement.addAttribute("version", "2.0");
doc.setRootElement(rootElement);
for(int i=0;i<files.length;i++){
if(files[i].isFile()){
Element fileElement = rootElement.addElement("file");
Element nameElement = fileElement.addElement("name");
nameElement.addText(files[i].getName().substring(0, files[i].getName().indexOf(".")));
Element lengthElement = fileElement.addElement("length");
lengthElement.addText(Long.toString(files[i].length()));
Element cssElement = fileElement.addElement("css");
cssElement.addText(files[i].getName().substring(files[i].getName().indexOf(".")+1));
}
}
writer.write(doc);
} catch (IOException e) {
e.printStackTrace();
}
}
}
执行代码之后,将D盘下面生成一个名为fileInfo.xml的xml文件,将D盘下面原有的所有文件的信息保存在xml文件之中。
生成的fileInfo.xml的内容格式为:
<?xml version="1.0" encoding="gbk"?>
<root version="2.0">
<file>
<name>lianliankan_92368</name>
<length>2035066</length>
<css>exe</css>
</file>
<file>
<name>问题</name>
<length>487</length>
<css>txt</css>
</file>
</root>
name为文件名称,length为文件大小,css为文件格式。
==================================================
使用dom4j解析xml文件
使用到的xml文件:
读取文件并修改的java类:
使用到的jar包下载地址:http://l6.yunpan.cn/lk/Qv4MQBIubxvag
如果下载地址不能下载,可以到网上搜dom4j的jar包,也可以给我发邮件liygheart@yeah.net
下面粘一下代码:
UpdateXmlValue.java:
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class UpdateXmlValue {
public static Document getConfig() throws DocumentException{
File configFile = new File("src/user.xml");
//初始化一个解析器
SAXReader reader = new SAXReader();
Document doc = null;
doc = reader.read(configFile);
return doc;
}
public static void updateXml() throws DocumentException, IOException{
//获取xml的配置信息
Document doc = getConfig();
//获取root节点
Element root = doc.getRootElement();
//通过root节点向下找子节点userName
Element userName = root.element("userName");
//下面是获取userName的方法
System.out.println(userName.getTextTrim());
//重新设置userName节点的值
userName.setText("zhangsan11");
//下面是保存修改过后的文件,直接创建一个xml文件,名字与读取的文件名字一样,直接覆盖
XMLWriter out = new XMLWriter(new FileWriter("src/user.xml"));
//将设置过后的doc保存到本地
out.write(doc);
//关闭IO流
out.close();
}
public static void main(String[] args) throws DocumentException, IOException {
UpdateXmlValue.updateXml();
}
}
user.xml:
<?xml version="1.0" encoding="UTF-8"?>
<user>
<userName>zhangsan0</userName>
<pwd>zhangsan1</pwd>
<sex>man</sex>
<email>zhangsan0@163.com</email>
</user>
dom4j生成xml方法
最新推荐文章于 2021-06-16 15:49:55 发布
2317

被折叠的 条评论
为什么被折叠?



