• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Recursively parse XML and splitting into separate XMLs

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic