how to delete specific row in XML using java

1.2k Views Asked by At

This is my XML file

<?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>

suppose I want to remove filename attribute of 4 th one
this is my code

        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();
        }

I want this result

<?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>

remove the 4th one filename

2

There are 2 best solutions below

2
On

First use xpath for get element what you want to delete. Second document.remove(Element).

And save it.

0
On

I modified the source code to use an Iterator to traverse the child element. This will allow for the Iterator to remove the element. I assume it is the Element with attribute displayName="Growth". If not, please clarify or experiment.

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();
        }
}
}