Castor Collection Field to empty - java

My castor masrshaller have an XML output as below
root>
field1 /field1>
field2 /field2>
..........
fieldn>
collection>
field>
field>
..........
field>
/collection>
/root>
my mapping for the collection part is
field name="collectionObj" type="string" collection="arraylist">
bind-xml name="field" location="collection" node="element" />
/field>
The issue is when the I will always have the collectiObj is empty
whole collection>tag disappears from the XMl output. Instead I would like to display as collection/>. Is there a way other than writing some fieldhandler, say setting some property to handle this issue.

Couldn't find any alternative other than a custom field handler.. It is working fine...

Related

How to skip field when empty in beanio?

The requirement is to skip field when its empty.
eg -
<segment name="seg1" class="com.company.bean.segmentBean" xmlType="none">
<field name="field1" xmlName= "fieldXml1" xmlType="attribute" maxLength="7" />
<field name="field2" xmlName= "fieldX2l1" xmlType="attribute" maxLength="1" typeHandler="Handler" />
</segment>
Lets assume that field2="". As the value of field2 is "". I would like to have the field skipped in segment. Basically the end result XML shouldnt display field2 as its empty("").
You need the lazy attribute on field2. As stated in the Reference Guide
lazy - Set to true to convert empty field text to null before type conversion. For repeating fields bound to a collection, the collection will not be created if all field values are null or the empty String. Defaults to false.
This will make field2 = null and by default, most XML libraries will not output any null elements, BeanIO included.
Try this for field2:
<field name="field2" lazy="true" xmlName= "fieldX2l1" xmlType="attribute" maxLength="1" typeHandler="Handler" />
Most of the time, I also combine lazy with trim. From the docs:
trim - Set to true to trim the field text before validation and type
conversion. Defaults to false.
<field name="field2" lazy="true" trim="true" xmlName= "fieldX2l1" xmlType="attribute" maxLength="1" typeHandler="Handler" />

Jaxb customization of nillable attribute value for a specific field

I'm working with an XSD schema file (that I cannot change) that defines element XXX as following:
<xsd:element name="XXX" type="Date" minOccurs="0"/>
This produces a java.util.Date in the generated class.
I would need to change how the field is marshalled to XML: if the attribute is null, I would like to produce an empty tag, like if the xsd was:
<xsd:element name="XXX" type="Date" minOccurs="0" nillable=true/>
This produces a JaxbElement in the generated class.
Is it possible to do something like this via Jaxb bindings?
Regards
Giulio
Suggestions:
Pre-process your schema with an XSLT to add nillable where you need it.
Use the jaxb2-simplify-plugin and customize your element with simplify:as-reference-property. I have actually never tried that but maybe it'll work.
Write an XJC plugin.
ps. I'm the author of the mentioned jaxb2-simplify-plugin.
Yes you can.. but isn't a good practice.
<bindings node="//xs:element[#name='XXX']">
<property name="xxx">
<baseType>
<javaType name= "javax.xml.bind.JAXBElement<java.util.Date>"/>
</baseType>
</property>
</bindings>
you should add also below attributes within <javaType> See here Documentation
parseMethod is the name of the parse method to be called during
unmarshalling.
printMethod is the name of the print method to be
called during marshalling.

How to solve IllegalArgumentException: Property is not indexed

I have a property in jsp like the following
<html:text property="sequenceNumbersMap[0]" styleId="sequenceNumbersMap[0]" value="0"/>
<html:text property="sequenceNumbersMap[1]" styleId="sequenceNumbersMap[1]" value="1"/>
<html:text property="sequenceNumbersMap[2]" styleId="sequenceNumbersMap[2]" value="2"/>
<html:text property="sequenceNumbersMap[3]" styleId="sequenceNumbersMap[3]" value="3"/>
and ActionForm has the property like
Map sequenceNumbersMap;
and getter/setter
public Map<Integer, Integer> getSequenceNumbersMap() {
return sequenceNumbersMap;
}
public void setSequenceNumbersMap(Map<Integer, Integer> sequenceNumbersMap) {
this.sequenceNumbersMap = sequenceNumbersMap;
}
but when i try to submit the jsp i get the following exception :
java.lang.IllegalArgumentException: Property 'sequenceNumbersMap' is not indexed
would someone help me to fix this issue?
Thanks
A map is not ordered so sequenceNumbersMap[i] does not mean anything. If you mean to get the value mapped to i rather than getting the i-th item in the map (which has no sense again), you can do it with sequenceNumbersMap.get(i).
You must either iterate through a map differently: How to loop through a HashMap in JSP?
Or use a different kind of data structure for sequence numbers (indexed, as it was pointed in the exception, e.g. java.util.List).

Mapping an Object[] with dozer

With the help of Dozer I want to map an Object[] to a DTO class
<mapping>
<class-a>com.example.myDtoClass</class-a>
<class-b>java.lang.Object[]</class-b>
<field>
<a>prop</a>
<!-- <b key="1">this</b> -->
<b>this[1]</b>
<b-hint>java.lang.String</b-hint>
</field>
</mapping>
but both settings <b>this[1]</b> and <b key="1">this</b> do not work and the prop field is set with the entire Object[] field so the result in returned JSON looks like
"prop": "[Ljava.lang.Object;#40147864"
what am I missing? I am expecting to have the 1 element of the Object[] in the prop variable
I made a bad mistake. Yes I get a Object[] to map, but the 1 element of that Object[] again is of type Object[]. I got confused and thought it was the main element. I did though expect a String, but got a array of binary code that has to be joined back to a string.

Map from XML using rest-assured

I'm trying to set up an integration test using rest-assured. In one of my test cases I have to validate some properties of an XML file with rest-assured's XmlPath which seems to use Groovy's GPath.
I have an XML document with the following structure (the ids are unique):
<rootelement>
<someelement id="1234" type="a">
<property key="hello" value="world" />
<property key="name" value="a name" />
<property key="status" value="new" />
<child target="645823" type="a" />
<child target="7482" type="b" />
<child target="8942" type="c">
<property key="pro" value="yes" />
</child>
</someelement>
<someelement>
...
</someelement>
<rootelement>
Ideally, given a someelement id, I want to get a map of it's properties, i.e. assuming the given someelement id is 1234 I'd like to get a map that looks like the following:
{"hello": "world", "name": "a name", "status": "new"}. How would I do this? I know that there's a getMap method in XmlPath, but I couldn't figure out which expression I'd have to use.
If it's not possible to get the properties as a map, I would be content with getting a list of the keys and a list for the values. Again, I don't know which expression I have to use. I tried something like that:
xmlPath.getList("**.find {it.#id = '1234'}.property.#key", String.class)
However, it doesn't find anything.
You can do this with Groovy (assuming xml is a String containing your xml)
def map = new XmlParser().parseText( xml )
.someelement
.find { it.#id == '1234' }
.property
.collectEntries { [ it.#key, it.#value ] }
assert map == [ hello:'world', name:'a name', status:'new' ]
Never used rest-assured, so I can't be much help in that direction though :-(

Categories

Resources