How to reparse an xml document.

asfak
2011-05-23
2013-05-15
  • asfak
    asfak
    2011-05-23

    Hi

    I had been using dom4j VisitorSupport for a problem.

    But since the file is very big I thought I should give a try to vtd-xml.

    I need to know how to reparse a document with all its modifications intact and without saving it to file system.

    That is I need to modify some xml content then I need to reparse the document to find if any other changes are required or not.

    For this reason I parsed the document again with the method 'parseFile'. But the problem is with XMLModifier. It does not keep the previous changes.

    Can anybody help me out to find what should be the flow of the code? To help you understand my problem I have shown a demo xml and a program.

    <?xml version="1.0"?>
    <purchaseOrder orderDate="1999-10-20">
        <billTo country="US">
            <name>Robert Smith</name>
            <street>8 Oak Avenue</street>
            <city>Old Town</city>
            <state>PA</state>
            <zip>95819</zip>
        </billTo>
    </purchaseOrder>
    
    File fo = new File("junk/test/newpo.xml");
                FileOutputStream fos = new FileOutputStream(fo);
                VTDGen vg = new VTDGen();
    
                XMLModifier xm = new XMLModifier();
                AutoPilot ap = new AutoPilot();
                int count = 0;
                while (vg.parseFile("junk/oldpo.xml", true)) {
                    VTDNav vn = vg.getNav();
                    ap.bind(vn);
                    xm.bind(vn);
                    if (count == 0) {
                        ap.selectXPath("//billTo/name");
                    }else {
                        ap.selectXPath("//billTo/city");
                    }
                    count++;
                    int i = -1;
                    REPROCESS: {
                        while ((i = ap.evalXPath()) != -1) {
                            xm.remove();
                            xm.insertBeforeElement("<name1/>\n");
                            break REPROCESS;
                        }
                    }
                    if (count > 1) {
                        break;
                    }
                }
                xm.output(fos);
                fos.close();
    

    Best regards.

     
  • jimmy zhang
    jimmy zhang
    2011-06-29

    It does keep all hte changes. XMLMOdifier has a method called output and reparse(). You should look into it.