![]() | ![]() | ![]() | ![]() | ![]() | ![]() | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() | ![]() | ![]() | ![]() | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() | ![]()
|
Castor XML FAQGeneral How do I set the encoding? I'm getting an error about 'xml' prefix already declared? Why is my 'get' method called twice? How can I speed up marshalling/unmarshalling performance? How do I ignore elements during unmarshalling? Where does Castor search for the castor.properties file? Can I programmatically change the properties found in the castor.properties file? Introspection Can private methods be introspected? Mapping My mapping file seems to have no effect! Are there any tools to automatically create a mapping file? How do I specify a namespace in the mapping file? How do I prevent a field from being marshalled? Marshalling The XML is marshalled on one line, how do I force line-breaks? What is the order of the marshalled XML elements? Source Code Generator Can I use a DTD with the source generator? My XML output looks incorrect, what could be wrong? The generated source code has incorrect or missing imports for imported schema types How can I make the generated source code more JDO friendly? Miscellaneous Is there a way to automatically create an XML Schema from an XML instance? How to enable XML validation with Castor XML Why is mapping ignored when using a FieldHandlerFactory GeneralHow do I set the encoding?
Create a new instance of the Marshaller class and use the
setEncoding method. You'll also need to make sure the
encoding for the Writer is set properly as well:
I'm getting an error about 'xml' prefix already declared?For Castor 0.9.5.2 only
The issue occurs with newer versions of Xerces than the version 1.4 that ships
with Castor. The older version works ok. For some reason, when the newer
version of Xerces encounters an "xml" prefixed attribute, such as
"xml:lang" it tries to automatically start a prefix mapping for "xml".
Which, in my opinion, is technically incorrect. They shouldn't be doing
that. According to the w3c, the "xml" prefix should never be declared.
The reason it started appearing in the new Castor (0.9.5.2), was because
of a switch to SAX 2 by default during unmarshalling.
Solution:
Why is my 'get' method called twice?The get method will be called a second time during the validation process. To prevent this from happening simply disable validation on the Marshaller or Unmarshaller. How can I speed up marshalling/unmarshalling performance?
To cut down on object creation, you can reuse an existing object
model, but be careful this is an experimental feature.
Create an Unmarshaller with your existing root object and set object
reuse to true...
How do I ignore elements during unmarshalling?
Where does Castor search for the castor.properties file?
Castor loads the castor.properties in the following order:
Note: You can also use LocalConfiguration.getInstance().getProperties() to change the properties values programatically. Can I programmatically change the properties found in the castor.properties file?Yes, many of these properties can be set directly on the Marshaller or Unmarshaller, but you can also use LocalConfiguration.getInstance().getProperties() to change the properties values programatically. IntrospectionCan private methods be introspected?Castor does not currently support introspection of private methods. Please make sure proper public accesssor methods are available for all fields that you wish to be handled by the Marshalling Framework. MappingMy mapping file seems to have no effect!Make sure you are not using one of the static methods on the Marshaller/Unmarshaller. Any configuration changes that you make to the Marshaller or Unmarshaller are not available from the static methods. Are there any tools to automatically create a mapping file?Yes! We provide one such tool, see org.exolab.castor.tools.MappingTool. There are some 3rd party tools as well. How do I specify a namespace in the mapping file?
For a specific field you can use a QName for the value of the bind-xml name
attribute as such:
For a class mapping, use the <map-to> element. For more information see the XML Mapping documentation. How do I prevent a field from being marshalled?
Set the transient attribute on the <bind-xml> element to true:
MarshallingThe XML is marshalled on one line, how do I force line-breaks?For all versions of Castor:
To enable pretty-printing (indenting, line-breaks) just modify the
castor.properties file and uncomment the following:
Note: This will slow down the marshalling process What is the order of the marshalled XML elements?
If you are using Castor's default introspection to automatically
map the objects into XML, then there is no guarantee on the order.
It simply depends on the order in which the fields are returned to
Castor using the Java reflection API. Note: If you use a mapping file Castor will generate the XML in
the order in which the mapping file is specified. Source Code GeneratorCan I use a DTD with the source generator?Not directly, however you can convert your DTD to an XML Schema fairly easily. We provide a tool (org.exolab.castor.xml.dtd.Converter) to do this. You can also use any number of 3rd-party tools such as XML Spy or XML Authority. My XML output looks incorrect, what could be wrong?Also: I used the source code generator, but all my xml element names are getting marshalled as lowercase with hyphens, what's up with that?
Solution: The generated source code has incorrect or missing imports for imported schema types
Example:
For those using 0.9.5.1, you'll need to upgrade due to the following bug: http://bugzilla.exolab.org/show_bug.cgi?id=1407 How can I make the generated source code more JDO friendly?For Castor 0.9.4 and above:
Castor JDO requires a reference to the actual collection to be returned from
the get-method. By default the source generator does not provide such a
method. To enable such methods to be created, simple add the following
line to your castorbuilder.properties file:
Your mapping file will also need to be updated to include the proper set/get method names. MiscellaneousIs there a way to automatically create an XML Schema from an XML instance?Yes! We provide such a tool. Please see org.exolab.castor.xml.schema.util.XMLInstance2Schema. It's not 100% perfect, but it does a reasonable job. How to enable XML validation with Castor XMLTo enable XML validation at the parser level, please add properties to your castor.properties file as follows: org.exolab.castor.parser.namespaces=true org.exolab.castor.sax.features=http://xml.org/sax/features/validation,\ http://apache.org/xml/features/validation/schema,\ http://apache.org/xml/features/validation/schema-full-checking Please note that the example given relies on the use of Apache Xerces, hence the apache.org properties; similar options should exist for other parsers. Why is mapping ignored when using a FieldHandlerFactoryWhen using a custom FieldHandlerFactory as in the following example
please make sure that you set the mapping file after you set the
ClassDescriptorResolver. You will note the following in the Javadoc for
Note: This method will nullify any Mapping currently being used by this Marshaller | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() ![]() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() | ![]() | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() | ![]() | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |