I am currently using a Spring Batch application to call an API and parse out the entire JSON response into an XML to be consumed by another application. Currently we are using a rather large expand query with the API which sometimes times out on elements. The required output format for the results is
<?xml version...?>
<RECORDS>
<RECORD>
<PROP NAME="json.path.to.element.name">
<PVAL>The value for the element</PVAL>
</PROP>
<PROP NAME="json.path.to.element2.name">
<PVAL>The value for the element2</PVAL>
</PROP>
...
</RECORD>
<RECORD>
...
</RECORD>
...
</RECORDS>
This was being created using a DOMSource and a Transformer. This unfortunately does not take advantage of the Spring Batch chunk processing. I am updating this to extend StaxEventItemWriter and passing a list of mapped objects. My mapped entities are
@XmlElement
public class Record implements Serializable {
private List<Prop> prop;
}
@XmlElement
public class Prop implements Serializable {
@XmlAttribute
private String name;
@XmlValue
private String pVal;
}
My Spring Batch XML config for my marshaller is
<bean id="xmlMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller">
<property name="aliases">
<map>
<entry key="RECORD" value="com...Record"/>
<entry key="PROP" value="com...Prop" />
</map>
</property>
</bean>
The problem I am getting is my output format is not working. I am getting
<?xml version...?>
<RECORDS>
<RECORD>
<prop>
<PROP>
<name>json.path.to.element1.name"</name>
<pVal>The value for the element1</pVal>
</PROP>
<name>json.path.to.element2.name"</name>
<pVal>The value for the element2</pVal>
</PROP>
...
</prop>
</RECORD>
<RECORD>
<prop>
...
</prop>
</RECORD>
...
</RECORDS>
The two things that need to happen is 1) the <prop> entity needs to be removed as directly under the Record are all the <PROP> entities. and 2) the <PROP> needs to be <PROP NAME...>
I have tried messing with the annotations but can’t seem to change the output. I am not even sure what the XSD would be for the desired structure.
For the second part of your question, the correct annotation for the name field should be as below if you want it to be an attribute.
Following the XStream annotation documents , the first part should be: