/*! \file nodetest.cpp test project */ #include #include #include "xmlpp.h" using namespace xmlpp; using namespace std; int main() { // every single document needs its own context // this is necessary for DTD handling and entity mapping XMLContextPtr ctx(new XMLContext); XMLContextPtr ctx2(new XMLContext); XMLDocument doc(ctx); XMLDocument doc_out(ctx2); XMLNodeListIterator it; /* // testing of assignment operators XMLNode z1(ctx2,"node1"); XMLNodePtr z2=new XMLNode(ctx2,"node2"); XMLNode z4(ctx2); // empty node z4=z2; // assignment from ptr cerr << z4.name() << endl; z4=z1; // assignment from ref cerr << z4.name() << endl; */ /* // testing of document generation from code XMLNode z4(ctx2, "node4"); z1->add_child(z4); z1->add_child(z2); XMLNode *z3=new XMLNode(ctx2,"node3"); z1->add_child(z3); XMLNodeList clist; clist=z1->children(); it=clist.begin(); while(it!=clist.end()) { cerr << "name: " << (*it)->name() << endl; it++; } doc_out.add_child(z1); XMLNode *pi1=new XMLNode(ctx2, "xml"); pi1->add_attr("version", "1.0"); doc_out.add_pi(pi1); z1->add_attr("value", "true"); z1->add_attr("visible", "false"); // save the constructed node ofstream outstream("test.xml"); if (outstream.is_open()) doc_out.save(outstream); */ // testing of loading data from a file // libxmlpp takes ~340ms on a P3-500 to // load and parse ~270kB xml data string infile("hamlet.xml"); /* ifstream istr(infile.c_str()); try { doc.load(istr, ctx); } catch (xmlerror e) { e.show_error(ctx); e.show_line(ctx, infile); exit(1); } */ // load a xml document from a file try { doc.load_file(infile); } catch (xmlerror e) { cerr << "Error: " << e.get_string() << endl; if(e.get_info().size()) { cerr << "File: " << e.get_info() << endl; } if(e.get_error()!=xml_filename_invalid && e.get_error()!=xml_file_access) { e.show_error(ctx); e.show_line(ctx, infile); } exit(1); } cerr << "succesfully loaded " << infile << endl; XMLNodeList nl, x1, x2; XMLNodeListIterator xIt; /* // list all process instructions // process instructions (= ) // are kept as a XMLNodeList in class XMLDocument, // so are (will be) DTD tags nl=doc.get_pi_list(); it=nl.begin(); while(it!=nl.end()) { cerr << "pi name: " << (*it)->name() << endl; XMLAttributes pia=(*it)->get_attrmap(); XMLAttributes::const_iterator aIt; for(aIt=pia.begin(); aIt!=pia.end(); aIt++) { XMLAttributes::value_type attr = *aIt; cerr << ' ' << attr.first.c_str() << '=' << '\"' << attr.second.c_str() << '\"'; cerr << endl; } it++; } */ // these two instructions are equivalent //nl=doc.firstchild("PLAY")->children("ACT"); nl=doc.firstchild("uiconfig")->firstchild("iconbar")->children(); it=nl.begin(); while(it!=nl.end()) { cerr << "in name: " << (*it)->name() << endl; try { x2=(*it)->children("item"); xIt=x2.begin(); while(xIt!=x2.end()) { cerr << " in name: " << (*xIt)->name() << endl; xIt++; } } catch (xmlerror e) { cerr << "Error: " << e.get_string() << endl; it++; continue; } it++; } /* // save document from local stream ofstream outstr("z.xml"); if (outstr.is_open()) doc.save(outstr); */ // save document from filename // if filename is omitted, // the filename at time of loading is used or the filename, // which was set by ->filename("xxx"). try { // we won't overwrite the original file // this could also be done by just .save_file("name") doc.filename("z.xml"); doc.save_file(); } catch (xmlerror e) { cerr << "Error: " << e.get_string() << endl; if(e.get_info().size()) { cerr << "File: " << e.get_info() << endl; } if(e.get_error()!=xml_filename_invalid && e.get_error()!=xml_file_access) { e.show_error(ctx); e.show_line(ctx, infile); } exit(1); } return 0; } /* vi: set ts=3: */