首页 > 其他 > 详细

dom4j解析XML文档

时间:2014-03-27 10:00:05      阅读:506      评论:0      收藏:0      [点我收藏+]

xml

<?xml version="1.0" encoding="UTF-8"?>
<university name="pku">
    <college name="c1">
        <class name="class1">
            <student name="stu1" sex=‘male‘ age="21" />
            <student name="stu2" sex=‘female‘ age="20" />
            <student name="stu3" sex=‘female‘ age="20" />
        </class>
        <class name="class2">
            <student name="stu4" sex=‘male‘ age="19" />
            <student name="stu5" sex=‘female‘ age="20" />
            <student name="stu6" sex=‘female‘ age="21" />
        </class>
    </college>
    <college name="c2">
        <class name="class3">
            <student name="stu7" sex=‘male‘ age="20" />
        </class>
    </college>
    <college name="c3">
    </college>
</university>

 

 

 

 

 

 

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.ProcessingInstruction;
import org.dom4j.VisitorSupport;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

/**
* Dom4j读写xml
* @author whwang
*/
public class TestDom4j {
public static void main(String[] args) {
read1();
// read2();
write();
}

public static void read1() {
try {
SAXReader reader = new SAXReader();
InputStream in = TestDom4j.class.getClassLoader().getResourceAsStream("university.xml");
Document doc = reader.read(in);
Element root = doc.getRootElement();
readNode(root, "");
} catch (DocumentException e) {
e.printStackTrace();
}
}

@SuppressWarnings("unchecked")
public static void readNode(Element root, String prefix) {
if (root == null) return;
// 获取节点的属性
List<Attribute> attrs = root.attributes();
if (attrs != null && attrs.size() > 0) {
System.err.print(prefix);
for (Attribute attr : attrs) {
System.err.print(attr.getValue() + " ");
}
System.err.println();
}
// 获取他的子节点
List<Element> childNodes = root.elements();
prefix += "\t";
for (Element e : childNodes) {
readNode(e, prefix);
}
}

public static void read2() {
try {
SAXReader reader = new SAXReader();
InputStream in = TestDom4j.class.getClassLoader().getResourceAsStream("university.xml");
Document doc = reader.read(in);
doc.accept(new MyVistor());
} catch (DocumentException e) {
e.printStackTrace();
}
}



/**
* 写入方法
*/
public static void write() {
try {
// 创建一个xml文档
Document doc = DocumentHelper.createDocument();
Element university = doc.addElement("university");
university.addAttribute("name", "tsu");
// 注释
university.addComment("这个是根节点");
Element college = university.addElement("college");
college.addAttribute("name", "cccccc");
college.setText("text");

File file = new File("src/dom4j-modify.xml");
if (file.exists()) {
file.delete();
}
file.createNewFile();
XMLWriter out = new XMLWriter(new FileWriter(file));
out.write(doc);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

class MyVistor extends VisitorSupport {
public void visit(Attribute node) {
System.out.println("Attibute: " + node.getName() + "="
+ node.getValue());
}

public void visit(Element node) {
if (node.isTextOnly()) {
System.out.println("Element: " + node.getName() + "="
+ node.getText());
} else {
System.out.println(node.getName());
}
}

@Override
public void visit(ProcessingInstruction node) {
System.out.println("PI:" + node.getTarget() + " " + node.getText());
}
}

dom4j解析XML文档,布布扣,bubuko.com

dom4j解析XML文档

原文:http://www.cnblogs.com/bingbingJava/p/3624770.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!