Remove XML Node using java parser

前端 未结 4 1079
迷失自我
迷失自我 2020-12-02 21:41

In the below sample XML, how to remove Entire B Node if E=13 using java parser.


   
     
       
         11         


        
4条回答
  •  南笙
    南笙 (楼主)
    2020-12-02 21:58

    Here is the code of removing node B using XPath with predicate. It is based on VTD-XML, which uniquely implements incremental update.

    import com.ximpleware.*;
    import java.io.*;
    
    public class removeNode {
        public static void  main(String s[]) throws VTDException, IOException{
            VTDGen vg = new VTDGen();
            if (!vg.parseFile("input.xml", false));
            VTDNav vn = vg.getNav();
            AutoPilot ap = new AutoPilot(vn);
            XMLModifier xm = new XMLModifier(vn);
            ap.selectXPath("/xml/A/B[C/E='13']");
            while (ap.evalXPath()!=-1){
                xm.remove();
            }
            xm.output("output.xml");
        }
    
    }
    

提交回复
热议问题