Splitting of a large XML file into small Chunks based on repeated elements

Viewed 612

Consider the following XML with 500 MB data

<?xml version="1.0" encoding="UTF-8"?>
<Parents>
  <process  Child ="A">...</process>
  <process  Child="B">...</process>
  <process  Child="A">...</process>
  <process  Child="C">..</process>
  <process Child=...
  </process>
 <\Parents>

This xml has multiple child attribute with tag "A" or "B" or other I want to create a separate XML for "A", "B", "C" or others like expamle_A.xml, example_B.xml etc. Below Code is creating separate xml foe each child attribute, means if we have 500 child attribute its creating 500 xml's.

public static void main(String args[]) {
        try {
            VTDGen v = new VTDGen();
            if (v.parseFile("C:\\..\\example.xml", true)) {
                VTDNav vn = vg.getNav();
                AutoPilot ap = new AutoPilot(vn);
                ap.selectXPath("/Parents/child");
                int  chunk = 0;
                while (( ap.evalXPath()) != -1) {
                    long frag = vn.getElementFragment();
                    (new FileOutputStream("C:\\....\\result" + chunk + ".xml")).write(vn.getXML().getBytes(), (int) frag,
                            (int) (frag >> 32));
                    chunk++;
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
}

Now the thing is i want to split the file on the basis of child attribute of same group for an instance all the child of "A" should be in example_A.xml file same way for B,C and others.

1 Answers
Related