XML之DOM4、JDom等解析方式
发布时间:2020-12-16 09:43:00 所属栏目:百科 来源:网络整理
导读:1、生成XML Java代码 public void createApplicationConfigXML(){ //建立document对象 try { Documentdocument=DocumentHelper.createDocument(); Elementroot=document.addElement( "root" ); //添加文档根 root.addComment( "这个一个注释" ); //加入一行
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 block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.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 ; } 解析
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |