COXParser and COXHTMLParser Overview
Copyright ©The Code Project 1996-2005, All Rights Reserved
COXParser is a simple class that allow the parsing of XML-style formatted files that contain nested tags, comments, markup and text. Tags are delimited using the usual <...>, </...>format, or <.../> for an empty tag, and each tag can contains one or more attributes (eg <TAG number="1" value="a">. Comments delimited by <!-- and -->delimiters are suppored, as is literal text. An example would be: 
This would be parsed into the following structure: 
Each element can contain a collection of further Elements, and a collection of attribute. Once a file or text buffer has been parsed, the Root Element ("[Root]") of the COXParser object will contain the collection of all elements (in COXParserElement objects) each with their set of attributes (in COXAttribute objects). Each element in the tree will contain further subElements (COXParserElement objects), Comments (COXParserObject) and text (COXParserObject). All objects contained within the tree are derived from COXParserObject. To determine the type of object, use COXParserObject::GetType(). This will return UNKNOWN, ELEMENT, PLAINTEXT, COMMENT, MARKUP or ATTRIBUTE. Using COXParser: To use this class simply declare a variable of type COXParser, then call COXParser::Parse() or COXParser::ParseFile(). The parser object will then contain the tree structure of the text buffer or file. The easiest way to traverse the tree is to use a recursive function:
void DisplayElement(COXParserElement* pElement) {
 if (pElement->GetType() != CParseObject::ELEMENT) 
  return; 
printf("%s\n", pElement->GetName()); 
for (int i = 0; i < pElement->NumAttributes(); i++) { 
printf("Name = %s ", pElement->Attribute(i)->GetName());
 if (pElement->Attribute(i)->GetAttributeType() == COXAttribute::ATTR_STRING) 
  printf("Value = %s\n", pElement->Attribute(i)->GetStringValue()); 
else if (pElement->Attribute(i)->GetAttributeType() == COXAttribute::ATTR_INTEGER) 
sprintf("Value = %d\n", pElement->Attribute(i)->GetIntValue()); 
} 
for (i = 0; i < pElement->NumObjects(); i++){
 CParseObject* pObject = pElement->Object(i); 
switch (pObject->GetType()) {
 case COXParserObject::CDATA: 
case COXParserObject::PLAINTEXT: 
  printf("Text = %s\n", pObject->GetText()); 
  break; 
case COXParserObject::COMMENT: 
  printf("Comment = %s\n", pObject->GetText()); 
  break; 
case COXParserObject::MARKUP: 
  printf("Markup = %s\n", pObject->GetText()); 
 break; 
case COXParserObject::PROCINSTR: 
  printf("Processing Instruction = %s\n", pObject->GetText()); 
  break; 
case COXParserObject::ELEMENT: 
  DisplayElement((COXParserElement*)pObject); break; 
  }
 } 
} 
Then: 
COXParser parser;
 parser.ParseFile("file.txt"); 
DisplayElement(parser.Root()); 
COXParser also allows you to hook in your own error reporting callback. Simply call COXParser::SetErrorRptFunction(ReportErrorFn, dwData); where ReportErrorFn is a function of type BOOL ReportErrorFn(UINT nErrorCode, LPCTSTR szError, int nLine, int nColumn, DWORD dwData), nErrorCode contains an error code. szError contains an error message for line nLine, col nColumn, and dwData is specifed in SetErrorRptFunction and passed to the callback. The return value for this function is not used as yet (In future it will be used to provide control over whether or not processing should be halted). You can use LPCTSTR COXParser::TranslateErrorCode() to get a literal representation of the errorcode. TODO: (These are supported as Markup elements - but further processing could be added) Parsing of Element Type Declarations ie Parsing of Attribute List Declarations ie Entity Declarations ie , or Parameter Entities ie ( then is the same as Notation Declaration ie (This is already supported as a PI - but further processing could be added) Add in support for the prolog ie ' Name validation (eg only certain chars allowed in names and tokens) 
COXHTMLParser
COXHTMLParser class allows us to parse HTML formatted files, that contain nested tags, comments, markup and text. It can be useful in HTML editors, redactors, viewers and so on. This class is derived from COXParser class. All useful functions are declared as virtual so it will be easy to derive your own class. To parse HTML file simple call method ParseFile() of COXParser, if you want to parse HTML text already allocated in the memory call COXParser::Parse() To use this class refer to COXParser overview.