Parsing XML
by David Cox
Example 1:
(a)
David1 Main St
AnywhereNY90210
(b)
David
Main Street
Anywhere
NY
90210
Listing One
void Parser::StartTag(Tag * t)
{
std::string buffer;
Match(m_leftStartAngle); // match "<"
TagName(buffer); // match tag name
if (t) t->Name(buffer); // save tag name
if (m_lookahead != m_rightStartAngle)
{
AttributeList(t); // match attributes
}
Match(m_rightStartAngle); // match ">"
}
EndTag( ) is even simpler:
void Parser::EndTag()
{
std::string tagName;
Match(m_leftEndAngle); // match ""
TagName(tagName); // match tag name
Match(m_rightEndAngle); // match ">"
}
Listing Two
#include
#include
#include
void main(int argc, char *argv[]) {
Parser p;
ifstream xmlFile(argv[1]);
p.Translate(xmlFile);
Tag * root = p.GetRoot();
TagIterator ti(root);
Tag * next = ti.Begin(); // get the root of the tree
while (next)
{
next->Visit(ti.Level()); // Visit prints the tag
next = ti.Next(); // get the next tag in the tree
}
exit(0);
}
2