一、生成xml文件
1. 下载dom4j-1.6.1.jar和jaxen-1.1-beta-10.jar,下载地址分别如下:
http://www.dom4j.org/
http://jaxen.codehaus.org/releases.html
注:本文附带的源码中有以上两个JAR文件。
2. 写一个批处理文件dom4j.bat,内容如下:
set CLASSPATH=c:\test\jar\jaxen-1.1-beta-10.jar;c:\test\jar\dom4j-1.6.1.jar;%CLASSPATH%
3. 编写代码(编译前运行一下dom4j.bat),成功运行后会在cmd内显示生成的xml文件的内容,而且在C盘下会生成“output.xml”文件。大部分代码摘自官方网站的quick start,我做了些修正,因为官方的不能直接运行。代码如下:
import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter; import java.io.IOException; import java.io.FileWriter; public class CreateXML { // 输出XML数据 public void write(Document document) throws IOException { // xml 标准格式,保存到文件 OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter writer = new XMLWriter(new FileWriter( "c:\\output.xml" ), format ); writer.write( document ); writer.close(); // 紧凑的格式,System.out显示( format = OutputFormat.createCompactFormat(); writer = new XMLWriter( System.out, format ); System.out.println("\r\n------------------Start------------------"); writer.write( document ); System.out.println("\r\n-------------------End-------------------"); writer.close(); } // 返回XML对象 public Document createDocument() { Document document = DocumentHelper.createDocument(); Element root = document.addElement( "root" ); Element author1 = root.addElement( "author" ) .addAttribute( "name", "James" ) .addAttribute( "location", "UK" ) .addText( "James Strachan" ); Element author2 = root.addElement( "author" ) .addAttribute( "name", "Bob" ) .addAttribute( "location", "US" ) .addText( "Bob McWhirter" ); return document; } public static void main(String[] args) { CreateXML dom4j = new CreateXML(); try { dom4j.write(dom4j.createDocument()); } catch (IOException e) { System.out.println(e.getMessage()); } } }
4. 运行过程和结果:
C:\test>dom4j.bat
C:\test>set CLASSPATH=c:\test\jar\jaxen-1.1-beta-10.jar;c:\test\jar\dom4j-1.6.1.
jar;.;c:\jdk1.5\lib
C:\test>javac CreateXML.java
C:\test>java CreateXML
------------------Start------------------
<?xml version="1.0" encoding="UTF-8"?>
<root><author name="James" location="UK">James Strachan</author><author name="Bob" location="US">Bob McWhirter</author></root>
-------------------End-------------------
C:\test>
二、获取XML文件中的数据
代码如下:
import java.net.URL; import java.io.IOException; import java.io.FileWriter; import java.io.File; import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.io.SAXReader; public class Parse_1 { public Document parse(String sfile) throws DocumentException { SAXReader reader = new SAXReader(); Document document = reader.read(new File(sfile)); return document; } public void write(Document document) throws IOException { // xml的格式,System.out显示( OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter writer = new XMLWriter( System.out, format ); System.out.println("\r\n------------------Start------------------"); writer.write( document ); System.out.println("\r\n-------------------End-------------------"); } public static void main(String[] args) { Parse_1 dom4j = new Parse_1(); try { dom4j.write(dom4j.parse("c:\\output.xml")); } catch (IOException e) { System.out.println(e.getMessage()); } catch (DocumentException e) { System.out.println(e.getMessage()); } } }
运行过程和结果:
C:\test>dom4j
C:\test>set CLASSPATH=c:\test\jar\jaxen-1.1-beta-10.jar;c:\test\jar\dom4j-1.6.1.
jar;.;c:\jdk1.5\lib
C:\test>javac Parse_1.java
C:\test>java Parse_1
------------------Start------------------
<?xml version="1.0" encoding="UTF-8"?>
<root>
<author name="James" location="UK">James Strachan</author>
<author name="Bob" location="US">Bob McWhirter</author>
</root>
-------------------End-------------------
C:\test>
三、获取、修改XML元素
代码如下:
import java.io.IOException; import java.io.File; import java.util.Iterator; import java.util.List; import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter; import org.dom4j.io.SAXReader; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Node; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.Attribute; public class Parse_2 { public Document parse(String sfile) throws DocumentException { SAXReader reader = new SAXReader(); Document document = reader.read(new File(sfile)); return document; } public void bar(Document document) throws IOException { List list = document.selectNodes( "//author" ); Iterator iter=list.iterator(); System.out.println("\r\n****** 获取的数据如下 ******"); while(iter.hasNext()){ Element titleElement=(Element)iter.next(); // 修改xml元素 if(titleElement.getText().equals("James Strachan")) { titleElement.setText("Faint Heipark"); } System.out.print(titleElement.valueOf( "@name" ) + ":"); System.out.println(titleElement.getText()); } // 输出全部原始数据 OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter writer = new XMLWriter( System.out, format ); System.out.println("\r\n------------------Start------------------"); writer.write( document ); System.out.println("\r\n-------------------End-------------------"); } public static void main(String[] args) { Parse_2 dom4j = new Parse_2(); try { dom4j.bar(dom4j.parse("c:\\output.xml")); } catch (Exception e) { System.out.println(e.getMessage()); } } }
运行结果:
C:\test>java Parse_2
****** 获取的数据如下 ******
James:Faint Heipark
Bob:Bob McWhirter
------------------Start------------------
<?xml version="1.0" encoding="UTF-8"?> <root> <author name="James" location="UK">Faint Heipark</author> <author name="Bob" location="US">Bob McWhirter</author> </root>
-------------------End-------------------