当前位置: 首页 > 工具软件 > dom4j > 使用案例 >

dom4j(2)

阚小云
2023-12-01

//修改特定元素

try{
      SAXReader saxReader = new SAXReader();
      Document document = saxReader.read(new File("c:/lbm.xml"));
      List list = document.selectNodes("//spring1/@publisher" );
      Iterator iter=list.iterator();
      while(iter.hasNext()){
      Attribute attribute=(Attribute)iter.next();
      if(attribute.getValue().equals("lbm"))
         attribute.setValue("libaoming");
      } 
      OutputFormat format = OutputFormat.createPrettyPrint();
      format.setEncoding("gbk");
      XMLWriter output = new XMLWriter( new FileWriter(new File("c:/libaoming.xml")),format);
         output.write( document );
         output.close();     
     }
     catch(Exception e) {
      System.out.println(e.getMessage());
     }
     System.out.print("complent");

 类似资料: