dom4j 读写xml文件
首先我们给出一段示例程序:
import
java.io.File;
import
java.io.FileWriter;
import
java.util.Iterator;
import
org.dom4j.Document;
import
org.dom4j.DocumentHelper;
import
org.dom4j.Element;
import
org.dom4j.io.OutputFormat;
import
org.dom4j.io.SAXReader;
import
org.dom4j.io.XMLWriter;
public
class
DOM4JTest
{
/** */
/**
*/
/** */
/**
*/
/** */
/**
*/
/** */
/**
* DOM4J读写XML示例
*
*
@param
args
*
@throws
Exception
*/
public
static
void
main(String[] args)
{
try
{
XMLWriter writer
=
null
;
//
声明写XML的对象
SAXReader reader
=
new
SAXReader();
OutputFormat format
=
OutputFormat.createPrettyPrint();
format.setEncoding(
"
GBK
"
);
//
设置XML文件的编码格式
String filePath
=
"
d:\\student.xml
"
;
File file
=
new
File(filePath);
if
(file.exi
相关文档:
测试用的xml文件
<?xml version="1.0" encoding="UTF-8"?>
<schools>
<school id='111'>测试学校</school>
<school id='222'>测试学校22
<class id='2.1'>测试班级222</class>
</school>
</schools>
测试用的JavaScript代码
$().ready(function ......
下载 dom4j-1.6.1.jar。
1: package org.zzp.common.xml.dom4j;
2:
3: import java.io.FileWriter;
4: import java.io.IOException;
5: import org.dom4j.Document;
6: import org.dom4j.DocumentHelper;
7: import org.dom4j.Element;
8: import org.dom4j.io.OutputFormat;
9: impor ......
需要dom4j.jar文件 ,自行下载。 test.xml 1: <?xml version="1.0" encoding="gbk"?>
2:
3: <students>
4: <person sex="男" age="21">
5: <id>1</id>
6: <name>章治鹏</name>
7: <homepage&g ......
Introduction to XML and XML With Java
If you are looking for sample programs to parse a XML file using DOM/SAX parser or looking for a program to generate a XML file please proceed directly to programs.
This small tutorial introduces you to the basic concepts of XML and using Xer ......