2013-12-19 33 views
1

這是我的XML文件如何在java中從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代碼從我的選項元素中刪除所有選項。通過使用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> 

回答

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

你說你想刪除第二個選項,但在你的例子中第三個選項被刪除。我有點困惑,所以如有必要請更改索引。

+0

PLZ看到這個問題..http://stackoverflow.com/questions/20782400/how-to-remove-children-element-from-xml-in-java/20782520?noredirect = 1#20782520 – vijayk

+0

@vijayk實際上它是同樣的問題。將'option'更改爲'quest'並選擇索引0。 –

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迭代。

+0

bt它只刪除第一個選項元素。 – vijayk

+0

你試過了嗎?它刪除最後一個 –

+0

'是的,我試過了。 – vijayk