1 package cn.mars.app.txn.whpf; 2 3 import java.io.FileOutputStream; 4 5 import org.dom4j.Attribute; 6 import org.dom4j.Document; 7 import org.dom4j.DocumentHelper; 8 import org.dom4j.Element; 9 import org.dom4j.io.OutputFormat; 10 import org.dom4j.io.XMLWriter; 11 12 public class Dom4jTest { 13 14 public static void main(String[] args) { 15 // 创建文档。 16 Document document = DocumentHelper.createDocument(); 17 // 设置文档DocType,这里为了举例,添加hibernate的DocType 18 document.addDocType("hibernate-configuration", "-//Hibernate/Hibernate Configuration DTD 3.0//EN", 19 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"); 20 // 文档增加节点,即根节点,一个文档只能有一个根节点,多加出错 21 Element root = document.addElement("skills"); 22 // 添加注释 23 root.addComment("第一个技能"); 24 // 根节点下添加节点 25 Element first = root.addElement("skill"); 26 // 节点添加属性 27 first.addAttribute("name", "独孤九剑"); 28 // 节点下添加节点 29 Element info = first.addElement("info"); 30 // 节点设置内容数据 31 info.setText("为独孤求败所创,变化万千,凌厉无比。其传人主要有风清扬、令狐冲。"); 32 33 // 同理增加其他节点,内容,属性等 34 Element second = root.addElement("skill"); 35 second.addAttribute("name", "葵花宝典"); 36 Element info2 = second.addElement("info"); 37 info2.setText("宦官所创,博大精深,而且凶险至极。练宝典功夫时,首先要自宫净身。"); 38 39 // 创建节点 40 Element third = DocumentHelper.createElement("skill"); 41 // 将节点加入到根节点中 42 root.add(third); 43 // 创建属性,第一个参数指定了拥有者,也可以为null,指定拥有者 44 Attribute name = DocumentHelper.createAttribute(third, "name", "北冥神功"); 45 // 将属性加入到节点上 46 third.add(name); 47 // 创建子节点并加入到节点中 48 Element info3 = DocumentHelper.createElement("info"); 49 info3.setText("逍遥派的顶级内功之一,能吸人内力转化为自己所有,威力无穷。"); 50 third.add(info3); 51 52 try { 53 // 创建格式化类 54 OutputFormat format = OutputFormat.createPrettyPrint(); 55 // 设置编码格式,默认UTF-8 56 format.setEncoding("UTF-8"); 57 // 创建输出流,此处要使用Writer,需要指定输入编码格式,使用OutputStream则不用 58 FileOutputStream fos = new FileOutputStream("d:/skills.xml"); 59 // 创建xml输出流 60 XMLWriter writer = new XMLWriter(fos, format); 61 // 生成xml文件 62 writer.write(document); 63 writer.close(); 64 } catch (Exception e) { 65 e.printStackTrace(); 66 } 67 } 68 }
输出文件内容:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 3 4 <skills> 5 <!--第一个技能--> 6 <skill name="独孤九剑"> 7 <info>为独孤求败所创,变化万千,凌厉无比。其传人主要有风清扬、令狐冲。</info> 8 </skill> 9 <skill name="葵花宝典"> 10 <info>宦官所创,博大精深,而且凶险至极。练宝典功夫时,首先要自宫净身。</info> 11 </skill> 12 <skill name="北冥神功"> 13 <info>逍遥派的顶级内功之一,能吸人内力转化为自己所有,威力无穷。</info> 14 </skill> 15 </skills>
1 SAXReader reader = new SAXReader(); 2 Document document = reader.read(new File("d:/skills.xml"));
实例代码:使用DOMReader解析
1 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 2 DocumentBuilder db = dbf.newDocumentBuilder(); 3 File file = new File("d:/skills.xml"); 4 org.w3c.dom.Document domDocument = db.parse(file); 5 DOMReader reader = new DOMReader(); 6 org.dom4j.Document document = reader.read(domDocument);
1 Element root = document.getRootElement();
实例代码:访问所有子节点
List skills = root.elements(); for (Iterator<?> it = skills.iterator(); it.hasNext();) { Element e = (Element) it.next(); }
实例代码:访问指定名称的节点,如访问名称为“skill”的全部节点
1 List skills = root.elements("skill"); 2 for (Iterator<?> it = skills.iterator(); it.hasNext();) { 3 Element e = (Element) it.next(); 4 //TODO 5 }
实例代码:访问指定名称的第一个节点
1 Element skill = root.element("skill");
1 for(Iterator<Element> it = root.elementIterator();it.hasNext();){ 2 Element e = it.next(); 3 //TODO 4 }
1 Element skill = root.element("skill"); 2 Attribute attr1 = skill.attribute("name");
1 Element skill = root.element("skill"); 2 Attribute attr2 = skill.attribute(0);
1 Element skill = root.element("skill"); 2 List<Attribute> list = skill.attributes(); 3 for (Iterator<Attribute> it = list.iterator(); it.hasNext();) { 4 Attribute attr = it.next(); 5 // TODO 6 }
1 Element skill = root.element("skill"); 2 for (Iterator<Attribute> it = skill.attributeIterator(); it.hasNext();) { 3 Attribute attr = it.next(); 4 // TODO 5 }
1 List list = document.selectNodes("//skills/skill/@name"); 2 for (Iterator it = list.iterator(); it.hasNext();) { 3 Attribute attr = (Attribute) it.next(); 4 //TODO 5 }
1 Element root = document.getRootElement(); 2 Element skill = root.element("skill"); 3 root.remove(skill);
1 Element skill = root.element("skill"); 2 skill.remove(skill.attribute("name"));
1 Element skill = root.element("skill"); 2 skill.setName("new_skill"); 3 skill.setText("你好");
1 Attribute attr = skill.attribute("name"); 2 attr.setText("newName");
1 String docXmlText = document.asXML(); 2 String rootXmlText = root.asXML();
1 String skillString = "<skill name="xxx">神龙摆尾</skill>"; 2 Document d = DocumentHelper.parseText(skillString);
1 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 2 xmlns="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" 3 version="1.0"> 4 <xsl:template match="foo"> 5 <hr/> 6 </xsl:template> 7 </xsl:stylesheet>
原文:https://www.cnblogs.com/3xiaoleilei/p/13662352.html