Remove child tags/elements

时间:2017-06-15 10:10:18

标签: xml xsd xml-parsing

i have a few milons rows of xml. There are the same parent tag, but more then one child tags, one of them child tags have a value which is *. My question is, how can i remove all of parents tags where the child tag contains * Thank you for your advice!

1 个答案:

答案 0 :(得分:0)

如果您知道如何使用XPath,这是一个非常简单的应用程序。它基于VTD-XML。我使用的XPath是/ root / comment [change =' *']。

import com.ximpleware.*;
import java.io.*;
public class XPathRemove {

    public static void main(String[] args) throws VTDException, IOException{
        // TODO Auto-generated method stub
        String inputXML = "<root><comment><change/></comment><comment><change>*</change></comment></root>";
        VTDGen vg= new VTDGen();
        vg.setDoc(inputXML.getBytes());
        vg.parse(false);
        VTDNav vn = vg.getNav();
        AutoPilot ap= new AutoPilot(vn);
        ap.selectXPath("/root/comment[change='*']");
        XMLModifier xm = new XMLModifier(vn);
        int i=0;
        while((i=ap.evalXPath())!=-1){
            xm.remove(); // remove all the selected comment node
        }
        xm.output("d:\\xml\\XPathRemove.xml");
    }

}