dom4j解析xml分析总结:
概括来说, 一个xml文件由三个要素组成:
- 元素: <project></project>
- 属性: <project name="blog"></project>
- 元素值与属性值: <project name="blog">blog text</project>
- 元素
- 创建元素 Element element = new BaseElement("name")
- 添加元素到父元素上 fatherElement.add(element) 或者 fatherElement.addElement(element)
- 删除元素fatherElement.remove(element)
- 属性
- 创建属性, 因为属性不能单独存在, 所以创建属性一般是根据元素来创建的 element.addAttribute("name", "value");
- 得到属性, Attribute attr = element.attribute("name")
- 删除属性 element.remove(attr)
- 值
- 元素和属性取值 有addText() 和 setText() , 前者返回元素或属性, 后者返回void
迭代元素:
- Element.elements(), Element.elements("name"), 前者迭代所有元素, 后者迭代名字为"name"的元素
迭代属性:
- Element.attributes()
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.dom4j.tree.BaseElement;
public class XmlUtil {
public void createDocument(){
Document doc = DocumentHelper.createDocument();
// 创建根元素, 由文档来创建
Element root = doc.addElement("projectDescription");
root.addComment("An Xml projectDescription");
// 创建元素
Element name = new BaseElement("name");
// 添加值
name.addText("blog");
root.add(name);
Element comment = root.addElement("comment");
Element projects = root.addElement("projects");
projects.addAttribute("name", "测试");
projects.addAttribute("attr", "属性");
projects.addElement("project").addText("blog");
projects.addElement("project").addAttribute("name", "post").addText("post");
Element buildSpec = root.addElement("buildSpec");
Element buildCommand = buildSpec.addElement("buildCommand");
buildCommand.addElement("name").addText("org.eclipse.jdt.core.javabuilder");
buildCommand.add(new BaseElement("arguments"));
// 继续添加一个buildCommond
buildCommand = buildSpec.addElement("buildCommand");
buildCommand.addElement("name").addText("org.springframework.ide.eclipse.core.springbuilder");
buildCommand.add(new BaseElement("arguments"));
Element natures = root.addElement("natures");
natures.addElement("nature").addText("org.springframework.ide.eclipse.core.springnature");
natures.addElement("nature").addText("org.eclipse.jdt.core.javanature");
System.out.println(doc.asXML());
try{
XMLWriter out = new XMLWriter(new FileWriter(new File("project.xml")));
out.write(doc);
out.close();
}catch(IOException e){
e.printStackTrace();
}
}
public void readDocument(){
Document doc = null;
SAXReader reader = new SAXReader();
try {
doc = reader.read(new FileReader(new File("project.xml")));
} catch (Exception e) {
e.printStackTrace();
}
// 取得根元素
Element root = doc.getRootElement();
// 取得元素
Element projects = root.element("projects");
// 取得属性
Attribute attr = projects.attribute("name");
// 取得元素值, 元素名字
String elementText = projects.getText().trim();
String elementName = projects.getName().trim();
// System.out.println(elementText + "#" + elementName);
// 取得属性值,属性名字
String attrText = attr.getText().trim();
String attrName = attr.getName().trim();
// System.out.println(attrText + "#" + attrName);
// 迭代元素
List<Element> projectList = projects.elements("project");
for (Element project : projectList){
// System.out.println(project.getName().trim() + "#" + project.getText().trim());
}
// 迭代属性
List<Attribute> attrs = projects.attributes();
for (Attribute projectAttr : attrs){
// System.out.println(projectAttr.getName().trim() + "#" + projectAttr.getText().trim());
}
// 迭代buildSpec下面的元素
Element buildSpec = root.element("buildSpec");
List buildCommands = buildSpec.elements("buildCommand");
Iterator<Element> iter = buildCommands.iterator();
while (iter.hasNext()){
Element buildCommand = iter.next();
// System.out.println(buildCommand.getName() + "#" + buildCommand.getTextTrim());
List<Element> childrenBuildCommand = buildCommand.elements();
for (Element e : childrenBuildCommand){
// System.out.println(e.getName() + "#" + e.getTextTrim());
}
}
// 删除元素
root.remove(root.element("natures"));
// 删除属性
projects.remove(projects.attribute("name"));
// 删除文本, 注意只会删除第一个project 元素的文本值
projects.element("project").setText("");
// System.out.println(doc.asXML());
// 利用XPath快速找到节点
projectList = doc.selectNodes("/projectDescription/projects/project");
for (Element project : projectList){
// System.out.println(project.getName().trim() + "#" + project.getText().trim());
}
}
public static void main(String args[]){
// new XmlUtil().createDocument();
new XmlUtil().readDocument();
}
}
createDocument()方法是根据.project文件来创建的, 方便对照, 另外为了测试属性迭代, 特意增加了几个没有的属性。
如果要使用XPath, 则在build path中还需要加入jaxen包