Hi Castor users,
I am new to Castor. I searched the mail archive and Castor documentation,
but still cannot find how to let Castor marshalling framework to ignore some
null field of an object.
The following are the sample code of doing marshalling:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
try {
db = dbf.newDocumentBuilder();
} catch (ParserConfigurationException e1) {
e1.printStackTrace();
}
Document doc = db.newDocument();
Element root = null;
namespace = "http://cis.com";
if(namespace == null) {
* root = doc.createElement(xmlName);*
} else {
root = doc.createElementNS(namespace, xmlName);
}
if(typeString.trim().endsWith("[]")) {
// array type
String itemName = config.getProperty("cisPropertiesPath", typeString +
".itemName");
Object[] arrVal = (Object[])value;
String itemTypeString = typeString.substring(0, typeString.length()-2);
int size = arrVal.length;
for(int i = 0; i < size; i++) {
Object val = arrVal[i];
if(StringToClassTransformer.isPrimitive(itemTypeString)) {
// item is primitive
Element item = doc.createElement(itemName);
Text text = doc.createTextNode(val.toString());
item.appendChild(text);
root.appendChild(item);
} else {
* Mapping mapper = new Mapping();
InputSource mapping = getMapping(itemTypeString);
mapper.loadMapping(mapping);
*
//Element item = doc.createElement(itemName);
* Marshaller marshaller = new Marshaller(root);
* try {
* marshaller.setMapping(mapper);
marshaller.marshal(val);
* } catch (MarshalException e) {
e.printStackTrace();
} catch (ValidationException e) {
e.printStackTrace();
} catch (MappingException e) {
e.printStackTrace();
}
}
} // end of for
}
After marshalling, the root Node reads as follows:
<?xml version="1.0" encoding="UTF-8"?><getHearingDetailsByCaseNoWSReturn
xmlns="http://cis.com">
<hearingDetailsVO><applicationNo>sAppNo0</applicationNo><duration/><stEndTime/><judgeName/><caseNo/><outCome/><endTime/><remarks/><sessionNo/><typeHear/><docID/><natureOfClaim/><roomNo/><outcomeDate/><tohCode/><NOA/><hearDate/><designation/><outComeTime/></hearingDetailsVO></getHearingDetailsByCaseNoWSReturn
However, since the object to be marshalled only has one non-null field
"applicationNo", I expect the following XML:
<?xml version="1.0" encoding="UTF-8"?><getHearingDetailsByCaseNoWSReturn
xmlns="http://cis.com">
<hearingDetailsVO><applicationNo>sAppNo0</applicationNo></hearingDetailsVO></getHearingDetailsByCaseNoWSReturn
I have tried to modify the binding to include "required=false" in each
field, but it does not take effect.
<class
name="com.cis.dto.hearing.HearingDetailsVO">
<map-to xml="hearingDetailsVO" />
<field name="applicationNo" type="java.lang.String" *required="false"*>
<bind-xml name="applicationNo" node="element" />
</field>
<field name="duration" type="java.lang.String" *required="false"*>
<bind-xml name="duration" node="element" />
</field>
<field name="stEndTime" type="java.lang.String" *required="false"*>
<bind-xml name="stEndTime" node="element" />
</field>
...
</class>
Could anyone point me a way to solve the problem?
Regards,
Xinjun