Java 解析XML dom4j
程序员文章站
2022-04-13 22:02:55
...
对于Java解析XML有很多种方法,不过用的最多应该就是dom4j了。先来无事,稍微研究一下它。
1.下载
下载地址:http://sourceforge.net/projects/dom4j
目前最高版本为1.6.1。
dom4j1.6.1的完整版大约11M,是一个名为dom4j-1.6.1.zip的压缩包,解压后有一个dom4j-1.6.1.jar文件,这就是应用时需要引入的类包,另外还需要引入jaxen-1.1-beta-6.jar文件,否则执行时可能抛java.lang.NoClassDefFoundError: org/jaxen/JaxenException异常,其他的包可以选择用之。
2.应用
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.CDATA;
import org.dom4j.Comment;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.DocumentType;
import org.dom4j.Element;
import org.dom4j.Namespace;
import org.dom4j.Node;
import org.dom4j.ProcessingInstruction;
import org.dom4j.Text;
import org.dom4j.VisitorSupport;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
/**
* @author:kenny dong
*/
public class Dom4JDemo {
/**
* @param args
* @throws DocumentException
* @throws IOException
*/
public static void main(String[] args) throws DocumentException, IOException {
Dom4JDemo demo = new Dom4JDemo();
Document doc = demo.read("D:/Demo.xml");
//demo.IteratorAllElement(demo.getRootElement(doc));
//demo.IteratorSpecialElement(demo.getRootElement(doc),"journal");
//demo.IteratorAttribute(demo.getRootElement(doc));
//demo.treeWalk(doc);
//doc.accept(new MyVisitor());
//demo.selectSingleNodeTest(doc);
demo.writeToFile(demo.createDocument());
demo.decorateOutputXML(demo.createDocument());
}
/**
* read from outside
* @param fileName
* @return
* @throws MalformedURLException
* @throws DocumentException
*/
public Document read(String fileName) throws MalformedURLException, DocumentException {
SAXReader reader = new SAXReader();
Document document = reader.read(new File(fileName));
return document;
}
/**
* get root element
* @param doc
* @return
*/
public Element getRootElement(Document doc){
return doc.getRootElement();
}
/**
* iterator all element by iterator
* @param root
*/
public void IteratorAllElement(Element root){
for ( Iterator i = root.elementIterator(); i.hasNext(); ) {
Element element = (Element) i.next();
System.out.println(element.getName() + "\n" + element.asXML());
}
}
/**
* iterator specail element by name
* @param root
* @param elementName
*/
public void IteratorSpecialElement(Element root,String elementName){
for ( Iterator i = root.elementIterator(elementName); i.hasNext();) {
Element element = (Element) i.next();
System.out.println(element.getName() + "\n" + element.asXML());
}
}
/**
* iterator all attributes
* @param root
*/
public void IteratorAttribute(Element root){
for ( Iterator i = root.attributeIterator(); i.hasNext(); ) {
Attribute attribute = (Attribute) i.next();
System.out.println(attribute.getName() + "\n" + attribute.asXML());
}
}
/**
* iterator all elements by recursion
* @param doc
*/
public void treeWalk(Document doc) {
treeWalk(getRootElement(doc));
}
public void treeWalk(Element element) {
for (int i = 0, size = element.nodeCount(); i < size; i++){
Node node = element.node(i);
if (node instanceof Element) {
treeWalk((Element) node);
} else {
System.out.println(node.asXML());
}
}
}
public void selectSingleNodeTest(Document doc){
Node node = doc.selectSingleNode("//journal/article");
System.out.println("select single node " + node.asXML());
}
public void selectNodesTest(Document document) {
List list = document.selectNodes("//journal/article" );
for(Object node : list){
if(node instanceof Element){
((Element) node).asXML();
}
}
}
public void findLinks(Document document) throws DocumentException {
List list = document.selectNodes("//a/@href");
for (Iterator iter = list.iterator(); iter.hasNext(); ) {
Attribute attribute = (Attribute) iter.next();
String url = attribute.getValue();
System.out.println("url " + url);
}
}
public Document stringToDocument(String str) throws DocumentException{
return DocumentHelper.parseText(str);
}
public Document createDocument() {
Document document = DocumentHelper.createDocument();// create docment
Element root = document.addElement("root");// create the root Element
root.addElement("author")// create child Element
.addAttribute("name", "James")// add attribute for the child Element
.addAttribute("location", "UK")
.addText("James Strachan")
.addComment("This is the first author");// add text for the child Element
root.addElement("author")
.addAttribute("name", "Bob")
.addAttribute("location", "US")
.addText("Bob McWhirter");
System.out.println(document.asXML());
return document;
}
public void writeToFile(Document doc) throws IOException{
XMLWriter writer = new XMLWriter(new FileWriter("d:/output.xml"));
writer.write(doc);
writer.close();
}
public void decorateOutputXML(Document doc) throws IOException{
FileWriter outputStream = new FileWriter("d:/output1.xml");
XMLWriter writer;
OutputFormat format;
format = OutputFormat.createPrettyPrint();//the default pretty printing format
//format = OutputFormat.createCompactFormat();//default compact format
//format = new OutputFormat("aaaa", false);// custom format
writer= new XMLWriter(outputStream, format );
writer.write(doc);
writer.close();
}
public void moidfyXML(Document doc){
Element root = doc.getRootElement();
Attribute author = root.attribute("author");// select attribute
root.remove(author);// remove attribute
root.addAttribute("user", "user_kenny");// add new attribute
}
}
class MyVisitor extends VisitorSupport {
public void visit(Element element){
System.out.println("element " + element.getName());
}
public void visit(Attribute attr){
System.out.println("atrribute " + attr.getName());
}
public void visit(Node node){
System.out.println("node " + node.asXML());
}
public void visit(Text text){
System.out.println("text " + text.asXML());
}
public void visit(ProcessingInstruction pro){
System.out.println("ProcessingInstruction " + pro.asXML());
}
public void visit(Namespace namespace){
System.out.println("Namespace " + namespace.asXML());
}
public void visit(Comment comment){
System.out.println("Comment " + comment.asXML());
}
public void visit(CDATA data){
System.out.println("CDATA " + data.asXML());
}
public void visit(DocumentType docType){
System.out.println("DocumentType " + docType.asXML());
}
public void visit(Document doc){
System.out.println("Document " + doc.asXML());
}
}
下一篇: DOM4J 代码演示