aspose file tools*
The moose likes XML and Related Technologies and the fly likes Recursively parse XML and splitting into separate XMLs 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 "Recursively parse XML and splitting into separate XMLs" Watch "Recursively parse XML and splitting into separate XMLs" New topic
Author

Recursively parse XML and splitting into separate XMLs

Tariq Ahsan
Ranch Hand

Joined: Nov 03, 2003
Posts: 116
Hello,

I am trying to find out using the DOM API if I could recursively parse an XML, based on certain tag name and split it into multiple XMLs. These XMLs (String datatype) will be fed into methods for further processing. I am using JDK 1.4.2.
Here's a sample XML -

<?xml version="1.0" encoding="iso-8859-2" ?>
<People>
<Person>
<Father>
<Name>John Doe</Name>
<Age>35</Age>
</Father>
<Child>
<Name>John Doe Jr.</Name>
<Age>1</Age>
</Child>
</Person>
<Person>
<Father>
<Name>Bill Moe</Name>
<Age>40</Age>
</Father>
<Child>
<Name>Bill Moe Jr.</Name>
<Age>2</Age>
</Child>
</Person>
</People>

The output XMLs -

<Person>
<Father>
<Name>John Doe</Name>
<Age>35</Age>
</Father>
<Child>
<Name>John Doe Jr.</Name>
<Age>1</Age>
</Child>
</Person>

and

<Person>
<Father>
<Name>Bill Moe</Name>
<Age>40</Age>
</Father>
<Child>
<Name>Bill Moe Jr.</Name>
<Age>2</Age>
</Child>
</Person>

What would be the best way to do this?

Thanks!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Once you have a DOM, you have already parsed the XML document. But that's just terminology (although it does help if we all use the words in the same way).

You can use normal DOM programming to get each of the Person elements, which is what your example shows. If you want to serialize each of those elements back to an XML document, then here's what you do:
Tariq Ahsan
Ranch Hand

Joined: Nov 03, 2003
Posts: 116
Paul,

Much appreciated for your code snippet. Tried it and think that's what I exactly needed to get what I wanted.
Again thanks a lot!

- Tariq
 
wood burning stoves
 
subject: Recursively parse XML and splitting into separate XMLs