////////////////////////////////////////////////////////////// ///Stores results of analysis into a text file void FileSniffer::StoreResult() { try{ std::stringstream ss; ss<<newfilesList.size(); XmlWriter writer; //create new xml writer.addDeclaration(); writer.start("filelist"); XmlWriter childnode1; childnode1.start("listsize"); //add filepath tag for each path childnode1.addBody(ss.str()); //add path childnode1.end(); //add ending tag writer.addBody(childnode1.xml()); for (std::set<std::string>::iterator it=newfilesList.begin(); it != newfilesList.end(); it++) //add all paths in fileset to the xml { XmlWriter childnode; childnode.start("filepath"); //add filepath tag for each path childnode.addBody(*it); //add path childnode.end(); //add ending tag writer.addBody(childnode.xml()); } writer.end(); std::ofstream myfile; //create new file and write the xml in it myfile.open (resultFilePath.c_str()); myfile << writer.xml(); myfile.close(); } catch(std::exception ex) { std::cout << "\n " << ex.what() << "\n\n"; } }
////////////////////////////////////////////////////////////////////////////////////////// ///Creates the XML database file from a set of filepaths and returns number of new paths int FileSniffer::createDatabase(std::set<std::string>& filelist) { int count=0; try{ XmlWriter writer; //create new xml writer.addDeclaration(); writer.start("filelist"); newfilesList.clear(); for (std::set<std::string>::iterator it=filelist.begin(); it != filelist.end(); it++) //add all paths in fileset to the xml { if(!isInExcluded(*it)) { count++; XmlWriter childnode; childnode.start("filepath"); //add filepath tag for each path childnode.addBody(*it); //add path childnode.end(); //add ending tag writer.addBody(childnode.xml()); newfilesList.insert(*it); } } writer.end(); std::ofstream myfile; //create new file and write the xml in it myfile.open (databaseFilePath.c_str()); myfile << writer.xml(); myfile.close(); StoreResult(); } catch(std::exception ex) { std::cout << "\n " << ex.what() << "\n\n"; } return count; }
void main() { //mOut("Testing XmlWriter"); //mOut("================="); //mOut("creating XML writer object:"); XmlWriter wtr; //mOut("adding XML declaration:"); wtr.addDeclaration(); //mOut(wtr.xml()); //mOut("adding comment:"); wtr.addComment("top level comment"); //mOut(wtr.xml()); //mOut("adding root element:"); wtr.start("root"); //mOut(wtr.xml()); //mOut("adding attributes:"); wtr.addAttribute("att1","val1"); wtr.addAttribute("att2","val2"); //mOut(wtr.xml()); //mOut("adding comment:"); wtr.addComment("comment in root's body"); //mOut(wtr.xml()); //mOut("Creating self-closing element:"); XmlWriter sub1; sub1.start("child1 /"); // mOut(sub1.xml()); //mOut("adding attribute:"); sub1.addAttribute("c1name","c1value"); XmlWriter subsub1; subsub1.start("grand"); subsub1.addBody("test grandchild"); subsub1.end(); sub1.addBody(subsub1.xml()); //mOut(sub1.xml()); //mOut("adding child to root's body:"); wtr.addBody(sub1.xml()); //mOut(wtr.xml()); //mOut("adding another comment"); wtr.addComment("another root's body comment"); //mOut(wtr.xml()); //mOut("adding string to root's body:"); wtr.addBody("root's body"); //mOut(wtr.xml()); //mOut("closing root element:\n"); wtr.end(); mOut(wtr.xml()); mOut("\n writing XML to file \"Test.xml\":"); std::ofstream out("test.xml"); if(out.good()) { out << wtr.xml().c_str(); out.close(); } std::cout << std::endl; /* mOut("creating composite element:"); XmlWriter cwtr, bcwtr1, bcwtr2; std::string temp = bcwtr1.element("child1","child1's body") .element("child2","child2's body").xml(); std::cout << "\n " << bcwtr1.xml(); bcwtr1.clear(); std::cout << "\n " << cwtr.start("parent") .addBody(bcwtr1.element("child1","child1's body").xml()) .addBody(bcwtr2.element("child2","body2").xml()) .end().xml(); std::cout << "\n\n";*/ }