Greezer
Greezer

Reputation: 231

How to loop on multiple xml files with jaxb

I have an application who create some temporary XML that I have to parse and catch some information in. One of theses XML contains multiples rows with path to each related XML. It's something like :

<z:row TimeStamp="2012-09-25T09:58:49" FileName="\\test\app\myfile_AB.xml"/>
<z:row TimeStamp="2012-09-25T09:58:49" FileName="\\test\app\myfile_CD.xml"/>
...

I would like to know if it's possible to parse this "master" XML with JAXB, get all the path and then do a loop and treat each of the related XMLs in one single operation.

All the XML have approximately the same structure.

Thank you in advance for any help

Upvotes: 2

Views: 1313

Answers (2)

bdoughan
bdoughan

Reputation: 149017

I would recommend parsing the outer document using a StAX (JSR-173) XMLStreamReader. The as you advance to each row element I would have the JAXB (JSR-222) implementation unmarshal the content. This eliminates the need to holding the entire outer document in memory.

Example Demonstrating the Concept

Upvotes: 1

Ravi K
Ravi K

Reputation: 1016

That is possible. You have to define 2 separate XSDs first. one is master XSD for master file & then second XSD for other all files. Below is one suggested XML structure for master XML

  <files>
      <z:row>
      <z:row>
      <z:row>
      .
      .
  <files>

Auto Generate java pojo classes for both XSDs. Then whenever you receive master XML, first unmarshall it using auto-generated class for master XSD. Here you will get object corresponding to root elemente e.g Files.java object. Now in this files object iterate over row objects & get FileName. Now for each FileName, again create separate unmarshaller & unmarshall that file with its auto-generated class.

Upvotes: 3

Related Questions