This two functions permit to create and destroy WBXML Encoder instances.
#include <wbxml.h>WBXMLEncoder *wbxml_encoder_createvoid
Create a WBXML Encoder instance. Returns NULL if not enough memory.
#include <wbxml.h>void wbxml_encoder_destroyWBXMLEncoder *encoder
Destroy a WBXML Encoder instance. This function doesn't destroy the WBXMLTree associated with that WBXML Encoder.
#include <wbxml.h>
...
WBXMLEncoder *wbxml_encoder = NULL;
/* Create Encoder */
if ((wbxml_encoder = wbxml_encoder_create()) == NULL)
return 0;
...
<< Your Code Here >>
...
/* Destroy Encoder */
wbxml_encoder_destroy(wbxml_encoder);
This functions that initialize the WBXML Encoder are usefull when generating WBXML and XML.
#include <wbxml.h>void wbxml_encoder_set_ignore_empty_textWBXMLEncoder *encoderWB_BOOL set_ignore
Set the WBXML Encoder to ignore empty texts (ie: ignorable Whitespaces) when generating output [Default: TRUE].
#include <wbxml.h>void wbxml_encoder_set_remove_text_blanksWBXMLEncoder *encoderWB_BOOL set_remove
Set the WBXML Encoder to remove leading and trailing blanks in texts (ie: ignorable Whitespaces) when generating output [Default: TRUE].
This functions that initialize the WBXML Encoder are only usefull when generating WBXML.
#include <wbxml.h>void wbxml_encoder_set_use_strtblWBXMLEncoder *encoderWB_BOOL use_strtbl
Set if we use String Table when Encoding into WBXML [Default: TRUE].
This functions that initialize the WBXML Encoder are only usefull when generating XML.
#include <wbxml.h>void wbxml_encoder_set_xml_gen_typeWBXMLEncoder *encoderWBXMLEncoderXMLGenType gen_type
Set the WBXML Encoder XML Generation Type, when generating XML [Default: WBXML_ENCODER_XML_GEN_COMPACT].
Generation Mode:
WBXML_ENCODER_XML_GEN_COMPACT - Compact XML generation
WBXML_ENCODER_XML_GEN_INDENT - Indented XML generation
WBXML_ENCODER_XML_GEN_CANONICAL - Canonical XML generation
#include <wbxml.h>void wbxml_encoder_set_indentWBXMLEncoder *encoderWB_UTINY indent
Set the WBXML Encoder indent, when generating XML in WBXML_ENCODER_XML_GEN_INDENT mode [Default: 0].
This are the main functions to encode a WBXMLTree.
#include <wbxml.h>void wbxml_encoder_set_treeWBXMLEncoder *encoderWBXMLTree *tree
Set the WBXML Tree to encode. You MUST call this function before calling following wbxml_encoder_encode_to_wbxml() or wbxml_encoder_encode_to_xml() function.
#include <wbxml.h>WBXMLError wbxml_encoder_encode_to_wbxmlWBXMLEncoder *encoderWB_UTINY **wbxmlWB_ULONG *wbxml_len
Encode a WBXML Tree to WBXML. wbxml_encoder_set_tree() MUST be used before calling this function.
#include <wbxml.h>WBXMLError wbxml_encoder_encode_to_xmlWBXMLEncoder *encoderWB_UTINY **xml
Encode a WBXML Tree to XML. wbxml_encoder_set_tree() MUST be used before calling this function.
#include <wbxml.h>
...
WBXMLEncoder *wbxml_encoder = NULL;
WBXMLTree *tree = NULL;
WB_UTINY *wbxml = NULL;
WB_ULONG wbxml_len = 0;
...
<< tree created and filled >>
...
/* Create WBXML Encoder */
if ((wbxml_encoder = wbxml_encoder_create()) == NULL) {
wbxml_tree_destroy(tree);
return WBXML_ERROR_NOT_ENOUGH_MEMORY;
}
/* Set the WBXML Tree to encode */
wbxml_encoder_set_tree(wbxml_encoder, tree);
/* Ignores "Empty Text" Nodes */
wbxml_encoder_set_ignore_empty_text(wbxml_encoder, TRUE);
/* Remove leading and trailing whitespaces in "Text Nodes" */
wbxml_encoder_set_remove_text_blanks(wbxml_encoder, TRUE);
/* Use String Table */
wbxml_encoder_set_use_strtbl(wbxml_encoder, TRUE);
/* Encode to WBXML */
ret = wbxml_encoder_encode_to_wbxml(wbxml_encoder, &wbxml, &wbxml_len);
/* Clean-up */
wbxml_tree_destroy(tree);
wbxml_encoder_destroy(wbxml_encoder);
if (ret != WBXML_OK) {
printf("Encoding failed: %s\n", wbxml_errors_string(ret));
}
else {
printf("Encoding succeded\n");
...
<< Do what you have to do with this WBXML Buffer >>
...
wbxml_free(wbxml);
}
There are two ways of using the WBXML Parser
Parse a WBXML Document into a WBXML Tree : wbxml_parser_parse_to_tree()
Parse a WBXML Document into your own internal structures, by implementing your own Parser Callbacks : wbxml_parser_parse()
This is the easier method. You just have one function to call, and the WBXML Document is converted
to an in-memory representation: the WBXML Tree.
#include <wbxml.h>WBXMLError wbxml_parser_parse_to_treeWB_UTINY *wbxmlWB_ULONG wbxml_lenWBXMLTree  **tree
Parse a WBXML Document and creates the associated WBXML Tree. Returns WBXML_OK if no error, an error code otherwise.
This two functions permit to create and destroy WBXML Parser instances.
#include <wbxml.h>WBXMLParser *wbxml_parser_createvoid
Create a WBXML Parser instance. Returns NULL if not enough memory.
#include <wbxml.h>void wbxml_parser_destroyWBXMLParser *parser
Destroy a WBXML Parser instance. The User Data is not destroyed by this function.
#include <wbxml.h>
...
WBXMLParser *wbxml_parser = NULL;
/* Create Parser */
if ((wbxml_parser = wbxml_parser_create()) == NULL)
return 0;
...
<< Your Code Here >>
...
/* Destroy Parser */
wbxml_parser_destroy(wbxml_parser);
This functions are used to initialise the Parser.
#include <wbxml.h>void wbxml_parser_set_user_dataWBXMLParser *parservoid *user_data
Set a pointer to User Data. This User Data is passed back to the user in User Defined Callbacks
while parsing the WBXML document.
#include <wbxml.h>void wbxml_parser_set_content_handlerWBXMLParser *parserWBXMLContentHandler *content_handler
Set the User Defined Callbacks to use while parsing the WBXML document.
A WBXMLContentHandler is a structure that defines all the Callback Functions.
typedef struct WBXMLContentHandler_s {
WBXMLStartDocumentHandler start_document_clb;       /* Start Document Handler */
WBXMLEndDocumentHandler end_document_clb;           /* End Document handler */
WBXMLStartElementHandler start_element_clb;         /* Start Element handler */
WBXMLEndElementHandler end_element_clb;             /* End Element handler */
WBXMLCharactersHandler characters_clb;              /* Characters handler */
WBXMLProcessingInstructionHandler pi_clb;           /* Processing Instruction Handler */
} WBXMLContentHandler;
void WBXMLStartDocumentHandlervoid *ctxWB_LONGcharsetconst WBXMLLangEntry *lang
This Callback function is called when starting to parse the WBXML document.
ctx: The User Data
charset: IANA Charset MIBenum of the WBXML document
lang: WBXML Language Table (defined in wbxml_tables.[h|c])
void WBXMLEndDocumentHandlervoid *ctx
This Callback function is called when finished to parse the WBXML document. The
parameter is the User Data.
void WBXMLStartElementHandlervoid *ctxWBXMLTag *localNameWBXMLAttribute **attsWB_BOOLempty
This Callback function is called when parsing a WBXML start Element.
ctx: The User Data
localName: The WBXML Tag
atts: The WBXML Attribute list
empty: TRUE if this is an empty Element, FALSE otherwise
void WBXMLEndElementHandlervoid *ctxWBXMLTag *localNameWB_BOOLempty
This Callback function is called when parsing a WBXML end Element.
ctx: The User Data
localName: The WBXML Tag
empty: TRUE if this is an empty Element, FALSE otherwise
void WBXMLCharactersHandlervoid *ctxWB_UTINY *chWB_ULONGstartWB_ULONGlength
This Callback function is called when parsing a Content Data.
ctx: The User Data
ch: Buffer of character data
start: Starting index in Buffer
length: Length of data
void WBXMLProcessingInstructionHandlervoid *ctxconst WB_UTINY *targetWB_UTINY *data
This Callback function is called when parsing a Processing Instruction.
ctx: The User Data
target: The processing instruction target
data: The processing instruction data
#include <wbxml.h>void wbxml_parser_set_main_tableWBXMLParser *parserconst WBXMLLangEntry *main_table
Set the main WBXML Languages Table. You should not call this function unless you
know what you are doing (the main table of wbxml_tables.c is already set when
creating the WBXML Parser).
#include <wbxml.h>WB_BOOL wbxml_parser_set_wbxml_public_idWBXMLParser *parserWB_LONG public_id
Force to parse the Document with a given WBXML Public ID. Must be called BEFORE parsing of document.
This functions are used to retrieve informations while or after Parsing.
#include <wbxml.h>WB_ULONG wbxml_parser_get_wbxml_public_idWBXMLParser *parser
Get the WBXML Public ID of current parsing WBXML document.
#include <wbxml.h>const WB_UTINY *wbxml_parser_get_xml_public_idWBXMLParser *parser
Get the XML Public ID of current parsing WBXML document.
#include <wbxml.h>WB_UTINY wbxml_parser_get_wbxml_versionWBXMLParser *parser
Get the WBXML Version of current parsing WBXML document.
#include <wbxml.h>WB_LONG wbxml_parser_get_current_byte_indexWBXMLParser *parser
Get current parsing position in WBXML document.
#include <wbxml.h>WBXMLError wbxml_parser_parseWBXMLParser *parserWB_UTINY *wbxmlWB_ULONG wbxml_len
This function actually launch the Parsing of the WBXML Document.
parser: The Parser to use
wbxml: The WBXML Document to parser
wbxml_len: The WBXML Document length
#include <string.h>
#include <wbxml.h>
#define INPUT_BUFFER_SIZE 1000
/** Start Document Callback */
void parse_clb_start_document(void *ctx, WB_LONG charset, const WBXMLLangEntry *lang)
{
printf("Parsing Document:\n"
"\tRoot Element: %s\n"
"\tPublic ID: %s\n"
"\tDTD: %s\n",
lang->publicID->xmlRootElt,
lang->publicID->xmlPublicID,
lang->publicID->xmlDTD);
}
/** End Document Callback */
void parse_clb_end_document(void *ctx)
{
printf("End of Document\n");
}
/** Start Element Callback */
void parse_clb_start_element(void *ctx, WBXMLTag *element, WBXMLAttribute **atts, WB_BOOL empty)
{
WB_ULONG *indent = (WB_ULONG *) ctx;
WB_ULONG i = 0, j = 0;
/* Indent start Element */
for (i=0; i<*indent; i++)
printf(" ");
/* Write start Element */
printf("<%s", wbxml_tag_get_xml_name(element));
/* Write Attributes */
if (atts != NULL) {
while (atts[j] != NULL)
{
/* Write Attribute Name */
printf(" %s=\"%s\"", wbxml_attribute_get_xml_name(atts[j]), wbxml_attribute_get_xml_value(atts[j]));
j++;
}
}
/* End of start Element */
if (empty) {
printf("/>\n");
}
else {
printf(">\n");
(*indent)++;
}
}
/** End Element Callback */
void parse_clb_end_element(void *ctx, WBXMLTag *element, WB_BOOL empty)
{
WB_ULONG *indent = (WB_ULONG *) ctx;
WB_ULONG i = 0;
if (!empty) {
(*indent)--;
/* Indent End Element */
for (i=0; i<*indent; i++)
printf(" ");
/* Write end tag */
printf("</%s>\n", wbxml_tag_get_xml_name(element));
}
}
/** Characters Callback */
void parse_clb_characters(void *ctx, WB_UTINY *ch, WB_ULONG start, WB_ULONG length)
{
WB_ULONG *indent = (WB_ULONG *) ctx;
WB_ULONG i = 0;
/* Indent Characters */
for (i=0; i<*indent; i++)
printf(" ");
/* Write Content */
for(i=start; i<length; i++)
printf("%c", ch[i]);
printf("\n");
}
/** Main Function */
WB_LONG main(WB_LONG argc, WB_TINY **argv)
{
FILE *input_file = NULL;
WB_ULONG count = 0, total = 0, wbxml_len = 0;
WB_UTINY input_buffer[INPUT_BUFFER_SIZE + 1];
WBXMLParser *wbxml_parser = NULL;
WB_UTINY *wbxml = NULL;
WB_ULONG indent = 0, error_index = 0;
WBXMLError ret = WBXML_OK;
WBXMLContentHandler parse_handler =
{
parse_clb_start_document,
parse_clb_end_document,
parse_clb_start_element,
parse_clb_end_element,
parse_clb_characters,
NULL
};
if (argc != 2) {
printf("Missing argument: WBXML Filename");
return 0;
}
/**********************************
*  Read the WBXML Document
*/
/* Open WBXML document */
if ((input_file = fopen(argv[1], "rb")) == NULL) {
printf("Failed to open %s\n", argv[1]);
return 0;
}
/* Read WBXML document */
while(!feof(input_file))    {
count = fread(input_buffer, sizeof(WB_UTINY), INPUT_BUFFER_SIZE, input_file);
if (ferror(input_file))      {
printf("Error while reading from file %s\n", argv[1]);
fclose(input_file);
if (wbxml != NULL)
wbxml_free(wbxml);
return 0;
}
total += count;
if ((wbxml = wbxml_realloc(wbxml, total)) == NULL) {
printf("Not enought memory\n");
fclose(input_file);
if (wbxml != NULL)
wbxml_free(wbxml);
return 0;
}
memcpy(wbxml + wbxml_len, input_buffer, count);
wbxml_len += count;
}
fclose(input_file);
/* Create WBXML Parser */
if ((wbxml_parser = wbxml_parser_create()) == NULL) {
wbxml_free(wbxml);
return 0;
}
/* Initialize WBXML Parser */
wbxml_parser_set_user_data(wbxml_parser, &indent);
wbxml_parser_set_content_handler(wbxml_parser, &parse_handler);
/* Parse WBXML document */
if ((ret = wbxml_parser_parse(wbxml_parser, wbxml, wbxml_len)) != WBXML_OK)
{
error_index = wbxml_parser_get_current_byte_index(wbxml_parser);
printf("Parsing failed at %u - Token %x - %s", error_index, wbxml[error_index], wbxml_errors_string(ret));
}
else {
printf("Parsing OK !");
}
/* Destroy WBXML Parser */
wbxml_parser_destroy(wbxml_parser);
/* Free wbxml buffer */
wbxml_free(wbxml);
return 0;
}
TODO !!!
This functions permit to convert directly XML to WBXML, and WBXML to XML.
#include <wbxml_conv.h>WBXMLError xml2wbxmlWB_UTINY *xmlWB_UTINY **wbxmlWB_ULONG *wbxml_lenXML2WBXMLParameters *params
XML2WBXMLParameters Structure:
typedef struct XML2WBXMLParameters_s {
WB_BOOL keep_ignorable_ws;  /* Keep Ignorable Whitespaces (Default: FALSE) */
WB_BOOL use_strtbl;         /* Generate String Table (Default: TRUE) */
} XML2WBXMLParameters;
#include <wbxml_conv.h>
...
WB_UTINY *wbxml = NULL, *xml = NULL;
WB_LONG wbxml_len = 0;
WBXMLError ret = WBXML_OK;
XML2WBXMLParameters params;
...
<< Fill the 'xml' buffer with the XML Document to convert >>
...
/* Converter parameters */
params.use_strtbl = TRUE;
params.keep_ignorable_ws = FALSE;
/* Convert XML document */
ret = xml2wbxml(xml, &wbxml, &wbxml_len, &params);
if (ret != WBXML_OK) {
printf("xml2wbxml failed: %s\n", wbxml_errors_string(ret));
}
else {
printf("xml2wbxml succeded\n");
...
<< Do what you have to do with this WBXML Buffer >>
...
/* Clean-up */
wbxml_free(wbxml);
}
...
#include <wbxml_conv.h>WBXMLError wbxml2xmlWB_UTINY *wbxmlWB_ULONG wbxml_lenWB_UTINY **xmlWBXML2XMLParameters *params
typedef struct WBXML2XMLParameters_s {
WBXMLEncoderXMLGenType gen_type;    /* WBXML_ENCODER_XML_GEN_COMPACT | WBXML_ENCODER_XML_GEN_INDENT | WBXML_ENCODER_XML_GEN_CANONICAL (Default: WBXML_ENCODER_XML_GEN_INDENT) */
WB_UTINY indent;                    /* Indentation Delta, when using WBXML_ENCODER_XML_GEN_INDENT Generation Type (Default: 0) */
WB_BOOL keep_ignorable_ws;          /* Keep Ignorable Whitespaces (Default: FALSE) */
} WBXML2XMLParameters;
#include <wbxml_conv.h>
...
WB_UTINY *wbxml = NULL, *xml = NULL;
WB_LONG wbxml_size = 0;
WBXMLError ret = WBXML_OK;
WBXML2XMLParameters params;
...
<< Fill the 'wbxml' buffer with the WBXML Document to convert, and the wbxml_size with its size >>
...
/* Converter parameters */
params.gen_type = WBXML_ENCODER_XML_GEN_INDENT;
params.indent = 1;
params.keep_ignorable_ws = FALSE;
/* Convert WBXML document */
ret = wbxml2xml(wbxml, wbxml_size, &xml, &params);
if (ret != WBXML_OK) {
printf("wbxml2xml failed: %s\n", wbxml_errors_string(ret));
}
else {
printf("wbxml2xml succeded: \n%s\n", xml);
...
<< Do what you have to do with this XML Buffer >>
...
/* Clean-up */
wbxml_free(xml);
}
...
This tools are used to encode an XML file to a WBXML file, and a WBXML file to an XML file.
Convert an XML file to WBXML.
xml2wbxml-o output.wbxml-k-ninput.xml
-k : keep ignorable whitespaces (Default: ignore)
-n : do NOT generate String Table (Default: generate)
Convert a WBXML file to XML.
wbxml2xml-o output.xml-m mode-i indent-kinput.wbxml
-m mode (Generation mode - Default: 1) with:
0: Compact Generation
1: Indent Generation
2: Canonical Generation
-i indent (Indent delta when using mode '1' - Default: 1)
-k : keep ignorable whitespaces (Default: ignore)
