1、生成XML
-----------------------------------------------------------------
下面是解析和修改XML
XML文件内容如下:
修改
解析
- publicvoidcreateApplicationConfigXML(){
- //建立document对象
- try{
- Documentdocument=DocumentHelper.createDocument();
- Elementroot=document.addElement("root");//添加文档根
- root.addComment("这个一个注释");//加入一行注释
- Elementrequest=root.addElement("request");//添加root的子节点
- request.addAttribute("type","cat");
- request.addAttribute("flow","tong");
- request.addAttribute("time","2009");
- Elementpro=request.addElement("pro");
- pro.addAttribute("type","att");
- pro.addAttribute("name","附件");
- pro.addText("测试哈子");
- Elementcd=request.addElement("pro");
- cd.addAttribute("type","cd");
- cd.addAttribute("name","特殊字符过滤");
- cd.addCDATA("特殊字符");
- //输出全部原始数据,在编译器中显示
- OutputFormatformat=OutputFormat.createPrettyPrint();
- format.setEncoding("GBK");//根据需要设置编码
- XMLWriterwriter=newXMLWriter(System.out,format);
- document.normalize();
- writer.write(document);
- writer.close();
- //输出全部原始数据,并用它生成新的我们需要的XML文件
- XMLWriterwriter2=newXMLWriter(newFileWriter(newFile(
- "test.xml")),format);
- writer2.write(document);//输出到文件
- writer2.close();
- }catch(UnsupportedEncodingExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }catch(IOExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }
- }
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文件内容如下:
- <?xmlversion="1.0"encoding="utf-8"?>
- <root>
- <requesttype="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-0309:36:15">
- <protype="att"></pro>
- <protype="textarea"name="OP_bxryj"title="处理意见"need="true"></pro>
- </request>
- </root>
<?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>
修改
- publicStringgetApplcationConfigFromXMLTest(){
- Stringvalue="";
- try{
- SAXReadersax=newSAXReader();
- DocumentxmlDoc=sax.read(newFile(this.UBSSDIC_PATH));
- Elementroot=xmlDoc.getRootElement();//根节点
- Iteratorit=root.elementIterator();
- while(it.hasNext()){
- Elementele=(Element)it.next();
- Attributeattribute=ele.attribute("type");
- if(attribute.getStringValue().equals("Pending")){
- attribute.setValue("sendread2");//修改属性节点的值
- }
- AttributeflowType=ele.attribute("flowType");
- flowType.detach();//删除某个属性
- ele.addAttribute("type","Pending");//添加一个属性节点
- }
- Elementnew_cdata=root.addElement("new_cdata");//添加一个元素
- new_cdata.addCDATA("tst&ree");
- Elementnew_ele=root.addElement("new_ele");//添加一个元素
- new_ele.addText("33434343");
- Elementobj=(Element)root.selectObject("//pro[@type='att']");//根据XPath查找元素
- obj.setText("测试dddddd");//修改元素的值即text节点
- //输出全部原始数据,在编译器中显示
- OutputFormatformat=OutputFormat.createPrettyPrint();
- format.setEncoding("GBK");
- XMLWriterwriter=newXMLWriter(System.out,format);
- writer.write(xmlDoc);
- writer.close();
- //输出全部原始数据,并用它生成新的我们需要的XML文件
- XMLWriterwriter2=newXMLWriter(newFileWriter(newFile(
- "test.xml")),format);
- writer2.write(xmlDoc);//输出到文件
- writer2.close();
- }catch(DocumentExceptione){
- System.out.println(e.getMessage());
- e.printStackTrace();
- }catch(IOExceptione){
- e.printStackTrace();
- }
- returnvalue;
- }
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 ; }
解析
- publicvoidparseApplicationConfigXML(){
- try{
- SAXReadersax=newSAXReader();
- DocumentxmlDoc=sax.read(newFile("E:\\20090316HPS\\Dom4jTest\\t.xml"));
- Elementroot=xmlDoc.getRootElement();//根节点
- Iteratorit=root.elementIterator("request");
- while(it.hasNext()){
- Elementrequest=(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("-------------------------------------------");
- Listlist=root.selectNodes("//pro");
- for(inti=0;i<list.size();i++){
- Elementpro=(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(DocumentExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }