ElOwner
ElOwner

Reputation: 361

Java - Split XML file that contains many loops

My question is simple for some and just impossible for others (like me)

I have an XML file and I want to split it into small XML files, of course I've to split it when I reach certain count on <DOCUMENT> Element, I've to put 60 <DOCUMENT> in each small XML file.

My document is formated like that :

<DOCS>
      <PIL>
          <ELEMENT1>
          <ELEMENT2>                   
          <ELEMENT1>
      <PIL>
      <DOCUMENT>
          <ELEMENT1>
          <ELEMENT2>
          <ELEMENT3>
          <ELEMENT4>
                <SUBELEMENT1>
                            <ELEMENT1>
                            <ELEMENT1>
                            <ELEMENT1>
                </SUBELEMENT1>
                <SUBELEMENT2>
                            <ELEMENT1>
                            <ELEMENT1>
                            <ELEMENT1>
                </SUBELEMENT2>
          </ELEMENT4>
          <ELEMENT5>
          <ELEMENT6>
      </DOCUMENT>
</DOCS>

So my question is how can I split it having 60 <DOCUMENT> in each file ? and do it very quick.

If someone have a solution using Talend that would be great. knowing that using tXMLOutput I can split it but it will only take one element from each loop and all the element of my "document" loop

Upvotes: 0

Views: 268

Answers (1)

Federico Paparoni
Federico Paparoni

Reputation: 722

You can you Stax API for reading the file and then put 60 of this element in a new file. For an example you can read this answer

In the example the file is created for each element. In your problem you can create a different file every 60 iterations like this

int counter=0;
int fileNumber=1;
File file = new File("out/split1.xml");
StreamResult sr=new StreamResult(file);
while(xsr.nextTag() == XMLStreamConstants.START_ELEMENT) {
    counter++;
    if (counter>60) {
       counter=0;
       fileNumber++;
       file = new File("out/split"+fileNumber+".xml");
       sr=new StreamResult(file);
    }
    t.transform(new StAXSource(xsr), sr);
}

Upvotes: 1

Related Questions