Reputation: 3166
I have the following xml to be consumed by my application:
<PackageFiles ApplicationGUID="839E9EFD-69C2-430D-B591-B9C8E9812377">
<Files>
<File Name="test.jpg" />
</Files>
</PackageFiles>
To unmarshall with JAXB I've defined the following two classes:
@XmlRootElement(name="PackageFiles")
public class PackageFiles {
@XmlAttribute(name="ApplicationGUID")
private String applicationGUID;
@XmlElementWrapper(name="Files")
@XmlElement(name="File")
private List<File> files;
}
@XmlRootElement(name="File")
public class File {
@XmlAttribute(name="Name")
private String name;
}
This works great but I was wondering is there a way to annotate the PackageFiles
class so that I can eliminate the File
class and instead have the Name
attribute in the <File ...>
element be populated into a List<String>
in the PackageFiles class?
Upvotes: 0
Views: 426
Reputation: 607
If this sort of complication comes up often, you may consider using a Transformer that uses xslt to perform the unmarshalling in a way that you can easily manipulate. Either that or you can make file a public static class inside PackageFiles like so:
public static class File{
@XmlElement
private String name;
}
Personally I prefer the xslt method because it's a bit more intuitive and a lot easier to maintain than .java files that are in a constant state of flux.
Upvotes: 1