dom4j 解析xml文件

来源:互联网 发布:护卫神php 编辑:程序博客网 时间:2024/05/16 19:13

dom4j解析xml分析总结:

概括来说, 一个xml文件由三个要素组成:

  1. 元素: <project></project>
  2. 属性: <project name="blog"></project>
  3. 元素值与属性值: <project name="blog">blog text</project>
因此创建, 解析xml文档也就是围绕这几个要素来展开的, dom4j一些主要的方法有:

  • 元素
    • 创建元素 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
另外元素和属性还有getName() 和setName() 方法设置元素或属性的名字 

迭代元素:

  • 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"));// 继续添加一个buildCommondbuildCommand = 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包