dom4j 解析、修改、生成XML 文件

来源:互联网 发布:淘宝极速退款没有了 编辑:程序博客网 时间:2024/05/21 08:47


项目中用到XML的地方非常普遍,XML也不是一种新的技术。但经常遇到朋友们问如何操作XML文件。所以写一个小例子,代码很乱。写出来和大家一起交流学习

1、生成XML

public void createApplicationConfigXML(){         //建立document对象 try { Document document = DocumentHelper.createDocument(); Element root = document.addElement("root");//添加文档根 root.addComment("这个一个注释");//加入一行注释 Element request = root.addElement("request"); //添加root的子节点 request.addAttribute("type", "cat"); request.addAttribute("flow", "tong"); request.addAttribute("time", "2009"); Element pro = request.addElement("pro"); pro.addAttribute("type", "att"); pro.addAttribute("name", "附件"); pro.addText("测试哈子");  Element cd = request.addElement("pro"); cd.addAttribute("type", "cd"); cd.addAttribute("name", "特殊字符过滤"); cd.addCDATA("特殊字符");              //输出全部原始数据,在编译器中显示 OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("GBK");//根据需要设置编码 XMLWriter writer = new XMLWriter(System.out, format); document.normalize(); writer.write(document);   writer.close(); // 输出全部原始数据,并用它生成新的我们需要的XML文件 XMLWriter writer2 = new XMLWriter(new FileWriter(new File(             "test.xml")), format); writer2.write(document); //输出到文件 writer2.close();} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}


-----------------------------------------------------------------
下面是解析和修改XML
XML文件内容如下:

<?xml version="1.0" encoding="utf-8"?><root><request type="Pending" flowType="GENERAL" flowName="报销流程"docId="185647" flowId="16409" nodeName="报销人确认" wikId="58288"sendId="1210040" userId="1210040" createDate="2009-12-03"title="费用报销 " flowCreaterId="1210040" nodeType="1"bosTime="2009-12-03 09:36:15"><pro type="att"></pro><pro type="textarea" name="OP_bxryj" title="处理意见" need="true"></pro></request></root>


修改

public String getApplcationConfigFromXMLTest(){String value = "";try {SAXReader sax = new SAXReader();Document xmlDoc = sax.read(new File(this.UBSSDIC_PATH));Element root = xmlDoc.getRootElement();//根节点Iterator it = root.elementIterator();while(it.hasNext()){Element ele = (Element)it.next();Attribute attribute = ele.attribute("type");if(attribute.getStringValue().equals("Pending")){attribute.setValue("sendread2");//修改属性节点的值}Attribute flowType = ele.attribute("flowType");flowType.detach();//删除某个属性ele.addAttribute("type", "Pending");//添加一个属性节点}Element new_cdata = root.addElement("new_cdata");//添加一个元素new_cdata.addCDATA("tst&ree");Element new_ele = root.addElement("new_ele");//添加一个元素new_ele.addText("33434343");Element obj = (Element)root.selectObject("//pro[@type='att']");//根据XPath查找元素obj.setText("测试dddddd");//修改元素的值 即text节点              //输出全部原始数据,在编译器中显示           OutputFormat format = OutputFormat.createPrettyPrint();           format.setEncoding("GBK");           XMLWriter writer = new XMLWriter(System.out, format);           writer.write(xmlDoc);             writer.close();           // 输出全部原始数据,并用它生成新的我们需要的XML文件           XMLWriter writer2 = new XMLWriter(new FileWriter(new File(             "test.xml")), format);           writer2.write(xmlDoc); //输出到文件           writer2.close();} catch (DocumentException e) {System.out.println(e.getMessage());e.printStackTrace();}catch(IOException e){e.printStackTrace();}return value ;}


解析

public void parseApplicationConfigXML(){try {SAXReader sax = new SAXReader();Document xmlDoc = sax.read(new File("E:\\20090316HPS\\Dom4jTest\\t.xml"));Element root = xmlDoc.getRootElement();//根节点Iterator it = root.elementIterator("request");while(it.hasNext()){Element request = (Element)it.next();System.out.println(request.getName());System.out.println(request.attributeValue("type"));System.out.println(request.attributeValue("flow"));System.out.println(request.attributeValue("time"));}System.out.println("-------------------------------------------");List list = root.selectNodes("//pro");for(int i = 0; i < list.size(); i++){Element pro = (Element)list.get(i);System.out.println(pro.getName());System.out.println(pro.attributeValue("type"));System.out.println(pro.attributeValue("name"));System.out.println(pro.getText());System.out.println("+++++++++++++++++++++++++++++++++");}} catch (DocumentException e) {// TODO Auto-generated catch blocke.printStackTrace();}}





0 0
原创粉丝点击