Runtime components : Core components : Formatters : Tasks : Handling binary message : Handling different kinds of Data Object : Handling UDTT DataElement
  
Handling UDTT DataElement
This section describes how to handle UDTT DataElement:
1 Defining data element:
Following is the sample definition of data element:
<data.xml>
  <kColl id="personData">
    <field id="name" />
    <field id="age" />
  </kColl>
</data.xml>
2 Defining format element:
Following is the sample definition of format element:
<format.xml>
  <format id="PersonFormat">
    <record>
      <fString dataName="name" encoding="cp937"/>
      <selfLength/>
      <fInteger dataName="age" byteOrdering="host"/>
      <selfLength/>
    </record>
  </format>
</format.xml>
3 Formatting the data element:
Following is the sample code to format the data element:
public static void main(String[] args) throws Exception {
KeyedCollection dataElement =
(KeyedCollection)DataElement.readObject("personData");
dataElement.setValueAt("name", "George. Wilhelm. T");
dataElement.setValueAt("age", 57);
FormatElement format = FormatFactory.getFormatElement("PersonFormat");
ReadAdapter read = new DataElementReadAdapter(dataElement);
Message msg = format.format(read);
System.out.println("====Format Result====");
System.out.println(msg);
}
If you run this main method, you can get the following result in the console:
Read BTT configuration from : "jar:///btt.xml"
Initialize BTT Component: traces
Initialize BTT Component: traces [Success]
Initialize BTT Component: dataElement
Initialize BTT Component: dataElement [Success]
Initialize BTT Component: format
Initialize BTT Component: format [Success]
3 BTT Components initialized.
====Format Result====
12C785969987854B40E68993888593944B40E30400000039
4 Unformatting the DataElement:
Following is the sample code to unformat the data element:
public static void main(String[] args) throws Exception {
KeyedCollection dataElement =
(KeyedCollection) DataElement.readObject("personData");

FormatElement format = FormatFactory.getFormatElement("PersonFormat");
WriteAdapter write = new DataElementWriteAdapter(dataElement);
byte[] bytes = HexCodecUtil.decodeHex("06C785969987850400000012".toCharArray());
Message message = new Message(bytes);
format.unformat(message, write);
System.out.println("====Unformat Result====");
System.out.println(dataElement);
}
If you run this main method, you can get the following result in the console:
Read BTT configuration from : "jar:///btt.xml"
Initialize BTT Component: traces
Initialize BTT Component: traces [Success]
Initialize BTT Component: dataElement
Initialize BTT Component: dataElement [Success]
Initialize BTT Component: format
Initialize BTT Component: format [Success]
3 BTT Components initialized.
====Unformat Result====
<kColl id="personData" dynamic="false" compress="false" >
<field id="name" value="George" description="" />
<field id="age" value="18" description="" />
</kColl >
5 Unformatting dynamic data element:
In the unformatting process, it is not required to define the data element. You can use a dynamic data element instead of predefined data element. Following is the sample code to unformat the dynamic data element:
public static void main(String[] args) throws Exception {
KeyedCollection dataElement = new KeyedCollection();
dataElement.setDynamic(true);

FormatElement format = FormatFactory.getFormatElement("PersonFormat");
WriteAdapter write = new DataElementWriteAdapter(dataElement);
byte[] bytes = HexCodecUtil.decodeHex("06C785969987850400000012".toCharArray());
Message message = new Message(bytes);
format.unformat(message, write);
System.out.println("====Unformat Result====");
System.out.println(dataElement);
}
If you run this main method, you can get the following result in the console:
Read BTT configuration from : "jar:///btt.xml"
Initialize BTT Component: traces
Initialize BTT Component: traces [Success]
Initialize BTT Component: dataElement
Initialize BTT Component: dataElement [Success]
Initialize BTT Component: format
Initialize BTT Component: format [Success]
3 BTT Components initialized.
====Unformat Result====
<kColl id="" dynamic="true" compress="false" >
<field id="name" value="George" description="" />
<field id="age" value="18" description="" />
</kColl>
Go up to
Handling different kinds of Data Object