Virtualzone Heiner Peuser's Blog: Tutorials, How-Tos, Software.

25Feb/110

Creating & Parsing WBXML with Java using kXML 2

WBXML (WAP Binary XML) is a binary representation of XML. It is used e.g. by the Microsoft Exchange ActiveSync protocol.

Only very few WBXML parsers for Java seem to be available. One of the libraries you will probably come across when searching for a WBXML Java Library is kXML. kXML is a small XML pull parser, specially designed for constrained environments such as Applets, Personal Java or MIDP devices (see kXML's about page).

Getting started with kXML is a bit tricky, since you find only a handful of tutorials that are suitable for kXML 2. These snippets should make it easier for you. Read more...

Converting WBXML to XML:

InputStream in = ...... // TODO
WbxmlParser parser = new WbxmlParser();
// You may want to set some code pages here using parser.setTagTable()
parser.setInput(in, "UTF-8");
Document dom = new Document();
dom.parse(parser);
 
ByteArrayOutputStream out = new ByteArrayOutputStream();
KXmlSerializer ser = new KXmlSerializer();
ser.setOutput(out, null);
dom.write(ser);
ser.flush();
 
byte[] b = out.toByteArray();
System.out.println(new String(b));

Converting XML to WBXML:

InputStream in = ...... // TODO
KXmlParser parser = new KXmlParser();
parser.setInput(in, "UTF-8");
Document dom = new Document();
dom.parse(parser);
 
ByteArrayOutputStream out = new ByteArrayOutputStream();
WbxmlSerializer ser = new WbxmlSerializer();
// You may want to set some code pages here using ser.setTagTable()
ser.setOutput(out, null);
dom.write(ser);
ser.flush();
 
byte[] b = out.toByteArray();
System.out.println(new String(b));

Working with Code Pages / Tag Tables

Code pages/tag tables specify which tag string is encoded by which integer. The protocol you're about to implement defines its own tag table - so you will have to tage a look at its specifications. Please refer to the the W3C WBXML documentation for further information.

Here is a small example:

public static final String [] TAG_TABLE_0 = {
	"Tag1", // 0x05
	"Tag2", // 0x06
	"Tag3", // 0x07
};
 
public static final String [] TAG_TABLE_1 = {
	// ...
};
 
parser.setTagTable(0, TAG_TABLE_0);
parser.setTagTable(1, TAG_TABLE_1);
parser.setTagTable(2, ...);
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

(required)

Turn on pictures to see the captcha *

No trackbacks yet.