2
votes

I was able to use the XML as external meta-data by following the article here. However, Moxy is marshalling the properties that are neither annotated nor specified in the external XML meta-data. Below is the e.g. How to avoid this behavior? I tried using xml-mapping-metadata-complete="true" but it didn't help.

Class with new prefix property added (removed other properties for brevity)

public class Customer
{
    private String prefix;

    public void setPrefix(String prefix)
    {
        this.prefix = prefix;
    }

    public String getPrefix()
    {
        return prefix;
    }
}

meta-data xml

<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm" package-name="blog.bindingfile">
   <xml-schema namespace="http://www.example.com/customer" element-form-default="QUALIFIED" />
   <java-types>
      <java-type name="Customer">
         <xml-root-element />
         <xml-type prop-order="firstName lastName address phoneNumbers" />
         <java-attributes>
            <xml-element java-attribute="firstName" name="first-name" />
            <xml-element java-attribute="lastName" name="last-name" />
            <xml-element java-attribute="phoneNumbers" name="phone-number" />
         </java-attributes>
      </java-type>
      <java-type name="PhoneNumber">
         <java-attributes>
            <xml-attribute java-attribute="type" />
            <xml-value java-attribute="number" />
         </java-attributes>
      </java-type>
   </java-types>
</xml-bindings>

output

<customer xmlns="http://www.example.com/customer">
   <first-name>Jane</first-name>
   <last-name>Doe</last-name>
   <address>
      <street>123 A Street</street>
   </address>
   <phone-number type="work">555-1111</phone-number>
   <phone-number type="cell">555-2222</phone-number>
   <prefix>pre</prefix>
</customer>
1

1 Answers

2
votes

To omit the prefix property from your marshalled XML, you should declare it as transient in your bindings file:

  ...
  <java-type name="Customer">
     <xml-root-element />
     <xml-type prop-order="firstName lastName address phoneNumbers" />
     <java-attributes>
        <xml-element java-attribute="firstName" name="first-name" />
        <xml-element java-attribute="lastName" name="last-name" />
        <xml-element java-attribute="phoneNumbers" name="phone-number" />
        <xml-transient java-attribute="prefix" />
     </java-attributes>
  </java-type>
  ...

By default, JAXB will map any public fields, so since there was no explicit "annotation" on the prefix field, it is mapped in the default way.

xml-mapping-metadata-complete="true" means "Ignore any annotations found in the Java class and use this bindings file as the sole source of mapping information -- do not augment any existing annotations."

Hope this helps, Rick