/* FemtoXML_CPP - A c++ wrapper for FemtoXML * FemtoXML - An easy to use XML library * * See doc/license.txt for license information (GPLv3) * * Authors: * Fredrik Hultin, noname@ the domain two lines below, 2007 * * http://nurd.se/~noname/femtoxml */ /* music.cpp * * Music collection example for the FemtoXML XML parser using the C++ wrapper. * To be used with the provided 'music.xml' (or any other file with the same structure). */ #include "document.h" #include #include // Example message handler void message(fxml_msg* msg, void* data) { std::cout << "Message from FemtoXML: " << msg->typeName << "(" << msg->type << "): " << msg->msg << std::endl; } int main(int argc, const char* const* argv) { // Create our document FemtoXML::Document document; // Check so we're given an argument if(argc != 2){ std::cout << "Usage: " << argv[0] << " " << std::endl; return 1; } // Start a try statement around parsing try{ // Open our document document.load(argv[1]); // This is the code for reading an XML-file with the structure of the provided 'music.xml'. // If the file doesn't contain any 'collection' elements, die if(document.root().count(FXML_TYPE_ELEMENT, "collection") <= 0){ throw std::runtime_error("The specified file apperas to be valid XML (or plain text), but contains no 'collection'-elements."); } // Loop through all the collections in the file for(int i=0; i < document.root().count("collection"); i++){ FemtoXML::Element collection = document.root().get("collection", i); // Checks if the collection has a description and print it if it indeed does if(collection.count(FXML_TYPE_ATTRIBUTE, "description") > 0){ std::cout << "Collection with name: " << collection.get(FXML_TYPE_ATTRIBUTE, "description").get(FXML_TYPE_ATTRIBUTE_VAL, 0).value() << std::endl << std::endl; }else{ std::cout << "Collection with no name" << std::endl << std::endl; } // Loop through all the albums in the collection for(int i2=0; i2 < collection.count("album"); i2++){ FemtoXML::Element album = collection.get("album", i2); std::cout << "Title:\t" << album.get("title").get(0).value() << std::endl; std::cout << "Artist:\t" << album.get("artist").get(0).value() << std::endl; std::cout << "Year:\t" << album.get("year").get(0).value() << std::endl; std::cout << "Track listing:" << std::endl; // Loop through all the tracks in the album for(int i3=0; i3 < album.count("track"); i3++){ FemtoXML::Element track = album.get("track", i3); std::cout << "\t" << track.get("number").get(0).value() << ": " << track.get(FXML_TYPE_TEXT, 0).value() << " (" << track.get("length").get(0).value() << ")" << std::endl; } std::cout << std::endl; } } } // Catch any runtime_error-exceptions generated by FemtoXML_CPP catch(std::runtime_error e){ // Print what went wrong std::cout << e.what() << std::endl; } return 0; }