DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Jimmy has posted 14 posts at DZone. View Full User Profile

Extract And Insert A Namespace Compensated Element Fragment With VTD-XML

05.21.2010
| 3967 views |
  • submit to reddit
        // description of your code here
/* Insert a ns-compensated fragment into an XML doc
*/

public class FragmentTest {    
    public static void main(String[] s) throws Exception{
        // instantiate VTDGen and XMLModifier
        VTDGen vg = new VTDGen();
        XMLModifier xm = new XMLModifier();
        AutoPilot ap = new AutoPilot();
        AutoPilot ap2 = new AutoPilot();
        ap.selectXPath("(/*/*/*)[position()>1 and position()<4]");
        ap2.selectXPath("/*/*/*");
        if (vg.parseFile("soap2.xml",true)){
            VTDNav vn = vg.getNav();
            xm.bind(vn);
            ap2.bind(vn);
            ap.bind(vn);
            ap2.evalXPath();
            ElementFragmentNs ef = vn.getElementFragmentNs();
            int i = -1;
            while((i=ap.evalXPath())!=-1){
                xm.insertAfterElement(ef);
            }           
            xm.output(new FileOutputStream("new_soap.xml"));
        }        
    }
}