jdom 修改 xml文件 保存

 1 /*
 2  * To change this template, choose Tools | Templates
 3  * and open the template in the editor.
 4  */
 5 
 6 package analyzewdsl;
 7 
 8 import java.io.File;
 9 import java.io.FileOutputStream;
10 import java.io.IOException;
11 import java.net.URLDecoder;
12 import java.util.logging.Level;
13 import java.util.logging.Logger;
14 import org.jdom.Document;
15 import org.jdom.Element;
16 import org.jdom.JDOMException;
17 import org.jdom.input.SAXBuilder;
18 import org.jdom.output.XMLOutputter;
19 import org.jdom.xpath.XPath;
20 
21 /**
22  *
23  * @author root
24  */
25 public class Main {
26 
27     private Document doc;
28 
29     private Element element;
30 
31 
32 
33     public static void main(String[] args) {
34         new Main().wsdl();
35     }
36 
37     /**
38      * @param args the command line arguments
39      */
40     public  void wsdl() {
41         // TODO code application logic here
42 
43         SAXBuilder builder=new SAXBuilder();
44         try {
45 
46 
47             String currentJarPath = URLDecoder.decode(Main.class.getProtectionDomain().getCodeSource().getLocation().getFile()+"!Hello.wsdl""UTF-8");
48             System.out.println(currentJarPath);
49 
50             doc = builder.build(currentJarPath);
51             element=doc.getRootElement();
52 
53             Element e=(Element)XPath.selectSingleNode(element, "//xsd:import");
54 
55 
56             e.setAttribute("schemaLocation""aaa");
57             System.out.println(e.getAttributeValue("schemaLocation").toString());
58 
59 
60             XMLOutputter out = new XMLOutputter();
61             
62 //            String s=this.getClass().getResource("/").toString();
63 //            System.out.println(s);
64 //
65             out.output(doc,new FileOutputStream(new File(currentJarPath.replaceAll("file:",""))));
66 
67         } catch (JDOMException ex) {
68             Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
69         } catch (IOException ex) {
70             Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
71         }
72     }
73 
74 }
75 


posted @ 2009-12-23 17:56  busing  阅读(875)  评论(0)    收藏  举报