- Main Page
- Namespaces
- Classes
- Files
- Related Pages

# /Users/fpy/titan-1-5-MBARI/Mers/Utils/Io/Xml/Stream/Libxml2/test2.cpp

    00001
    00002 //
    00003 // Copyright (c) 2004           Model-based Embedded and Robotic Systems Group
    00004 // All Rights Reserved          Massachusetts Institute of Technology
    00005 //
    00006 // This software is provided as is. Model-based Embedded and Robotic Systems
    00007 // (MERS) group does not assume any responsibility.
    00008 //
    00009 // Organization:        Model-based Embedded and Robotic Systems Group
    00010 //                                      Massachusetts Institute of Technology
    00011 //
    00013
    00014 #include "Libxml2StreamReader.h"
    00015 #include "../XmlStreamException.h"
    00016 #include "../XmlStreamObjectFactory.h"
    00017 #include <iostream>
    00018
    00019 using namespace Mers::Utils::Io::Xml::Stream::Libxml2;
    00020 using namespace std;
    00021
    00022 class MyFactory : public XmlStreamObjectFactory {
    00023 public:
    00024   MyFactory(XmlStreamReader& reader) : XmlStreamObjectFactory(reader) {}
    00025
    00026   void parseDocument() {
    00027     if(!checkStart("MOF")) cout << "Not an MOF start tag!" << endl;
    00028     XmlStreamReader::EventType event = reader.nextTag();
    00029     while(!reader.isEndElement()) {
    00030       cout << "Element: " << reader.getLocalName()
    00031            << ", name attribute " << reader.getAttributeValue("name") << endl;
    00032       cout << "  Event type: " << reader.getEventType() << ", chars="
    00033            << reader.getText() << endl;
    00034       event = reader.nextSiblingTag();
    00035     }
    00036   }
    00037 };
    00038
    00039 int main(int argc, char* argv[])
    00040 {
    00041   // initialize the library and check for potential version mismatches
    00042   LIBXML_TEST_VERSION
    00043
    00044   try {
    00045     Libxml2StreamReader reader(argv[1]);
    00046     if(reader.getEventType()==XmlStreamReader::START_DOCUMENT) {
    00047       cout << "StartDocument\n";
    00048     } else {
    00049       cout << "ERROR: getEventType() returned a weird event type!\n";
    00050     }
    00051
    00052     reader.next();
    00053     MyFactory factory(reader);
    00054     factory.parseDocument();
    00055
    00056     /*
    00057     while(reader.hasNext()) {
    00058       reader.next();
    00059       if(reader.isStartElement()) {
    00060         cout << "StartElement: " << reader.getLocalName() << endl;
    00061       } else if(reader.isEndElement()) {
    00062         cout << "EndElement: " << reader.getLocalName() << endl;
    00063       } else if(reader.isCharacters()) {
    00064         cout << "Characters: " << reader.getText() << endl;
    00065       } else if(reader.isWhitespace()) {
    00066         cout << '.';
    00067       } else if(reader.getEventType()==XmlStreamReader::END_DOCUMENT) {
    00068         cout << "EndDocument";
    00069       } else {
    00070         cout << "Unrecognized event. " << endl;
    00071       }
    00072       }*/
    00073
    00074     // clean-up function for libxml2
    00075     xmlCleanupParser();
    00076
    00077   } catch(XmlStreamException e) {
    00078     cerr << e.what() << endl;
    00079   } catch(...) {
    00080     cerr << "Unknown exception" << endl;
    00081   }
    00082 }