如何从Java中的XML中删除子元素? [英] How to remove children element from XML in java?

查看:76
本文介绍了如何从Java中的XML中删除子元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从xml中删除子元素。
我的XML文件是

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>

如果我想先删除问题,该怎么办?
输出XML ..

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>

我的Java代码删除问题一。

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


推荐答案

此代码首先删除问题。而且有效。

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

这篇关于如何从Java中的XML中删除子元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