Java To XML
The Java To XML activity allows you to convert a Java object’s data members into an XML document. For more information about the conversion rules, see Java Schema .
For example, the Java object has a method declared as
public int setID()
, but there is no method for getting the ID, and the data member ID
is not public. In this case, there is an element named ID
in this activity’s output schema, but that element has no value because there is no public mechanism for getting the data. Enumeration
Java enum type elements can be mapped to a simple type schema with enumeration facets. You can use the following annotations in your Java classes to use Java enums :
-
javax.xml.bind.annotation.XmlEnum
-
javax.xml.bind.annotation.XmlEnumValue
-
XmlEnum
andXmlEnumValue
together provide a mapping of type enum to the XML representation.
For more information and for the usage of these annotations, see https://jaxb.java.net/nonav/2.2.4/docs/api/javax/xml/bind/annotation/XmlEnum.html
and https://jaxb.java.net/nonav/2.2.4/docs/api/javax/xml/bind/annotation/XmlEnumValue.html
Field |
Description |
xsd:string |
java.lang.String |
xsd:boolean |
java.lang.Boolean |
xsd.byte |
java.lang.Byte |
xsd:short |
java.lang.Boolean |
xsd:int |
java.lang.Integer |
xsd:long |
java.lang.Long |
Example 1
@XmlEnum(String.class)
public enum Card { CLUBS, DIAMONDS, HEARTS, SPADES }
<xs:simpleType name="Card">
<xs:restriction base="xs:string"/>
<xs:enumeration value="CLUBS"/>
<xs:enumeration value="DIAMONDS"/>
<xs:enumeration value="HEARTS"/>
<xs:enumeration value="SPADES"/>
</xs:simpleType>
Example 2
@XmlEnum(Integer.class)
public enum Coin {
@XmlEnumValue("1") PENNY(1),
@XmlEnumValue("5") NICKEL(5),
@XmlEnumValue("10") DIME(10),
@XmlEnumValue("25") QUARTER(25) }
<!-- Example: XML Schema fragment -->
<xs:simpleType name="Coin">
<xs:restriction base="xs:int">
<xs:enumeration value="1"/>
<xs:enumeration value="5"/>
<xs:enumeration value="10"/>
<xs:enumeration value="25"/>
</xs:restriction>
</xs:simpleType>
Sequenced Elements
For a Java schema to generate the schema of complex type element having a sequence order indicator in the right sequence, use XMLType annotations in the Java classes.
This change in behavior is enabled by a system property java.property.com.tibco.xml.conversion.SequencingViaAnnotation=true
.
ActiveMatrix BusinessWorks tries to get the order of the element through annotations when the property is set to true
. The default value is false
.
Use the following annotation:
javax.xml.bind.annotation.XmlType
Properties and fields mapped to elements are mapped to a content model within a complex type.
The annotation element propOrder()
can be used to customize content model to xs:sequence
. This is used to specify the order of the XML elements in xs:sequence
.
For example, map a class to a complex type element with xs:sequence with a customized ordering of JavaBean properties.
@XmlType(propOrder={"street", "city" , "state", "zip", "name" })
public class USAddress {
private String name;
private String street;
private String city;
private String state;
private long zip;
public String getName() {..};
public void setName(String) {..};
public String getStreet() {..};
public void setStreet(String) {..};
public String getCity() {..};
public void setCity(String) {..};
public String getState() {..};
public void setState(String) {..};
public long getZip() {..};
public void setZip(long zip) {..};
}
<!-- XML Schema mapping for USAddress -->
<xs:complexType name="USAddress">
<xs:sequence>
<xs:element name="street" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="state" type="xs:string"/>
<xs:element name="zip" type="xs:decimal"/>
<xs:element name="name" type="xs:string"/>
</xs:sequence>
</xs:complexType>
An exception is thrown if any of the following conditions are true:
All the field members are not specified in the proporder element. Or, proporder contains the name of a field member that does not exist.