I'm wondering if it's possible to annotate my classes so that the first time the marshaller encounters an object, it generates an XML element of the appropriate type, but any subsequent reference to this object by anything else will have an XML IDREF entry created?
相关问题
- Illegal to have multiple roots (start tag in epilo
- Newtonsoft DeserializeXNode expands internal array
- how to use special characters like '<'
- XML - XSLT - document() function inside count() fu
- convert logback.xml to log4j.properties
相关文章
- Creating XML Elements without namespace declaratio
- Get Attribute Value From Simple XML Using JQuery /
- Directly signing an Office Word document using XML
- When sending XML to JMS should I use TextMessage o
- Fragment Content Overlaps Toolbar and Bottom Navig
- Getting “Error: Missing Constraints in ConstraintL
- xslt localization
- Upgrading transaction.commit_manually() to Django
You can leverage the concept of JAXB's
XmlAdapter
to do something like the following:input.xml
The following is the XML document I will use for this example. The 3rd
phone-number
entry is a reference to the 1stphone-number
entry, and the 5thphone-number
entry is a reference to the 4th.:Customer
The customer class maintains a collection of
PhoneNumber
objects. The same instance of PhoneNumber may appear multiple times in the collection.PhoneNumber
This is a class that can either appear in the document itself or as a reference. This will be handled using an
XmlAdapter
. An XmlAdapter is configured using the@XmlJavaTypeAdapter
annotation. Since we have specified this adapter at the type/class level it will apply to all properties referencing thePhoneNumber
class:WorkPhoneNumber
Based on your comment I have added a subclass of
PhoneNumber
.PhoneNumberAdapter
Below is the implementation of the
XmlAdapter
. Note that we must maintain if the PhoneNumber object has been seen before. If it has we only populate theid
portion of theAdaptedPhoneNumber
object.Demo
To ensure the same instance of the
XmlAdapter
is used for the entiremarshal
andunmarshal
operations we must specifically set an instance of the XmlAdapter on both theMarshaller
andUnmarshaller
:Output
For More Information