Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 14th, 2014, 07:31 PM
Authorized User
 
Join Date: Feb 2013
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default [StaX Parser] Chunking a huge XML File

Hallo everyone,

I would like to split a XML File with multiple repeated Tag. I found the ideal example in internet and the solution in Java:

Input XML:
Code:
<statements>
   <statement account="123">
      ...stuff...
   </statement>
   <statement account="456">
      ...stuff...
   </statement>
</statements>
I would like to chunk this one XML File into a list of String for the tag statement:
Code:
<statement account="123">
		...stuff...
</statement>
...
...
...
Code:
<statement account="456">
		...stuff...
</statement>
The below solution does the main purpose, though each chunk is going to be an independent XML File:
Code:
import java.io.File;
import java.io.FileReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stax.StAXSource;
import javax.xml.transform.stream.StreamResult;

public class Demo {

    public static void main(String[] args) throws Exception  {
        XMLInputFactory xif = XMLInputFactory.newInstance();
        XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader("input.xml"));
        xsr.nextTag(); // Advance to statements element

        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer t = tf.newTransformer();
        while(xsr.nextTag() == XMLStreamConstants.START_ELEMENT) {
            File file = new File("out/" + xsr.getAttributeValue(null, "account") + ".xml");
            t.transform(new StAXSource(xsr), new StreamResult(file));
        }
    }


My question or my problem:
I would like to put the chuncks as a list of String. Is there a chance for me to implement this directly? Thank you for the comments.

Regards,

Ratna





Similar Threads
Thread Thread Starter Forum Replies Last Post
Parsing XML with XSLT or SAX/STAX sportinguista XSLT 2 November 27th, 2012 04:26 AM
XML File not found error from xerces parser P.J.Kim XML 0 October 19th, 2004 05:39 AM
problem openning huge xls file helmekki Excel VBA 3 October 5th, 2004 04:43 AM
Download huge file using ASP iniro Classic ASP Basics 4 September 27th, 2004 11:56 PM
Help with reading huge XML file hitesh_kapadia ASP.NET 1.x and 2.0 Application Design 0 August 6th, 2004 02:54 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.