This example shows how to use an XMLParser object to
parse an XML document from a String, how to remove undesired types of
tags from that document, and how to output the modified document as a
String.
// store an arbitrary xml document in a string
//
String xml_string = L"<xml_tag attrib1='val1'> cdata tokens </xml_tag>";
xml_string.concat(L" <tag> tag contents </tag>");
xml_string.concat(L" <metadata> metadata contents </metadata>");
xml_string.concat(L" <item> item contents </item>");
// declare a parser
//
XMLPaser parser;
// specify which tokens we want to allow
// we will allow all but the item tokens
//
Vector<String> handled_values;
handled_values.concat(L"xml_tag");
handled_values.concat(L"tag");
handled_values.concat(L"metadata");
// parse the document. when the valid values are specified
// before this point, invalid tokens would be filtered automatically
//
parser.parseXML(xml_string);
// will output:
// <xml_tag attrib1='val1'>
// cdata
// tokens
// </xml_tag>
// <tag>
// tag
// contents
// </tag>
// <metadata>
// metadata
// contents
// </metadata>
//
Console::put(parser.toXML());
// retrieve the document in a Vector<XMLToken> form for processing
// elsewhere
//
Vector<XMLToken> token_vector;
token_vector.assign(parser.getTokenVector());
// we may wish to modify the document outside of the parser
// lets add a few more xml tokens
//
XMLToken token;
token.init(START_TAG, L"new_token");
token_vector.concat(token);
token.init(CDATA, L"new_token_content");
token_vector.concat(token);
token.init(END_TAG, L"new_token");
// we didn't manually set the depths of the new tokens, so now
// the document is incorrect. The parser can take care of that for us.
//
XMLParser::setTokenDepths(token_vector);
// perhaps we wish to remove the metadata token type from the document
// to do so, put the token vector back in the parser
//
parser.setTokenVector(token_vector);
// define a set of valid values, which include the tokens we manually
// added, but not metadata tag
//
handled_values.clear(Integral::RESET);
handled_values.concat(L"xml_tag");
handled_values.concat(L"tag");
handled_values.concat(L"new_token");
// remove the invalid tokens
//
parser.setValidValues(handled_values);
parser.removeUnhandledTokens();
// see what the document looks like now.
// You've already seen how to do that with the parser,
// here's a way to do it without the parser object.
//
token_vector.assign(parser.getTokenVector());
// will output:
//
// <xml_tag attrib1='val1'>
// cdata
// tokens
// <xml_tag>
// <tag>
// tag
// contents
// </tag>
// <new_token>
// new_token_contents
// </new_token>
//
Console::put(XMLParser::toXML(token_vector));