MicroXml provides stand-alone support for the basic, most-used features of XML -- tags, attributes, and element values. It produces a DOM tree of XML nodes. MicroXml does not support DTDs, CDATAs and other advanced XML features.
MicroXml is easy to use and provides easy access to view/navigate its nodes in a debugger.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 | /*
MicroXml provides stand-alone support for the basic, most-used features
of XML -- tags, attributes, and element values. That's all. It produces
a DOM tree of XML nodes.
MicroXml does not support DTDs, CDATAs and other advanced XML features.
It stores the XML declaration but doesn't use it.
However, within these limitations, MicroXml is easy to use and allows
far simpler debugging of XML results than when using a full-featured
XML library. With XmlDoc::to_string() one can round-trip the XML for
fast eyeball checking.
This module contains main() which will parse a sample XML string into
a DOM tree, then write the DOM tree out as XML text.
Jack Trainor 2015
*/
#include <iostream>
#include <string>
#include <cstdio>
#include <deque>
#include <vector>
#include <map>
typedef unsigned int uint;
static const int NO_TAG = 0;
static const int START_TAG = 1;
static const int END_TAG = 2;
static const int EMPTY_TAG = 3;
static const int COMMENT_TAG = 4;
static const int DECL_TAG = 5;
static const int DOCTYPE_TAG = 6;
bool strEq(const std::string& s1, const std::string& s2);
int getTagType(const std::string& xml);
bool charInString(const char c, const std::string& s);
void compressReturns(std::string& s);
////////////////////////////////////////////////////////////////////////////////
class XBuffer
{
public:
const std::string& text;
int index;
int len;
XBuffer(const std::string& text, int index=0);
virtual ~XBuffer(void);
virtual bool atEnd();
virtual void incIndex();
virtual char getChar();
virtual char getNextChar();
virtual void skipSpace();
virtual void skipChars(const std::string& chars);
virtual bool getCharsToDelimiter(const std::string& delimiters,
std::string& chars, bool inclDelimiter=false);
};
////////////////////////////////////////////////////////////////////////////////
class XmlNode
{
public:
std::string tag;
std::string text;
std::map<std::string, std::string> attributes;
std::vector<XmlNode*> children;
XmlNode* parent;
XmlNode(const std::string& tag, const std::string& text,
const std::map<std::string, std::string>& attributes, XmlNode* parent);
virtual ~XmlNode(void);
virtual void addChild(XmlNode* node);
virtual void appendText(const std::string& text);
virtual void toString(std::string& s);
};
////////////////////////////////////////////////////////////////////////////////
class XmlDocument
{
public:
std::deque<XmlNode*> stack;
XmlNode* root;
XmlDocument(void);
virtual ~XmlDocument(void);
virtual void pushNode(XmlNode* node);
virtual XmlNode* popNode();
virtual XmlNode* getCurNode();
virtual void startElement(const std::string& name,
const std::map<std::string, std::string>& attributes);
virtual void characters(const std::string& chars);
virtual void endElement(const std::string& name);
virtual void getNameAttrsFromTag(std::string& xml, std::string& name,
std::map<std::string,std::string>& attrs);
virtual void lex(const std::string& xml, std::vector<std::string>& chunks);
virtual XmlNode* parse(const std::string& xml, bool stripEmptyChars=true);
virtual void handleError(const std::string& msg);
// virtual void test();
};
//==============================================================================
// Utils
//==============================================================================
void trim(std::string &str){
int i = 0;
int len = str.size();
while (isspace(str[i]) != 0)
i++;
str = str.substr(i, str.length() - i);
i = str.size()-1;
len = str.size();
while (len > 0 && i >= 0 && isspace(str[i]) != 0)
i--;
if (len > 0 && i >= 0) {
str = str.substr(0, i + 1);
}
}
void replace(std::string &str, char oldC, char newC) {
for (uint i = 0; i < str.size(); i++) {
char c = str[i];
if (c == oldC) {
str[i] = newC;
}
}
}
bool strEq(const std::string& s1, const std::string& s2) {
return (s1.compare(s2) == 0);
}
bool charInString(const char c, const std::string& s) {
size_t pos = s.find(c);
return (pos != std::string::npos);
}
void compressReturns(std::string& xml) {
XBuffer xbuf(xml, 0);
std::string sOut;
while (!xbuf.atEnd()) {
std::string chars;
xbuf.getCharsToDelimiter("\n", chars, true);
sOut.append(chars);
xbuf.skipChars("\n");
}
xml.clear();
xml.append(sOut);
}
int getTagType(const std::string& xml) {
int len = xml.size();
if (xml[0] == '<' && xml[len-1] == '>') {
if ((xml.find("<?xml") == 0) && (xml[len-2] == '?')) {
return DECL_TAG;
} else if (xml.find("<!DOCTYPE") == 0) {
return DOCTYPE_TAG;
} else if ((xml.find("<!--") == 0) && (xml[len-2] == '-') && (xml[len-3] == '-')) {
return COMMENT_TAG;
} else if ((xml[1] != '/') && (xml[len-2] != '/')) {
return START_TAG;
} else if ((xml[1] != '/') && (xml[len-2] == '/')) {
return EMPTY_TAG;
} else if ((xml[1] == '/') && (xml[len-2] != '/')) {
return END_TAG;
}
}
return NO_TAG;
}
//==============================================================================
// XBuffer
//==============================================================================
XBuffer::XBuffer(const std::string& text, int index) :
text(text), index(index), len(text.length()) {
}
XBuffer::~XBuffer(void) {
}
bool XBuffer::atEnd() {
return (index >= len);
}
void XBuffer::incIndex() {
if (!atEnd()) {
index += 1;
}
}
char XBuffer::getChar() {
if (!atEnd()) {
return text.at(index);
}
return 0;
}
char XBuffer::getNextChar() {
incIndex();
return getChar();
}
void XBuffer::skipSpace() {
while (!atEnd()) {
int c = getChar();
if (isspace(c)) {
incIndex();
} else {
break;
}
}
}
void XBuffer::skipChars(const std::string& chars) {
while (!atEnd()) {
int c = getChar();
if (charInString(c, chars)) {
incIndex();
} else {
break;
}
}
}
bool XBuffer::getCharsToDelimiter(const std::string& delimiters,
std::string& chars, bool inclDelimiter) {
chars.clear();
bool foundDelimiter = false;
while (!atEnd() && !foundDelimiter) {
char c = getChar();
size_t found = delimiters.find(c);
foundDelimiter = (found != std::string::npos);
if (!foundDelimiter || (foundDelimiter && inclDelimiter)) {
chars.append(1, c);
incIndex();
}
}
return foundDelimiter;
}
//==============================================================================
// XmlNode
//==============================================================================
XmlNode::XmlNode(const std::string& tag, const std::string& text,
const std::map<std::string, std::string>& attributes, XmlNode* parent=NULL) :
tag(tag), text(text), attributes(attributes), parent(parent) {
if (parent) {
parent->addChild(this);
}
}
XmlNode::~XmlNode(void) {
for (std::vector<XmlNode*>::iterator it=children.begin(); it!=children.end(); ++it) {
XmlNode* node = *it;
delete node;
}
}
void XmlNode::addChild(XmlNode* node) {
children.push_back(node);
node->parent = this;
}
void XmlNode::appendText(const std::string& text_) {
text.append(text_);
}
void XmlNode::toString(std::string& s) {
std::string attrsStr;
for (std::map<std::string,std::string>::iterator it=attributes.begin(); it!=attributes.end(); ++it) {
if (attrsStr.size() != 0) {
attrsStr.append(" ");
}
attrsStr.append(it->first + "=\"" + it->second + "\"");
}
if (children.size() > 0 || text.size() > 0) {
if (attrsStr.size() > 0) {
s.append("\n<" + tag + " " + attrsStr + ">");
} else {
s.append("\n<" + tag + ">");
}
s.append("\n<" + tag + ">");
s.append(text);
for (std::vector<XmlNode*>::iterator it=children.begin(); it!=children.end(); ++it) {
std::string nodeString;
XmlNode* node = *it;
node->toString(nodeString);
s.append(nodeString);
}
s.append("</" + tag + ">\n");
} else {
if (attrsStr.size() > 0) {
attrsStr.insert(0, " ");
}
s.append("<" + tag + attrsStr + "/>\n");
}
compressReturns(s);
}
//==============================================================================
// XmlDocument
//==============================================================================
XmlDocument::XmlDocument(void) : root(NULL) {
}
XmlDocument::~XmlDocument(void) {
delete root;
}
void XmlDocument::handleError(const std::string& msg) {
std::cout << msg << std::endl;
}
void XmlDocument::pushNode(XmlNode* node) {
stack.push_back(node);
}
XmlNode* XmlDocument::popNode() {
XmlNode* node = getCurNode();
if (node) {
stack.pop_back();
} else {
std::string msg = "XmlDocument::popNode stack empty.";
handleError(msg);
}
return node;
}
XmlNode* XmlDocument::getCurNode() {
XmlNode* node = NULL;
int nodeCount = stack.size();
if (nodeCount > 0) {
node = stack[nodeCount-1];
}
return node;
}
void XmlDocument::startElement(const std::string& name, const std::map<std::string, std::string>& attributes) {
std::cout << "startElement: [" + name + "]" << std::endl;
XmlNode* node = new XmlNode(name, "", attributes);
if (!root) {
root = node;
}
XmlNode* curNode = getCurNode();
if (curNode) {
curNode->addChild(node);
}
pushNode(node);
}
void XmlDocument::characters(const std::string& chars) {
XmlNode* curNode = getCurNode();
if (curNode) {
curNode->appendText(chars);
}
}
void XmlDocument::endElement(const std::string& name) {
std::cout << "endElement: [" + name + "]" << std::endl;
XmlNode* node = popNode();
if (node && (node->tag.compare(name) != 0)) {
std::string msg = "XmlDocument::end_element: tag [";
msg.append(name + "] not matching node [");
msg.append(node->tag + "]");
handleError(msg);
}
}
void XmlDocument::getNameAttrsFromTag(std::string& xml, std::string& name,
std::map<std::string,std::string>& attrs) {
replace(xml, '\'', '"');
XBuffer xbuf(xml);
xbuf.incIndex();
xbuf.skipSpace();
xbuf.getCharsToDelimiter("' />", name);
char c = xbuf.getChar();
while (true) {
if (c == ' ') {
xbuf.skipSpace();
std::string attrName;
xbuf.getCharsToDelimiter("=", attrName);
c = xbuf.getNextChar();
if (c == '"') {
xbuf.incIndex();
std::string attrVal;
xbuf.getCharsToDelimiter("\"", attrVal);
c = xbuf.getChar();
if (c == '"') {
attrs[attrName] = attrVal;
xbuf.incIndex();
} else {
std::string msg = "XmlDoc::get_name_attrs_from_tag [";
msg.append(xml + "] reached end of buffer too soon.");
handleError(msg);
}
c = xbuf.getChar();
}
} else {
break;
}
}
}
void XmlDocument::lex(const std::string& xml, std::vector<std::string>& chunks) {
// Divide xml into raw items delimited by '<' and '>' pair or not so delimited.
std::string chars;
XBuffer xbuf(xml);
while (!xbuf.atEnd()) {
bool found = xbuf.getCharsToDelimiter("<", chars);
bool charsEmpty = (chars.size() == 0);
bool charsReturn = (chars.compare("\n") == 0);
if (found && !charsEmpty && !charsReturn) {
chunks.push_back(chars);
}
if (!xbuf.atEnd()) {
bool found = xbuf.getCharsToDelimiter(">", chars, true);
bool charsEmpty = (chars.size() == 0);
if (found && !charsEmpty) {
chunks.push_back(chars);
}
}
}
}
XmlNode* XmlDocument::parse(const std::string& xml, bool stripEmptyChars) {
std::vector<std::string> chunks;
lex(xml, chunks);
if (chunks.size() > 0) {
for (unsigned int i = 0; i < chunks.size(); i++) {
std::string name;
std::map<std::string, std::string> attrs;
std::string chunk = chunks[i];
int tagType = getTagType(chunk);
switch (tagType) {
case START_TAG:
getNameAttrsFromTag(chunk, name, attrs);
startElement(name, attrs);
break;
case END_TAG:
name = chunk.substr(2, chunk.size()-3);
endElement(name);
break;
case EMPTY_TAG:
getNameAttrsFromTag(chunk, name, attrs);
startElement(name, attrs);
endElement(name);
break;
case COMMENT_TAG:
break;
case DECL_TAG:
break;
case DOCTYPE_TAG:
break;
default:
if (stripEmptyChars == true) {
trim(chunk);
}
characters(chunk);
}
}
}
return root;
}
////////////////////////////////////////////////////////////////////////////////
const std::string XML = "<catalog> \
<book id= \"bk101 \"> \
<author>gambardella, matthew</author> \
<title>xml developer \'s guide</title> \
<genre>computer</genre> \
<price>44.95</price> \
<publish_date>2000-10-01</publish_date> \
<description>an in-depth look at creating applications \
with xml.</description> \
</book> \
<book id= \"bk102 \"> \
<author>ralls, kim</author> \
<title>midnight rain</title> \
<genre>fantasy</genre> \
<price>5.95</price> \
<publish_date>2000-12-16</publish_date> \
<description>a former architect battles corporate zombies, \
an evil sorceress, and her own childhood to become queen \
of the world.</description> \
</book> \
<book id= \"bk103 \"> \
<author>corets, eva</author> \
<title>maeve ascendant</title> \
<genre>fantasy</genre> \
<price>5.95</price> \
<publish_date>2000-11-17</publish_date> \
<description>after the collapse of a nanotechnology \
society in england, the young survivors lay the \
foundation for a new society.</description> \
</book> \
</catalog> \
";
void test() {
XmlDocument xdoc;
xdoc.parse(XML);
std::string s;
xdoc.root->toString(s);
std::cout << s << std::endl;
}
int main(int argc, char *argv[]) {
std::cout << "MicroXml Recipe." << std::endl;
test();
std::cout << "Press RETURN." << std::endl;
std::cin.get();
return 0;
}
|
Nearly all XML files I work with are just tags, texts and sometimes attributes. When I was using a full XML library, I found it unwieldy to examine these nodes in a debugger. Furthermore, as I moved from one XML library to another, I found it always took some time to work out how to use the different APIs. For data which boils down to a simple node tree, this seems like overkill.
I'm glad the big libraries are around to handle the full glory of XML but it's much more than I need when I'm reading in some settings or other basic structured data. So I've been using different versions of this library for years and translated it into Java, Python and C# as well.