dom4j 解析xml文件
来源:互联网 发布:护卫神php 编辑:程序博客网 时间:2024/05/16 19:13
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"));// 继续添加一个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包
- xml文件解析-DOM4J
- dom4j解析Xml文件
- DOM4J 解析XML文件
- dom4j解析xml文件
- dom4j解析XML文件
- dom4j解析xml文件
- dom4j 解析xml文件
- dom4j解析xml文件
- dom4j解析XML文件
- Dom4J解析XML文件
- dom4j解析xml文件
- Dom4j解析xml文件
- Dom4j 解析xml文件
- dom4j解析xml文件
- dom4j解析xml文件
- dom4j解析xml文件
- dom4j解析xml文件
- DOM4J解析XML文件
- NAND Flash的驱动程序设计
- PHP 日期时间基本操作
- 10 000小时的标准----是否有先天的才能
- .NET中公共变量与属性的区别(get/set)[转]
- 码间干扰
- dom4j 解析xml文件
- Java 下载文件不在浏览器中直接打开
- PHP替换字符串中指定字符串 替换指定的次数
- 两个RichTextBox同步滚动的实现
- Oracle几条有用的SQL语句
- java模板方法设计
- [ZeroMQ] 汇总帖
- 有史以来十大互联网数字媒体并购
- 用timb计算程序的运行时间