Cutting a XML with JAXB

belai123

New Member
I have the following xml:\[code\] <Package> <PackageHeader> <name>External Vendor File</name> <description>External vendor file for some purpose</description> <version>3.141694baR3</version> </PackageHeader> <PackageBody> <Characteristic id="1"> <Size> <value>1.68</value> <scale>Meters</scale> <comment>Size can vary, depending on temperature</comment> </Size> <Weight> <value>9</value> <scale>M*Tons</scale> <comment>His mama is so fat, we had to use another scale</comment> </Weight> <rating> <ratingCompany>ISO</ratingCompany> <rating:details xmlns:rating="http://www.w3schools.com/ratingDetails"> <rating:value companyDepartment="Finance">A</rating:value> <rating:expirationDate update="1/12/2010">1/1/2014</rating:expirationDate> <rating:comment userID="z94234">You're not Silvia.</rating:comment> <rating:comment userID="r24942">You're one of the Kung-Fu Creatures On The Rampage</rating:comment> <rating:comment userID="i77880">TWO!</rating:comment> <rating:priority>3</rating:priority> </rating:details> </rating> </Characteristic> <Characteristic id="2"> <Size/> <Weight/> <rating/> </Characteristic> ... <Characteristic id="n"/> </PackageBody> </Package>\[/code\]And the following Java code:\[code\] public class XMLTest { public static void main(String[] args) throws Exception { Package currentPackage = new Package(); Package sourcePackage = new Package(); int totalCharacteristics; PackageBody currentPackageBody = new PackageBody(); Characteristic currentCharacteristic = new Characteristic(); rating currentRating = new rating(); FileInputStream fis = new FileInputStream("sourceFile.xml"); JAXBContext myCurrentContext = JAXBContext.newInstance(Package.class); Marshaller m = myCurrentContext.createMarshaller(); Unmarshaller um = myCurrentContext.createUnmarshaller(); sourcePackage = (Package)um.unmarshal(fis); currentPackage.setPackageHeader(sourcePackage.getPackageHeader()); totalCharacteristics = sourcePackage.getPackageBody().getCharacteristics().size(); for (int i = 0; i < totalCharacteristics; i++) { currentRating = sourcePackage.getPackageBody().getCharacteristics().get(i).getrating(); } currentCharacteristic.setrating(currentRating); currentPackageBody.getCharacteristics().add(currentCharacteristic); currentPackage.setPackageBody(currentPackageBody); m.marshal(currentPackage, new File("targetFile.xml")); fis.close(); } }\[/code\]Which gives me the next XML:\[code\] <Package> <PackageHeader> <name>External Vendor File</name> <description>External vendor file for some purpose</description> <version>3.141694baR3</version> </PackageHeader> <PackageBody> <Characteristic id="1"> <rating> <ratingCompany>ISO</ratingCompany> <rating:details xmlns:rating="http://www.w3schools.com/ratingDetails"> <rating:value companyDepartment="Finance">A</rating:value> <rating:expirationDate update="1/12/2010">1/1/2014</rating:expirationDate> <rating:comment userID="z94234">You're not Silvia.</rating:comment> <rating:comment userID="r24942">You're one of the Kung-Fu Creatures On The Rampage</rating:comment> <rating:comment userID="i77880">TWO!</rating:comment> <rating:priority>3</rating:priority> </rating:details> </rating> </Characteristic> <Characteristic id="2"> <rating/> </Characteristic> ... <Characteristic id="n"/> </PackageBody> </Package>\[/code\]And this is what I need:\[code\] <Package> <PackageHeader> <name>External Vendor File</name> <description>External vendor file for some purpose</description> <version>3.141694baR3</version> </PackageHeader> <PackageBody> <Characteristic> <rating id="1"> <ratingCompany>ISO</ratingCompany> <rating:details xmlns:rating="http://www.w3schools.com/ratingDetails"> <rating:comment userID="z94234">You're not Silvia.</rating:comment> <rating:comment userID="r24942">You're one of the Kung-Fu Creatures On The Rampage</rating:comment> <rating:comment userID="i77880">TWO!</rating:comment> <rating:priority>3</rating:priority> </rating:details> </rating> </Characteristic> <Characteristic> <rating id="2"/> </Characteristic> ... <Characteristic/> </PackageBody> </Package>\[/code\]But I have a few questions:
  • How could I implement a way to read a 4GBs file? (for example, reading it with StAX).
  • If I want to filter some tags from source to target(as in the last xml), would I have to assign them one by one to the targetFile? Is there any iterator that might allow me to go through all subnodes and assign them?
  • If the sourceFile changes, would I need to rerun the xjc and recompile the whole project?
Thanks.
 
Back
Top