I’m just starting to learn to use JAXB to marshal my java objects into XML. I’ve got no problem serializing objects, but now what I want to do is insert an element into the xml for which there is no corresponding POJO.
For example, I have a list of Folders, and I want to put each Folder into an xml element called Folders. Does this make sense? I could just write <Folders> and </Folders> to the output stream where appropriate, but I’d like to do this in a more formal way.
You could use JAXB with StAX to do this. Using a
XMLStreamWriterwill help formalize the interaction with the XML and give you better control over such this as encodings:The above code assumes that your
Folderclass is annotated with@XmlRootElement: