2014-01-29 59 views
0

這是我的XML文件如何使用Java

<?xml version="1.0" encoding="UTF-8"?> 
<playerxml> 
    <config> 
       <player type="videoplayer" sessionId="" buffrTime="9" width="1024" height="768"> 
      <xmlpath url="../config/interface_videoplayer.xml"/> 
      <preloadpath url="../presentation/nuggetplayer/preloader.swf"/> 
      <assetfolder url="../presentation/nuggetplayer"/> 
      <content url="../content/N1939"/> 
      <keyboard url="../config/keys.xml"/> 
     </player> 
    </config> 
    <structure heading="Being Alive - What Does It Mean?" classId="" totaltime="7:31"> 
     <filename id= "" displayName="What Is Living?" name="class11_bio_being_alive-what_does_i_mean_01.swf" url="/DigischoolCMS/fileEd.do?n_id=MN3000&amp;swf_id=1" supportingFile="" type="swf"></filename> 
     <filename id= "" displayName="Cellular Organisation" name="class11_bio_being_alive-what_does_i_mean_02.swf" url="/DigischoolCMS/fileEd.do?n_id=MN3000&amp;swf_id=2" supportingFile="" type="swf"></filename> 
     <filename id= "" displayName="Exam Focus" name="class11_bio_being_alive-what_does_i_mean_03.swf" url="/DigischoolCMS/fileEd.do?n_id=MN3000&amp;swf_id=3" supportingFile="" type="swf" nav = "false"></filename> 
     <filename id= "" displayName="Growth" name="class11_bio_being_alive-what_does_i_mean_04.swf" url="/DigischoolCMS/fileEd.do?n_id=MN3000&amp;swf_id=4" supportingFile="" type="swf"></filename> 
     <filename id= "" displayName="Exam Focus" name="class11_bio_being_alive-what_does_i_mean_05.swf" url="/DigischoolCMS/fileEd.do?n_id=MN3000&amp;swf_id=5" supportingFile="" type="swf" nav="false"></filename> 
     <filename id= "" displayName="Reproduction" name="class11_bio_being_alive-what_does_i_mean_06.swf" url="/DigischoolCMS/fileEd.do?n_id=MN3000&amp;swf_id=6" supportingFile="" type="swf"></filename> 
     <filename id= "" displayName="Exam Focus" name="class11_bio_being_alive-what_does_i_mean_07.swf" url="/DigischoolCMS/fileEd.do?n_id=MN3000&amp;swf_id=7" supportingFile="" type="swf" nav="false"></filename> 
     <filename id= "" displayName="Response to Stimuli" name="class11_bio_being_alive-what_does_i_mean_08.swf" url="/DigischoolCMS/fileEd.do?n_id=MN3000&amp;swf_id=8" supportingFile="" type="swf"></filename> 
     <filename id= "" displayName="Exam Focus" name="class11_bio_being_alive-what_does_i_mean_09.swf" url="/DigischoolCMS/fileEd.do?n_id=MN3000&amp;swf_id=9" supportingFile="" type="swf" nav = "false"></filename> 
     <filename id= "" displayName="Summary" name="class11_bio_being_alive-what_does_i_mean_10.swf" url="/DigischoolCMS/fileEd.do?n_id=MN3000&amp;swf_id=10" supportingFile="" type="swf" nav = "false"></filename> 
    </structure> 
</playerxml> 

