惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Docker
I
InfoQ
L
LangChain Blog
阮一峰的网络日志
阮一峰的网络日志
Y
Y Combinator Blog
博客园_首页
Martin Fowler
Martin Fowler
宝玉的分享
宝玉的分享
A
About on SuperTechFans
Apple Machine Learning Research
Apple Machine Learning Research
Vercel News
Vercel News
T
The Blog of Author Tim Ferriss
C
Check Point Blog
B
Blog RSS Feed
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Engineering at Meta
Engineering at Meta
B
Blog
爱范儿
爱范儿
Stack Overflow Blog
Stack Overflow Blog
aimingoo的专栏
aimingoo的专栏
WordPress大学
WordPress大学
F
Fortinet All Blogs
月光博客
月光博客
GbyAI
GbyAI

博客园 - 老苏

(转).NET 3.5新特性 在 .NET Framework 2.0 中未处理的异常导致基于 ASP.NET 的应用程序意外退出 转:理解javascript中的delete机制(3) 转:理解javascript中的delete机制(2) 转:理解javascript中的delete机制(1) Java NIO类库Selector机制解析(上) VSS 2005 复位 工作目录(Reset Working Folder) SWT Display.getDefault() 和Display.getCurrent()的区别 网页纯JS日期时间控件 网页打印2-打印界面实现 A4纸网页打印中对应像素的设定和换算 axman 的专栏,专业,真专业 在ASP.NET 2.0中实现本地化 取消XP的视频预览功能 WEB程序员也要学习学习安全防护(一) 用Delphi编写ASP的ActiveX 兼容性 无提示关闭窗口 为何 .NET 总是BUG不断? 2005年的春天
读写XML(1)-关于Dom4j 生成xml文件,以及格式化的xml文件的...
老苏 · 2008-07-21 · via 博客园 - 老苏

   项目中遇到了读写xml文件的问题,下面贴上代码再逐一解释

 1private void saveToXmlFile(ProcessInfo[] infos)
 2        {
 3            if (dom == null)
 4            {
 5                loadProcessInfo(true);
 6            }

 7            XMLWriter writer = null;
 8            try
 9            {
10                OutputFormat format = OutputFormat.createPrettyPrint();
11                writer = new XMLWriter(new FileWriter(filepath), format);
12                if (dom != null)
13                {
14                    dom.clearContent();
15                }

16                dom = DocumentHelper.createDocument();
17                dom.setXMLEncoding("utf-8");
18                
19                Element root = dom.addElement("processes");
20                for (int i = 0; i < infos.length; i++)
21                {
22                    info2Element(root, infos[i]);
23                }

24                
25                writer.write(dom);
26                writer.flush();
27            }

28            catch (IOException e)
29            {
30                log.error("存储XML文件出错", e);
31            }

32            finally
33            {
34                if (writer != null)
35                {
36                    try
37                    {
38                        writer.close();
39                    }

40                    catch (IOException e)
41                    {
42                        // TODO Auto-generated catch block
43                        e.printStackTrace();
44                    }

45                }

46            }

47            
48            
49        }

创建xml 的主要类 DocumentHelper
见Line:16
dom = DocumentHelper.createDocument();
dom.setXMLEncoding("utf-8");
创建Document对象并设置编码
写入文件的主要类XMLWriter
格式化xml的辅助类OutputFormat
其创建过程参见Line 10
OutputFormat format = OutputFormat.createPrettyPrint();
writer = new XMLWriter(new FileWriter(filepath), format);
然后Document创建根Element
info2Element()的方法则是将对象转换到Element结构中去。
格式化的主要功臣就是 OutputFormat  相应的还有createCompactFormat() 创建压缩的xml文件,删去了所有的换行等无用的字符。createPrettyPrint() 则是生成格式化的xml 代码,让看起来好读一点。