• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Recursively parse XML and splitting into separate XMLs

 
Tariq Ahsan
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 20983
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic