How to remove children element from XML in java?

2019-08-26 20:37发布

I want to remove child element from xml.
My XML file is

    <mcss>
        <quest ans="0"> 
            <question><![CDATA[ This is question one]]></question>
            <options>
                <option><![CDATA[B<Option one]]></option>
                <option><![CDATA[B<Option second]]></option>
                <option><![CDATA[B<Option three]]></option>
            </options>
            <explaination><![CDATA[explaination one]]></explaination>
        </quest>

        <quest ans="0"> 
            <question><![CDATA[ This is question two]]></question>
            <options>
                <option><![CDATA[B<Option one]]></option>
                <option><![CDATA[B<Option second]]></option>
                <option><![CDATA[B<Option three]]></option>
            </options>
            <explaination><![CDATA[explaination two]]></explaination>
        </quest>
</mcss>

if I want to remove question first the how can i do? output XML..

    <?xml version="1.0" encoding="UTF-8"?>
    <mcss>
        <quest ans="0"> 
            <question><![CDATA[ This is question two]]></question>
            <options>
                <option><![CDATA[B<Option one]]></option>
                <option><![CDATA[B<Option second]]></option>
                <option><![CDATA[B<Option three]]></option>
            </options>
            <explaination><![CDATA[explaination two]]></explaination>
        </quest>
</mcss>

My java Code to remove question one.

    String path="D://test//N2074_set2.xml";
            File structureXml = new File(path);
            SAXBuilder saxb = new SAXBuilder();
            Document document = saxb.build(structureXml);
            Element rootElement = document.getRootElement();
            XMLOutputter xmlOutput = new XMLOutputter();

            for (int i = 0; i < qestList.size(); i++) {
            Element quesList = (Element) qestList.get(2);
            if(quesList.getName().equalsIgnoreCase("quest"))
                rootElement.removeContent(2);

        }
FileOutputStream file=new FileOutputStream(path);

            xmlOutput.output(document, file);

3条回答
该账号已被封号
2楼-- · 2019-08-26 20:44

I think you have missed an easy solution to the problem. If you have any JDOM Content (Element, Attributed, Text, etc.), you can remove it from its parent using the detach() method. In your case though, you may want to use an Iterator to go through the Child Elements of the root node, and then remove the matching content from the Iterator.... :

for (Iterator<Element> questit = rootElement.getChildren("quest").iterator();
         questit.hasNext();) {
    if (" This is question one".equals(questit.next().getChildText("question")) {
         questit.remove();
    }
}

Note that you cannot use detach() inside an iterator because that will cause a ConcurrentModificationException.

查看更多
爷、活的狠高调
3楼-- · 2019-08-26 20:49

You can go through each question node and match for your desire question to be removed, if it is match then You can remove it's parent node.

查看更多
SAY GOODBYE
4楼-- · 2019-08-26 20:50

this code delete question first. and it works.

 Document document = saxb.build(structureXml);
                Element rootElement = document.getRootElement();
                XMLOutputter xmlOutput = new XMLOutputter();
                List qestList = rootElement.getChildren();
                Element quesList = (Element) qestList.get(0);
                if(quesList.getName().equalsIgnoreCase("quest")){
                    rootElement.removeContent(quesList);
                }
                FileOutputStream outputStream=new FileOutputStream(path);
                xmlOutput.output(document, outputStream);
查看更多
登录 后发表回答