java修改xml

上几天工作需要大量的xml文档,文档的docid不能重复,所以就写了一个方法来实现,记录下来以免丢掉。

 

View Code
 1 private static void ChangeId(File file,int i)throws DocumentException, IOException{
 2         UUID  uuid = UUID.randomUUID();
 3         SAXReader saxReader = new SAXReader();
 4            Document document = saxReader.read(file);
 5            List list = document.selectNodes("/document/header/docinfo/id/@value" );
 6            Iterator iter=list.iterator();
 7            while(iter.hasNext()){
 8                Attribute attribute=(Attribute)iter.next();
 9                attribute.setValue("2.16.156.12345.1.1000.204.403.167303."+i); 
10             }
11            File f = new File("C:/ufida/deploy/EHR_GATEWAY/EHR_GATEWAY_ADPT/XMLDATA/"+i/10000+"/");
12            f.mkdir();
13            OutputFormat format = OutputFormat.createPrettyPrint();
14             format.setEncoding("utf-8");
15         XMLWriter output = new XMLWriter(
16                   ( new FileOutputStream("C:/ufida/deploy/EHR_GATEWAY/EHR_GATEWAY_ADPT/XMLDATA/"+i/10000+"/"+uuid.toString()+".xml") ),format);
17                  output.write( document );
18                  output.close();
19 
20     }

 

posted @ 2012-05-11 14:56  爱我所艾  阅读(733)  评论(0编辑  收藏  举报