如何使用java删除XML中的特定行

时间:2014-01-29 12:48:50

标签: java dom jdom jdom-2

这是我的XML文件

<?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个的文件名属性 这是我的代码

        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个文件名

2 个答案:

答案 0 :(得分:0)

首先使用xpath为get元素删除你想要删除的内容。 第二个文件。删除(元素)。

并保存。

答案 1 :(得分:0)

我修改了源代码以使用Iterator遍历子元素。这将允许Iterator删除元素。我假设它是具有属性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();
        }
}
}