class XmlParser
This class is a 'lexical' parser of XML. It is intended for implementation of descent parsers of XML.
void SkipWhites()
Skips any whitespaces in the input XML.
void RegisterEntity(const String& id, const String& text)
Registers a new XML entity id with value text.
bool IsEof()
Returns true if parser reached the end of text.
const char *GetPtr() const
Returns a pointer to the position in the text the parser reached.
bool IsTag()
Returns true if the parser is at XML start-tag.
String PeekTag()
Returns the next tag id, but does not advance. If the parser is not at start-tag, XmlError is thrown.
String ReadTag()
Returns XML start-tag id and advances. If the parser is not at start-tag, XmlError is thrown.
bool Tag(const String& tag)
bool Tag(const char *tag)
If parser is at XML start-tag, advances and returns true, otherwise returns false.
void PassTag(const String& tag)
void PassTag(const char *tag)
If parser is at XML start-tag, advances, otherwise throws XmlError exception.
bool IsEnd()
Returns true if the parser is at matching end-tag. Note that encountering non-matching tags invokes XmlError, unless the parser is in relaxed mode.
bool End()
Returns true if the parser is at matching end-tag and advances. Note that encountering non-matching tags throws XmlError, unless the parser is in relaxed mode.
void PassEnd()
If parser is at matching end-tag, advances, otherwise XmlError is thrown.
bool TagE(const char *tag)
Calls Tag(tag) - if it returns true, immediately calls PassTag. In other words, matches element with empty content.
void PassTagE(const char *tag)
Calls PassTag(tag) and then PassEnd(). In other words, requires to advance over element with empty content.
bool TagElseSkip(const char *tag)
If call Tag(tag).returns true. Otherwise calls Skip and returns else. This is a shortcut to relatively common construct if(Tag(tag)) { ... } else Skip();
bool LoopTag(const char *tag)
If End call returns true, returns false. If call to Tag(tag) returns true, returns true. Otherwise it calls Skip and repeats. This is useful if we are only interested in just one type of subtag of current level, e.g.: while(LoopTag("foo")) { ... } is an equivalent of common construct while(!End()) if(Tag(tag)) { ... } else Skip();
int GetAttrCount() const
Returns the number of attributes of the last start-tag.
String GetAttr(int i) const
Returns the name of attribute i of the last start-tag.
bool IsAttr(const char *id) const
Returns true if id an attribute of the last start-tag.
String operator[](int i) const
Returns the value of attribute i of the last start-tag.
String operator[](const char *id) const
Returns the value of attribute with name id of the last start-tag. If no such attribute exists, return empty String (Null).
int Int(const char *id, int def = Null) const
Returns the value of attribute, converted to integer number, with the name id of the last start-tag. If no such attribute exists or if attribute text cannot be converted to integer, returns def.
double Double(const char *id, double def = Null) const
Returns the value of attribute, converted to floating point number, with the name id of the last start-tag. If no such attribute exists or if attribute text cannot be converted to integer, returns def.
bool IsText()
Returns true if parser is at text content of element.
String ReadText()
Reads a single text content. If there are contained elements in the content, parser stops at them. E.g. if parser is at "just a <b>test</b> foo", this method returns "just a ". If there is no text, returns empty String.
String ReadTextE()
Reads all text content until end-tag. If there are contained elements in the content, parser skips them. E.g. if parser is at "just a <b>test</b> foo", this method returns "just a foo". If there is no text, returns empty String.
bool IsDecl()
Returns true if parser is at XML declaration.
String ReadDecl()
Reads XML declaration - throws XmlError if parser is not at declaration.
bool IsPI()
Returns true if parser is at XML processing info.
String ReadPI()
Reads XML processing - throws XmlError if parser is not at any.
Returns true if parser is at XML comment.
Reads XML comment - throws XmlError if parser is not at any.
void Skip()
Skips current symbol. If the symbol is start-tag, skips everything until matching end-tag is passed.
void SkipEnd()
Skips everything past the end-tag of last parser start-tag.
VectorMap<String, String> PickAttrs() pick_
Picks all attributes of last passed start-tag. No attribute-related methods (including this one) can be called after this call until the next start-tag is processed.
int GetLine() const
Returns the current line of input text.
int GetColumn() const
Returns the current column of input text.
void Relaxed(bool b)
Activates the relaxed mode. In relaxed mode, XmlParser ignores mismatches of start-tag and end-tags. Unknown entities are replace with character '&'. This mode was introduced to deal with broken XML files.
void Raw(bool b)
Activtes the raw mode. I raw mode, all logic about start-tags and end-tags is completely supressed. This mode was introduced to deal with HTML.
XmlParser(const char *s)
Creates the parser for xml input text s. The pointed text must be valid through the whole parsing process (XmlParser does not make copy of the text).
XmlParser(Stream& in)
Creates the parser for the input stream in.
|