• Main Page
  • Namespaces
  • Classes
  • Files
  • Directories
  • File List
  • File Members

example1.cpp

Go to the documentation of this file.
00001 
00002  //
00003  // XML storage classes
00004  //
00005  // example1.cpp
00006  //
00007  // Copyright (c) 2005, 2010 Martin Fuchs <martin-fuchs@gmx.net>
00008  //
00009 
00010 
00011 /*
00012 
00013   All rights reserved.
00014 
00015   Redistribution and use in source and binary forms, with or without
00016   modification, are permitted provided that the following conditions are met:
00017 
00018   * Redistributions of source code must retain the above copyright
00019     notice, this list of conditions and the following disclaimer.
00020   * Redistributions in binary form must reproduce the above copyright
00021     notice, this list of conditions and the following disclaimer in
00022     the documentation and/or other materials provided with the
00023     distribution.
00024 
00025   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00026   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00028   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00029   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00030   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00031   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00032   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00033   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00034   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00035   POSSIBILITY OF SUCH DAMAGE.
00036 
00037 */
00038 
00039 //#define XS_USE_EXPAT
00040 
00041 //#define XML_STATIC
00042 #include <xmlstorage.h>
00043 using namespace XMLStorage;
00044 
00045 #include <iostream>
00046 using namespace std;
00047 
00048 
00049 void test_write()
00050 {
00051     XMLDoc doc;
00052     XMLPos pos(&doc);
00053 
00054      // Zusammenstellen der XML-Struktur im Dokument 'doc'
00055     pos.create("PARAMS");
00056       pos.create("TEXT");
00057         pos->set_content("Testgruppe und Straße");
00058       pos.back(); // </TEXT>
00059 
00060       pos.create("COLOR");
00061         pos.create("RED");
00062         pos->set_content("234");
00063         pos.back(); // </RED>
00064 
00065         pos.create("GREEN");
00066         pos->set_content("22");
00067         pos.back(); // </GREEN>
00068 
00069         pos.create("BLUE");
00070         pos->set_content("33");
00071         pos.back(); // </BLUE>
00072       pos.back(); // </COLOR>
00073 
00074       pos.create("FONT");
00075         pos.create("FONT_FILENAME");
00076         pos->set_content("Arial.ttf");
00077         pos.back(); // </FONT_FILENAME>
00078 
00079         pos.create("SIZE");
00080         pos->set_content("12");
00081         pos.back(); // </SIZE>
00082       pos.back(); // </FONT>
00083 
00084       pos.create("NAME");
00085         pos->set_content("Müller ÄÖÜ ß");
00086       pos.back(); // </NAME>
00087     pos.back(); // </PARAMS>
00088 
00089      // Schreiben der XML-Datei
00090     doc.write_file(TEXT("example1.xml"));
00091 }
00092 
00093 
00094 void test_read()
00095 {
00096     XMLDoc doc;
00097 
00098      // Einlesen des XML-Files
00099     doc.read_file(TEXT("example1.xml"));
00100 
00101     XMLPos pos(&doc);
00102     string text, filename, name;
00103 
00104      // Abfrage eines Elementinhalts, beispielsweise von FONT_FILENAME
00105     if (pos.go_down("PARAMS")) {
00106       if (pos.go_down("TEXT")) {
00107         text = pos->get_content();
00108         pos.back(); // </TEXT>
00109       }
00110 
00111        // Abfrage eines Elementinhalts, beispielsweise von FONT_FILENAME
00112       if (pos.go_down("NAME")) {
00113         name = pos->get_content();
00114         pos.back();
00115       }
00116       pos.back(); // </PARAMS>
00117     }
00118 
00119      // Abfrage eines Elementinhalts, beispielsweise von FONT_FILENAME
00120     if (pos.go("PARAMS/FONT/FONT_FILENAME")) {
00121         filename = pos->get_content();
00122         pos.back();
00123     }
00124 
00125      // Ausgabe der ermittelten Werte
00126     cout << "text: " << text << '\n';
00127     cout << "font filename: " << filename << '\n';
00128     cout << "name: " << name << '\n';
00129 }
00130 
00131 
00132 int main()
00133 {
00134     test_write();
00135     test_read();
00136 
00137     return 0;
00138 }

Generated on Fri Nov 14 2014 00:05:04 for XMLStorage/C++ by  doxygen 1.7.2