1 / 27

Using an XML Parser

Using an XML Parser. Objective. You will be able to use a publically available open source parser to convert an XML file into an internal data structure accessible by your C++ code. Download Venue.xml. If you don't already have it, download file Venue.xml from the class web site.

moe
Télécharger la présentation

Using an XML Parser

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Using an XML Parser

  2. Objective • You will be able to use a publically available open source parser to convert an XML file into an internal data structure accessible by your C++ code.

  3. Download Venue.xml • If you don't already have it, download file Venue.xml from the class web site. • http://www.cse.usf.edu/~turnerr/Object_Oriented_Design/Downloads/2011_03_28_XML/Venue.xml

  4. tinyxml • http://www.grinninglizard.com/tinyxml/

  5. Download from sourceforge http://sourceforge.net/projects/tinyxml/

  6. Download from sourceforge

  7. Extract

  8. Getting Started • Create a new empty C++ console application. • test_venue_xml • Add new item test_venue.cpp • Start with Hello World. #include <iostream> using namespace std; int main() { cout << "This is venue_test\n"; cin.get(); cin.get(); return 0; }

  9. Getting Started • Build and run.

  10. Add Venue.xml • Copy file Venue.xml into your default directory. • Same directory as test_venue_xml.cpp

  11. Copy Downloaded Source files • Copy tinyxml source files into the same directory and add to the project. • tinyxml.cpp, tinyxml.h, tinyxmlerror.cpp, tinyxmlparser.cpp

  12. Add a #define to tinyxml.h

  13. Start a Real Main #include <iostream> #include <cassert> #include <string> #include "tinyxml.h" using namespace std; int main() { cout << "This is venue_test\n"; string filename = "Venue.xml"; TiXmlDocument doc(filename); cin.get(); cin.get(); return 0; } Build and run.

  14. Initial Test #include <iostream> #include <string> #include <cassert> #include "tinyxml.h" using namespace std; int main() { cout << "This is venue_test\n"; string filename = "Venue.xml"; TiXmlDocument doc(filename); bool loadOkay = doc.LoadFile(); if (!loadOkay) { cout << "Could not load file " << filename << endl; cout << "Error='" << doc.ErrorDesc() <<"'. Exiting.\n"; cin.get(); exit( 1 ); } cout << "Demo doc read from disk\n"; cout << "Printing via doc.Print \n"; doc.Print( stdout ); cin.get(); return 0; }

  15. Initial Test Run

  16. Initial Test Run (continued)

  17. Venue.xml

  18. Venue.xml DOM Document venue_file venue ... seat_row seat_row name seat_row address The Little Theater name seat number section A 1 Front state zip_code city street 01420 MA 145 Main Street Littleton

  19. Looking at Nodes in the DOM • At end of main(): //cout << "Printing via doc.Print \n"; //doc.Print( stdout ); TiXmlNode* venue_file_node = doc.FirstChild("venue_file"); assert(venue_file_node != 0); cout << venue_file_node->Value() << endl; cin.get(); return 0; }

  20. The First Node

  21. Navigating the DOM TiXmlNode* venue_file_node = doc.FirstChild("venue_file"); assert(venue_file_node != 0); cout << venue_file_node->Value() << endl; TiXmlNode* venue_node = venue_file_node->FirstChild(); assert(venue_node != 0); cout << venue_node->Value() << endl; cin.get(); return 0; }

  22. Second Node

  23. More Nodes TiXmlNode* venue_file_node = doc.FirstChild("venue_file"); assert(venue_file_node != 0); cout << venue_file_node->Value() << endl; TiXmlNode* venue_node = venue_file_node->FirstChild(); assert(venue_node != 0); cout << venue_node->Value() << endl; TiXmlNode* name_node = venue_node->FirstChild(); assert(name_node != 0); cout << name_node->Value() << endl; cin.get(); return 0; }

  24. More Nodes

  25. A Text Value TiXmlNode* venue_file_node = doc.FirstChild("venue_file"); assert(venue_file_node != 0); cout << venue_file_node->Value() << endl; TiXmlNode* venue_node = venue_file_node->FirstChild(); assert(venue_node != 0); cout << venue_node->Value() << endl; TiXmlNode* name_node = venue_node->FirstChild(); assert(name_node != 0); cout << name_node->Value() << endl; TiXmlNode* name_text_node = name_node->FirstChild(); assert(name_text_node != 0); cout << name_text_node->Value() << endl; cin.get(); return 0; }

  26. A Text Value

  27. Conclusion • We can use the tinyxml api to navigate the DOM. • Get to any node. • Retrieve its data.

More Related