void XMLParser::containsAttribute(XmlReader xrdPtr, shared_ptr < AbstractXmlElement > srd_ptr) { XmlReader::attribElems attribs = xrdPtr.attributes(); if (attribs.size() > 0) { for (size_t i = 0; i < attribs.size(); ++i) { srd_ptr->addAttrib(attribs[i].first, attribs[i].second); } } }
int main() { std::cout << "\n Testing XmlReader"; std::cout << "\n ===================\n"; std::string Prologue = "<?xml version=\"1.0\"?><!-- top level comment -->"; std::string test1 = Prologue; test1 += "<parent att1='val1' att2='val2'><child1 /><child2>child2 body<child1 /></child2></parent>"; std::string test2 = Prologue; test2 += "<Dir><path>/TestFile/Dir1</path><File><name>text1.txt</name><date>02/11/2012</date></File></Dir>"; XmlReader rdr(test1); std::cout << "\n source = " << test1.c_str() << std::endl; // testing helper function std::cout << "\n Testing extractIdentifier"; std::cout << "\n ---------------------------"; std::string ident; size_t position = 1; do { ident = rdr.extractIdentifier(position); std::cout << "\n " << ident.c_str(); } while(ident.size() > 0); // testing typical use, step through each element extracting tags, attributes, // bodies, and or entire element strings std::cout << "\n testing next(), tag(), element(), body(), and attributes()"; std::cout << "\n -----------------------------------------------------------"; rdr.reset(); while(rdr.next()) { std::cout << "\n tag: " << rdr.tag().c_str(); std::cout << "\n element: " << rdr.element().c_str(); std::cout << "\n body: " << rdr.body().c_str(); XmlReader::attribElems attribs = rdr.attributes(); for(size_t i=0; i<attribs.size(); ++i) std::cout << "\n attributes: " << attribs[i].first.c_str() << ", " << attribs[i].second.c_str(); } std::cout << "\n\n"; // testing typical use, step through each element extracting tags, attributes, // bodies, and or entire element strings rdr.xml() = test2; std::cout << "\n source = " << rdr.xml().c_str() << std::endl; std::cout << "\n testing next(), tag(), element(), body(), and attributes()"; std::cout << "\n -----------------------------------------------------------"; rdr.reset(); while(rdr.next()) { std::cout << "\n tag: " << rdr.tag().c_str(); std::cout << "\n element: " << rdr.element().c_str(); std::cout << "\n body: " << rdr.body().c_str(); XmlReader::attribElems attribs = rdr.attributes(); for(size_t i=0; i<attribs.size(); ++i) std::cout << "\n attributes: " << attribs[i].first.c_str() << ", " << attribs[i].second.c_str(); } std::cout << "\n\n"; return 0; }