1 / 35

Using an XML Parser

Using an XML Parser. Download. XML Parser example as of end of last class: http://www.cse.usf.edu/~turnerr/Object_Oriented_Design/Downloads/2011_03_30_XML_Parser/ Extract Build Run. 1. 1. 1. 1. 1. 1. 2. 2. 2. 2. 2. 2. 3. 3. 3. 3. 3. 3. 4. 4. 4. 4. 4. 4. 5. 5. 5. 5.

neviah
Download Presentation

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. Download • XML Parser example as of end of last class: • http://www.cse.usf.edu/~turnerr/Object_Oriented_Design/Downloads/2011_03_30_XML_Parser/ • Extract • Build • Run

  3. 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4 5 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 Row F Row E Row D Row C Row B Row A The Little Theater Center Section Right Section Back Section Left Section Front Section

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

  5. The Address Node TiXmlNode* address_node = name_node->NextSibling(); assert(address_node != 0); cout << address_node->Value() << endl; cin.get(); return 0; }

  6. The Address Node

  7. Adding a Boundary Class • main() is getter verbose! • Let's put this code into a boundary class. • Add class Venue_from_Xml

  8. Venue_from_Xml.h #pragma once #include "tinyxml.h" class Venue_from_Xml { public: static void Get_Venue(TiXmlNode* venue_node); }; We don't need a constructor or destructor. We will never instantiate this class.

  9. Venue_from_Xml.cpp #include <iostream> #include "Venue_from_Xml.h" #include "tinyxml.h" using namespace std; void Venue_from_Xml::Get_Venue(TiXmlNode* venue_node) { }

  10. Move Code from main() Add #include "Venue_from_Xml.h" Move these statements to new class Venue_from_Xml

  11. Venue_from_Xml.cpp #include <iostream> #include "Venue_from_Xml.h" #include "tinyxml.h" using namespace std; void Venue_from_Xml::Get_Venue(TiXmlNode* venue_node) { 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; TiXmlNode* address_node = name_node->NextSibling(); assert(address_node != 0); cout << address_node->Value() << endl; }

  12. main.cpp #include <iostream> #include <string> #include <cassert> #include "tinyxml.h" #include "Venue_from_Xml.h" using namespace std; int main() { cout << "This is venue_test\n"; ... TiXmlNode* venue_node = venue_file_node->FirstChild(); assert(venue_node != 0); cout << venue_node->Value() << endl; Venue_from_Xml::Get_Venue(venue_node); cin.get(); return 0; }

  13. Program Running Works the same!

  14. Add Get_Address #pragma once #include "tinyxml.h" class Venue_from_Xml { public: static void Get_Venue(TiXmlNode* venue_node); private: static void Get_Address(TiXmlNode* address_node); };

  15. Add Get_Address to cpp File void Venue_from_Xml::Get_Address(TiXmlNode* address_node) { TiXmlNode* street_node = address_node->FirstChild(); assert(street_node != 0); cout << street_node->FirstChild()->Value() << endl; TiXmlNode* city_node = street_node->NextSibling(); assert(city_node != 0); cout << city_node->FirstChild()->Value() << endl; TiXmlNode* state_node = city_node->NextSibling(); assert(state_node != 0); cout << state_node->FirstChild()->Value() << endl; TiXmlNode* zip_code = state_node->NextSibling(); assert(zip_code != 0); cout << zip_code->FirstChild()->Value() << endl; } Note shortcut

  16. Add Get_Address void Venue_from_Xml::Get_Venue(TiXmlNode* venue_node) { 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; TiXmlNode* address_node = name_node->NextSibling(); assert(address_node != 0); cout << address_node->Value() << endl; Get_Address(address_node); }

  17. The Address in Output

  18. On Unix • Try it on Unix. • Create a new directory. • Copy source files and Venue.xml. • Compile • Execute

  19. Copying Files to Circe

  20. Files on Circe

  21. Running on Circe

  22. Get the Seats

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

  24. Get the Seats • Start by getting a single seat row. void Venue_from_Xml::Get_Seat_Row(TiXmlNode* seat_row_node) { TiXmlNode* name_node = seat_row_node->FirstChild("name"); assert(name_node != 0); cout << name_node->Value() << ": "; cout << name_node->FirstChild()->Value() << endl; } void Venue_from_Xml::Get_Seats(TiXmlNode* seat_row_node) { Get_Seat_Row(seat_row_node); }

  25. Get the Seats void Venue_from_Xml::Get_Venue(TiXmlNode* venue_node) { 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; TiXmlNode* address_node = name_node->NextSibling(); assert(address_node != 0); cout << address_node->Value() << endl; Get_Address(address_node); TiXmlNode* seat_row_node = address_node->NextSibling(); assert(seat_row_node != 0); cout << seat_row_node->Value() << endl; Get_Seats(seat_row_node ); }

  26. Update Venue_from_Xml.h #pragma once #include "tinyxml.h" class Venue_from_Xml { public: Venue_from_Xml(void) {}; static void Get_Venue(TiXmlNode* venue_node); private: static void Get_Address(TiXmlNode* address_node); static void Get_Seat_Row(TiXmlNode* seat_row_node); static void Get_Seats(TiXmlNode* seat_row_node); };

  27. First Seat Row Name

  28. Get All Seats in the Row void Venue_from_Xml::Get_Seat_Row(TiXmlNode* seat_row_node) { TiXmlNode* name_node = seat_row_node->FirstChild("name"); assert(name_node != 0); cout << name_node->Value() << ": "; cout << name_node->FirstChild()->Value() << endl; TiXmlNode* seat_node = seat_row_node->FirstChild("seat"); while (seat_node != 0) { cout << seat_node->Value() << " "; Get_Seat(seat_node); seat_node = seat_node->NextSibling(); } }

  29. Get Seat void Venue_from_Xml::Get_Seat(TiXmlNode* seat_node) { TiXmlNode* number_node = seat_node->FirstChild("number"); cout << number_node->Value() << ": "; cout << number_node->FirstChild()->Value() << " "; TiXmlNode* section_node = seat_node->FirstChild("section"); cout << section_node->Value() << ": "; cout << section_node->FirstChild()->Value() << endl; }

  30. Update Venue_from_Xml.h #pragma once #include "tinyxml.h" class Venue_from_Xml { public: Venue_from_Xml(void) {}; static void Get_Venue(TiXmlNode* venue_node); private: static void Get_Address(TiXmlNode* address_node); static void Get_Seat_Row(TiXmlNode* seat_row_node); static void Get_Seats(TiXmlNode* seat_row_node); static void Get_Seat(TiXmlNode* seat_node); };

  31. Seats Displayed

  32. Observe Structure

  33. Get all seat rows void Venue_from_Xml::Get_Seats(TiXmlNode* seat_row_node) { while (seat_row_node != 0) { cout << seat_row_node->Value() << ": "; Get_Seat_Row(seat_row_node); seat_row_node = seat_row_node->NextSibling(); } }

  34. All Seat Rows in Output

  35. All Seat Rows in Output

More Related