dom4j 解析xml文件

0 篇文章 0 订阅

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值