這是我的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&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&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&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&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&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&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&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&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&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&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&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&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&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&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&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&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&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&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&swf_id=10" supportingFile="" type="swf" nav = "false"></filename>
</structure>
</playerxml>
刪除第4個文件名
而你看到的結果是什麼? – Gangnus