| | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| How to Unmarshal raw XML segments into arbitrary typesIntended Audience Prerequisites Basic concept Mapping file References Intended AudienceAnyone who wants to map raw XML segments into data types. PrerequisitesNone. Basic conceptLets say you have an XML structure:
And you want to save off portions of this XML in your objects, instead of having Castor map them immediately.
There are two approaches that can be used for this. The first, approach would be to use the AnyNode type. The benefits of this approach is that you do not have to do anything special to get the data in the mapping file, and can both unmarshal and marshal the xml data. The drawback is that you have to call toString() on the object to get the XML. The second approach is to use a custom FieldHandler implementation to convert the values. The benefit of this approach is that you are able to convert into any type you want. The drawback is that there is currently no way to marshal the xml data back into XML. It ends up as XML encoded data inside the marshalled XML. Mapping fileHere is an example mapping file that includes both examples.
The anyType field is set to java.lang.Object. Behind the scenes it is actually an AnyType object. Also, notice that the stringXML field is of type java.lang.Object. This is because Castor looks at that first to determine what type of parsing needs to be done, then passes it on to the Field Handler, which converts it into whatever is desired. Here is an example Field Handler that converts the input into a string.
References
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||