如何从Java中的XML中删除子元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20675639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove child element from XML in java?
提问by vijayk
This is my XML file
这是我的 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>
I just want to remove 2nd one option from my xml.
My java code remove all option from my options element. by using option.getParentElement().removeChild("option");
我只想从我的 xml 中删除第二个选项。
我的 java 代码从我的选项元素中删除所有选项。通过使用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();
}
my output is:
我的输出是:
<?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>
but I want like this.
但我想要这样。
<?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>
采纳答案by default locale
Element.removeChildwill remove only the first child with the given name. You can use Element.removeContent(int index)to remove child element by index
Element.removeChild将仅删除具有给定名称的第一个孩子。您可以使用Element.removeContent(int index)按索引删除子元素
if (ques.getName().equalsIgnoreCase("options")) {
ques.removeContent(2);
}
or Element.removeContent(Content content)to remove specific element.
或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);
}
}
You said you want to remove the second option, but in your example third one is removed. I'm slightly confused, so change index if necessary.
您说要删除第二个选项,但在您的示例中,删除了第三个选项。我有点困惑,因此如有必要,请更改索引。
回答by shreyansh jogi
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");
}
}
}
just focus on the loop and iterate from last index -1 that i have done in code.
只关注循环并从我在代码中完成的最后一个索引 -1 开始迭代。