File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes Use a SAX Parser to split huge XML file into multiple parts, also need to keep the hierarchy intact Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Use a SAX Parser to split huge XML file into multiple parts, also need to keep the hierarchy intact" Watch "Use a SAX Parser to split huge XML file into multiple parts, also need to keep the hierarchy intact" New topic
Author

Use a SAX Parser to split huge XML file into multiple parts, also need to keep the hierarchy intact

amit bose
Greenhorn

Joined: Apr 01, 2005
Posts: 25
Hi,

How can I use a SAX Parser to split a huge XML file into multiple parts (I also will need to keep the hierarchy intact):

e.g.If the input xml is:
<institution>
<institution-name>Institute-123</institution-name>
<institution-address>Institute-address</institution-address>
<departments>
<department id="101">
<employee>
<name>Emp1-101</name>
</employee>
<employee>
<name>Emp2-101</name>
</employee>
</department>
<department id="102">
<employee>
<name>Emp1-102</name>
</employee>
<employee>
<name>Emp2-102</name>
</employee>
</department>
</departments>
</institution>

The Split XMLs must list the employee names with the department & institution details(with hierarchy intact).Thus, there will be 4 xmls in this case for employees

(Emp1-101.xml)

<data>
<institution-name>Institute-123</institution-name>
<institution-address>Institute-address</institution-address>
<department id="101">
<employee>
<name>Emp1-101</name>
</employee>
</department>
</data>

Similarly we need to have XMLs for Emp2-101, Emp1-102, Emp2-102

Please note that I cannot use DOM as the input XML is very huge.

Thanks,
Amit
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

using Code tags to write the xml would really help...

It looks like you want us to do your home work

ok, first of all do you know how sax parsing works ?
if not, take a look at this link: http://www.java-samples.com/showtutorial.php?tutorialid=152


My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

As salvin francis said, your XML is hard to read, but if my assumption about your requirements is correct, you could use a series of Transformers to produce the output documents. Make their input come from an XMLFilter and just pass your SAX events on to that filter.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Use a SAX Parser to split huge XML file into multiple parts, also need to keep the hierarchy intact