假設我想刪除的4個一種
filename屬性刪除特定行XML這是我的代碼

 try { 
      String path="D://test//N2019_set1.xml"; 
      File structureXml = new File(path); 
      SAXBuilder saxb = new SAXBuilder(); 
      Document document = saxb.build(structureXml); 
      Element rootElement = document.getRootElement(); 
      XMLOutputter xmlOutput = new XMLOutputter(); 
      List playerList = rootElement.getChildren(); 
      for (int i = 0; i < playerList.size(); i++) { 
       Element structureList = (Element) playerList.get(i); 
       if(structureList.getName().equalsIgnoreCase("structure")){ 
        List optList = structureList.getChildren(); 
        System.out.println(optList.size()); 
        for (int k = 0; k < optList.size(); k++) { 
         Element option = (Element) optList.get(k); 
         String a; 
         String b="swf_id="+5; 

         if(option.getName().equalsIgnoreCase("filename")){ 
structureList.removeContent(4); 
}} 
       }   
       FileOutputStream file=new FileOutputStream(path); 
       xmlOutput.output(document, file); 
     }catch (JDOMException ex) { 
      ex.printStackTrace(); 
     } catch (IOException ex) { 
      ex.printStackTrace(); 
     } 

我想此結果

<?xml version="1.0" encoding="UTF-8"?> 
<playerxml> 
    <config> 
       <player type="videoplayer" sessionId="" buffrTime="9" width="1024" height="768"> 
      <xmlpath url="../config/interface_videoplayer.xml"/> 
      <preloadpath url="../presentation/nuggetplayer/preloader.swf"/> 
      <assetfolder url="../presentation/nuggetplayer"/> 
      <content url="../content/N1939"/> 
      <keyboard url="../config/keys.xml"/> 
     </player> 
    </config> 
    <structure heading="Being Alive - What Does It Mean?" classId="" totaltime="7:31"> 
     <filename id= "" displayName="What Is Living?" name="class11_bio_being_alive-what_does_i_mean_01.swf" url="/DigischoolCMS/fileEd.do?n_id=MN3000&amp;swf_id=1" supportingFile="" type="swf"></filename> 
     <filename id= "" displayName="Cellular Organisation" name="class11_bio_being_alive-what_does_i_mean_02.swf" url="/DigischoolCMS/fileEd.do?n_id=MN3000&amp;swf_id=2" supportingFile="" type="swf"></filename> 
     <filename id= "" displayName="Exam Focus" name="class11_bio_being_alive-what_does_i_mean_03.swf" url="/DigischoolCMS/fileEd.do?n_id=MN3000&amp;swf_id=3" supportingFile="" type="swf" nav = "false"></filename> 

     <filename id= "" displayName="Exam Focus" name="class11_bio_being_alive-what_does_i_mean_05.swf" url="/DigischoolCMS/fileEd.do?n_id=MN3000&amp;swf_id=5" supportingFile="" type="swf" nav="false"></filename> 
     <filename id= "" displayName="Reproduction" name="class11_bio_being_alive-what_does_i_mean_06.swf" url="/DigischoolCMS/fileEd.do?n_id=MN3000&amp;swf_id=6" supportingFile="" type="swf"></filename> 
     <filename id= "" displayName="Exam Focus" name="class11_bio_being_alive-what_does_i_mean_07.swf" url="/DigischoolCMS/fileEd.do?n_id=MN3000&amp;swf_id=7" supportingFile="" type="swf" nav="false"></filename> 
     <filename id= "" displayName="Response to Stimuli" name="class11_bio_being_alive-what_does_i_mean_08.swf" url="/DigischoolCMS/fileEd.do?n_id=MN3000&amp;swf_id=8" supportingFile="" type="swf"></filename> 
     <filename id= "" displayName="Exam Focus" name="class11_bio_being_alive-what_does_i_mean_09.swf" url="/DigischoolCMS/fileEd.do?n_id=MN3000&amp;swf_id=9" supportingFile="" type="swf" nav = "false"></filename> 
     <filename id= "" displayName="Summary" name="class11_bio_being_alive-what_does_i_mean_10.swf" url="/DigischoolCMS/fileEd.do?n_id=MN3000&amp;swf_id=10" supportingFile="" type="swf" nav = "false"></filename> 
    </structure> 
</playerxml> 

刪除第4個文件名

+0

而你看到的結果是什麼? – Gangnus

回答

0

首先使用xpath獲取要刪除的元素。 第二個document.remove(Element)。

並保存。

+0

有7個文件名的元素,然後哪一個被刪除..如何找到? – vijayk

+0

看看xpaht在文檔上做了些什麼,例如document.selectNodes(「// playerxml/structure/filename」)會給你所選元素的列表。 – masarka

0

我修改了源代碼以使用迭代器來遍歷子元素。這將允許迭代器刪除元素。我假設它是具有屬性displayName =「Growth」的元素。如果不是,請澄清或試驗。

String path="c:/work/play/java/test.xml"; 
File structureXml = new File(path); 
SAXBuilder saxb = new SAXBuilder(); 
Document document = saxb.build(structureXml); 
Element rootElement = document.getRootElement(); 
XMLOutputter xmlOutput = new XMLOutputter(); 
List structureList = rootElement.getChildren("structure"); 
Iterator itr = structureList.iterator(); 
while (itr.hasNext()) { 
    Element structure = (Element)itr.next(); 
List elementList = structure.getChildren(); 
Iterator itr2 = elementList.iterator(); 
while (itr2.hasNext()) { 
    Element option = (Element)itr2.next(); 
    System.out.println(option.toString()); 
    System.out.println(option.getName()); 
     System.out.println(option.getAttributeValue("displayName")); 
     if(option.getName().equalsIgnoreCase("filename") && 
       option.getAttributeValue("displayName").compareTo("Growth")==0){ 
      itr2.remove(); 
     } 
} 
}