title: Splitting Large XML FIles Java (StAX)
body: <p>I am using this code <a href="https://github.com/koen-serneels/blog/tree/master/BigXmlTest/src/main/java/be/error/bigxmltest" rel="nofollow noreferrer">https://github.com/koen-serneels/blog/tree/master/BigXmlTest/src/main/java/be/error/bigxmltest</a> from GitHub to split large XML files(6GB).I am starting with the basic XML File provided to understand the program. The program outputs the header for each file that is split. I am Trying to add a Footer Element now also for each file and having some difficulties, any suggestions or advice on how to go about this would be great.</p>
  
  <p>I have added The footer to the schema and written the code to add a footer in the "openOutputFileAndWriteHeader()" method. However This adds the footer within the Content Element. Shown Below: </p>
  
  <pre><code>private XMLEventWriter openOutputFileAndWriteHeader(int fileNumber) throws Exception
  {
  
      XMLEventFactory xmlEventFactory = XMLEventFactory.newInstance();
      XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
      xmlOutputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
      XMLEventWriter writer = xmlOutputFactory.createXMLEventWriter(new FileOutputStream(new File("C:\\Users\\thamm\\Desktop\\SplitFiles\\File_" + fileNumber + ".xml")));
  
      writer.setDefaultNamespace(DOCUMENT_NS);
      writer.add(xmlEventFactory.createStartDocument());
  
      writer.add(xmlEventFactory.createStartElement(new QName(DOCUMENT_NS, ROOT_ELEMENT), null, null));
  
      HeaderType header = objectFactory.createHeaderType();
      header.setSomeHeaderElement("List of Employees");
      marshaller.marshal(new JAXBElement&lt;HeaderType&gt;(new QName(DOCUMENT_NS, HEADER_ELEMENT, ""), HeaderType.class, 
                      HeaderType.class, header), writer);
  
      writer.add(new StartElementEvent(new QName(DOCUMENT_NS, CONTENT_ELEMENT)));
  
      FooterType footer = objectFactory.createFooterType();
      footer.setSomeFooterElement("Footer");
      marshaller.marshal(new JAXBElement&lt;FooterType&gt;(new QName(DOCUMENT_NS, FOOTER_ELEMENT,""), FooterType.class, 
                        FooterType.class, footer), writer);
  
      return writer;
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>&lt;?xml version='1.0' encoding='UTF-8'?&gt;
  &lt;BigXmlTest xmlns="http://www.sidero.ie/bigxmltest"&gt;
      &lt;Header&gt;
          &lt;SomeHeaderElement&gt;List of Employees&lt;/SomeHeaderElement&gt;
      &lt;/Header&gt;
      &lt;Content&gt;
          &lt;Footer&gt;
              &lt;SomeFooterElement&gt;Footer&lt;/SomeFooterElement&gt;
          &lt;/Footer&gt;
          &lt;Data&gt;Data4&lt;/Data&gt;
          &lt;Data&gt;Data5&lt;/Data&gt;
          &lt;Data&gt;Data6&lt;/Data&gt;
      &lt;/Content&gt;
  &lt;/BigXmlTest&gt;
  </code></pre>