How do I get Castor to ignore certain XML fields?

1.7k Views Asked by At

I am maintaining some complex Java code and the Castor (v1.2) unmarshaling is very slow due to quite a few "missing" Java objects. See, the XML contains more fields than I require but Castor repeatedly tries to instantiate the Java objects, causing lots of ClassNotFound errors.

Castor Mapping File:

<mapping>
  <class name="com.example.imaging.product.Product">
    <map-to xml="product"/>
    <field name="productId" type="long">
      <bind-xml name="id" node="attribute"/>
    </field>
  </class>

  <class name="com.example.imaging.product.RegionConfiguration">
    <map-to xml="mediaConfiguration"/>
    <field name="name" type="string">
      <bind-xml name="name" node="attribute"/>
    </field>
    <field name="design" type="int">
      <bind-xml name="designId" node="attribute"/>
    </field>
  </class>
</mapping>

XML Source:

<?xml version="1.0"?>
<product id="1234">
  <productImage colorId="1"/>
  <mediaConfiguration name="Front" designId="98765" />
  <color id="1" name="Red" default="true"/>
</product>

My problem is that the color field doesn't have a Java equivalent and I don't want it unmarshaled. I tried setting org.exolab.castor.xml.strictelements=false in the castor.properties file but that doesn't keep it from walking the classload path and throwing ClassNotFound errors.

How can I make Castor skip over non-needed XML elements?

1

There are 1 best solutions below

1
On

It sounds like you can't override the behavior of trying to unmarshal each element, see the Castor reference. Have you measured what the real performance impact is? It might be best just to ignore this until Castor develops better override behavior.

If the class is not described in the mapping file, Castor will instrospect the class using the Java Reflection API to determine if there is any function of the form getXxxYyy()/setXxxYyy( x). This accessor will be associated with XML element/attribute named 'xxx-yyy'. In the future, we will provide a way to override this default behavior.