如何从java中删除XML中的子元素?

时间:2013-12-19 07:21:21

标签: java xml dom jdom jdom-2

这是我的XML文件

<?xml version="1.0" encoding ="utf-8" ?>
<mcss>
    <quest ans="1"> 
        <question><![CDATA[Write 5x= 3y-1 as linear equation form.]]></question>
        <options>
            <option><![CDATA[5x-3y+1=0]]></option>
            <option><![CDATA[-5x-3y-1=0]]></option>
            <option><![CDATA[5x+3y+1=0]]></option>          
        </options>
        <explaination><![CDATA[Recall the linear equation in two variables form.]]></explaination>
    </quest>
</mcss>

我只是想从我的xml中删除第二个选项 我的java代码从我的options元素中删除所有选项。使用option.getParentElement().removeChild("option");

try {
    String path="D://test//n2027_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 qestList = rootElement.getChildren();
    for (int i = 0; i < qestList.size(); i++) {
        Element quesList = (Element) qestList.get(i);
        System.out.println(quesList.getAttributeValue("ans"));
        //change ans field
        quesList.setAttribute("ans", ""+i);
        List qList = quesList.getChildren();
        for(int a=0;a< qList.size();a++) {
            Element ques =(Element) qList.get(a);
            if(ques.getAttributeValue("file")!=null){
                //read xml
                System.out.println(ques.getAttributeValue("file"));
                //write xml attribute
                //System.out.println(ques.setAttribute("file","dasd"+a));
            }
            if(ques.getName().equalsIgnoreCase("question")){
                //read 
                System.out.println(ques.getTextTrim());
                ques.addContent(new CDATA(ques.getTextTrim()));
            }
            if (ques.getName().equalsIgnoreCase("options")) {
                List optList = ques.getChildren();
                for (int k = 0; k < optList.size(); k++) {
                    Element option = (Element) optList.get(k);
                    if(option.getName().equalsIgnoreCase("option")){
                        //read 
                        option.getParentElement().removeChild("option");
                    }
                }
            }
            if(ques.getName().equalsIgnoreCase("explaination")){
                ques.removeContent();
                ques.addContent(new CDATA("explaination"+a));
            }
        }
    }         
    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" ?>
<mcss>
    <quest ans="1"> 
        <question><![CDATA[Write 5x= 3y-1 as linear equation form.]]></question>
        <options>

        </options>
        <explaination><![CDATA[Recall the linear equation in two variables form.]]></explaination>
    </quest>
</mcss>

但我想要这样。

<?xml version="1.0" encoding ="utf-8" ?>
<mcss>
    <quest ans="1"> 
        <question><![CDATA[Write 5x= 3y-1 as linear equation form.]]></question>
        <options>
            <option><![CDATA[5x-3y+1=0]]></option>
            <option><![CDATA[-5x-3y-1=0]]></option>

        </options>
        <explaination><![CDATA[Recall the linear equation in two variables form.]]></explaination>
    </quest>
</mcss>

2 个答案:

答案 0 :(得分:4)

Element.removeChild将仅移除具有给定名称的第一个孩子。您可以使用Element.removeContent(int index)按索引删除子元素

if (ques.getName().equalsIgnoreCase("options")) {
    ques.removeContent(2);
}

Element.removeContent(Content content)删除特定元素。

if (ques.getName().equalsIgnoreCase("options")) {
    List<Element> options = ques.getChildren("option");
    if(options.size()>2) {
        Element optionToRemove = options.get(2);
        ques.removeContent(optionToRemove);
    }
}    

您说要删除第二个选项,但在您的示例中,第三个选项被删除。我有点困惑,所以如果有必要改变索引。

答案 1 :(得分:0)

if (ques.getName().equalsIgnoreCase("options")) {
                         List optList = ques.getChildren();
                         for (int k = optList.size()-1; k < optList.size(); k++) {
                             Element option = (Element) optList.get(k);
                             if(option.getName().equalsIgnoreCase("option")){
                                //read 
                                option.getParentElement().removeChild("option");
                            }
                         }
                    }

只关注循环并从我在代码中完成的最后一个索引-1进行迭代。