nlib
nn::nlib::TaggedTextParser Class Referencefinal

A simple parser for parsing XML-like tagged text. More...

#include "nn/nlib/TaggedTextParser.h"

Public Member Functions

 TaggedTextParser (char *tagged_text)
 Instantiates the object. More...
 
const char * GetText () const
 Returns the pointer to the character data obtained by the GetNext function. More...
 
Event Next ()
 Gets the next element or text. More...
 

Detailed Description

A simple parser for parsing XML-like tagged text.

Description
Use this function for parsing tagged text. It is suitable for reading the information from files such as those for simple settings, which are guaranteed to be correct.
After instantiating the object, advance through the path using the Next function, and get the element names and the text using the GetText function.
The TaggedTextParser class has the following features.
  • The implementation is extremely compact and coded in just a header file of around 200 lines.
  • Rewrites the input text in memory as it runs. As a result, no additional memory is required.
  • Coding is simpler than a SAX format parser for a PULL-type parser.
  • Fast, because it does not construct DOM-like data.
Note the following.
  • The parts corresponding to XML declarations, PI, and comments are skipped, but if the text is complicated the parser may fail.
  • The parts corresponding to XML attributes are skipped.
  • No support for escapes and the like.
  • Almost no checking of syntax is performed.
For specification details, see the implementation of the header file.
// Strings are rewritten, so you cannot make them const char.
char text[] = "<doc > my text </toc >";
TaggedTextParser parser(text);
parser.Next(); // TaggedTextParser::kStartElement
parser.GetText(); // "doc"
parser.Next(); // TaggedTextParser::kCharacters
parser.GetText(); // "my text"
parser.Next(); // TaggedTextParser::kEndElement
parser.GetText(); // "toc"
parser.Next(); // TaggedTextParser::kEndDocument

Definition at line 23 of file TaggedTextParser.h.

Constructor & Destructor Documentation

◆ TaggedTextParser()

nn::nlib::TaggedTextParser::TaggedTextParser ( char *  tagged_text)
inlineexplicit

Instantiates the object.

Parameters
[in,out]tagged_textThe text string being parsed.
Description
Note that the input text can be rewritten during the parsing process.

Definition at line 39 of file TaggedTextParser.h.

Member Function Documentation

◆ GetText()

nn::nlib::TaggedTextParser::GetText ( ) const
inline

Returns the pointer to the character data obtained by the GetNext function.

Returns
The obtained text string (element name and character data).
Description
Note that the pointer points to inside the input text. Also note that if the data is character data, any leading and trailing white spaces are removed from the text that is returned.

Definition at line 47 of file TaggedTextParser.h.

◆ Next()

nn::nlib::TaggedTextParser::Next ( )
inline

Gets the next element or text.

Return values
TaggedTextParser::kStartElementIndicates a start tag.
TaggedTextParser::kEndElementIndicates an end tag.
TaggedTextParser::kCharactersIndicates character data.
TaggedTextParser::kEndDocumentIndicates that the document has ended.
TaggedTextParser::kSyntaxErrorAn error occurred.

Definition at line 52 of file TaggedTextParser.h.


The documentation for this class was generated from the following files: