用JDOM解析xml文件

 

     bakDiagramFile(filename,saName);/

     SAXBuilder sb = new SAXBuilder();

         try {

             Document doc = sb.build(filename );/filename是要解析的xml文件

             Element root = doc.getRootElement();

 

             List componentFlowEditorElements = root.getChildren();

             for (int i = 0; i < componentFlowEditorElements.size(); i++) {           

                 Element element = (Element) componentFlowEditorElements.get(i); 

                 if ("children".equals(element.getName()) || "edges".equals(element.getName())) {                              

                List children = element.getChildren();

                 for(int j=0;j<children.size();j++){

                 Element childrenElement =(Element) children.get(j);

                 if ("children".equals(childrenElement.getName())){

                 childrenElement.removeChild("element");

               }

                 }

                 }                

             }            

 

             XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat().setEncoding("ASCII"));

             File file2 = new File(filename);

             FileWriter writer = new FileWriter(file2);

             xmlOut.output(doc, writer);

             writer.close();

         } catch (JDOMException e) {

             e.printStackTrace();

         } catch (IOException e) {

             e.printStackTrace();

         } 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章