Chapter 2. Usage

Table of Contents

Parsing XML
Creating elements tree
Modifying elements
Serializing to XML

Parsing XML

import tigase.xml.*;

DomBuilderHandler domHandler = new DomBuilderHandler();
SimpleParser parser = SingletonFactory.getParserInstance();

// array of chars to parse
char[] data = "<test/>".toCharArray();

// parsing data using parser and handler
parser.parse(handler, data, 0, data.length);

// check if there was no pasing errors
if (domHandler.parseError()) {
    // do something if XML parsing fails, ie. due to invalid characters in the input array..

// retrieve queue of parsed elements (root elements)
Queue<Element> elems = domHandler.getParsedElements();

// for each parsed element print it
Element elem = null;
while ((elem = elems.poll()) != null) {
    System.out.println("parsed element = " + elem);