使用DOM4J和JDOM分别创建和解析XML
下载dom4j-1.6.1.jar和jdom-1.0.jar并加入CLASSPATH,下面是两个分别使用DOM4J和JDOM创建和解析XML的Java类:
JdomTest .java
package com.lph;
import java.io.File;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.jdom.Comment;
import org.jdom.Document;
import org.jdom.Element;import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
/**
* 使用JDOM解析和创建XML文档
* @author Administrator
*
*/public class JdomTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JdomTest t=new JdomTest();
t.parseRss();
t.outputDocument();
}
/**
* 解析RSS XML文档
*
*/
public static void parseRss(){
Document doc=null;
try {
SAXBuilder builder = new SAXBuilder();
doc = builder.build(new File("E:/rss.xml"));} catch(Exception e) {
e.printStackTrace();
}
Element root = doc.getRootElement();// Get the channel element (only one occurs)
Element channel = root.getChild("channel");
// 1..n item elements
List items = channel.getChildren("item");
Iterator i = items.iterator();
while (i.hasNext()) {
Element item = (Element) i.next();// get description element
Element elDesc = item.getChild("description");
String strDesc = "";
if (elDesc != null) {
strDesc = elDesc.getTextTrim();
}// get link element
Element elLink = item.getChild("link");
String strLink = "";
if (elLink != null) {
strLink = elLink.getTextTrim();
} else {
// try 'guid'
// ... http://radio.weblogs.com/0112015/rss.xml
elLink = item.getChild("guid");
if (elLink != null) {
strLink = elLink.getTextTrim();} else {
}
}
}
}
/**
* 创建XML文档
*
*/
public static void outputDocument() {
// Create the root element
Element carElement = new Element("car");
//create the document
Document myDocument = new Document(carElement);
//add an attribute to the root element
// carElement.addAttribute(new Attribute("vin", "123fhg5869705iop90"));//add a comment
carElement.addContent(new Comment("Description of a car"));//add some child elements
/*
* Note that this is the first approach to adding an element and
* textual content. The second approach is commented out.
*/
Element make = new Element("make");
make.addContent("Toyota");
carElement.addContent(make);
//carElement.addContent(new Element("make").addContent("Toyota"));//add some more elements
carElement.addContent(new Element("model").addContent("Celica"));
carElement.addContent(new Element("year").addContent("1997"));
carElement.addContent(new Element("color").addContent("green"));
try {
XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
outputter.output(myDocument, System.out);
} catch (Exception e) {
e.printStackTrace();
}
}}
DOM4JTest .java
package com.lph;
import java.io.*;
import java.util.Iterator;import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.DocumentHelper;import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.dom4j.io.SAXReader;
/**
* 使用DOM4J解析和创建XML文档
* @author Administrator
*
*/
public class DOM4JTest {
public static void main(String[] args) {
try {
DOM4JTest modelReder = new DOM4JTest();
modelReder.testWrite();
modelReder.testRead();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 解析RSS xml文档
* @throws Exception
*/
public void testRead() throws Exception{
SAXReader reader = new SAXReader();
Document document = reader.read(new FileInputStream("E:/rss.xml"));
Element root = document.getRootElement();
// for (Iterator iter = root.elementIterator(); iter.hasNext();) {
Element element = root.element("channel");//(Element) iter.next();
if("channel".equals(element.getName())){
for (Iterator iter2 = element.elementIterator("item"); iter2.hasNext();) {
Element element2 = (Element) iter2.next();
System.out.println("title:"+element2.element("title").getTextTrim());
// System.out.println("description:"+element2.element("description").getTextTrim());
}
}// }
}
/**
* 创建XML文档并输出文件
* @throws Exception
*/
public void testWrite() throws Exception{
Document document = DocumentHelper.createDocument();
Element root = document.addElement("root");
Element element1 = root.addElement("user")
.addAttribute("name","Alexander")
.addAttribute("blog", "http://netnova.blogjava.net")
.addText("我是中文的名字");
XMLWriter writer = new XMLWriter(new FileOutputStream("E:/output.xml"),OutputFormat.createPrettyPrint());
writer.write(document);
writer.close();
}
}