Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleXMLBase.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 
26 /**
27  @example ExampleXMLBase.cpp
28  @relates XMLIO,XMLBase
29  @brief Example XMLBase operations
30  @ingroup g_examples
31  @author MIP
32 */
33 
34 #include <Base/Common/XMLIO.hh>
35 #include <Base/Debug/Debug.hh>
36 #include <Base/Common/XMLBase.hh>
37 
38 using namespace BIAS;
39 using namespace std;
40 
41 // ############### this is the test class with xml support ###############
42 
43 #define VERSIONOFTESTCLASS 0.37
44 /** \cond POORLY_NAMED_EXAMPLE_CLASSES **/
45 /* some base */
46 class myTestClass: public XMLBase, Debug {
47 public:
48  virtual int XMLGetClassName(string& TopLevelTag, double& Version) const;
49  virtual int XMLOut(const xmlNodePtr Node, XMLIO& XMLObject) const;
50  virtual int XMLIn(const xmlNodePtr Node, XMLIO& XMLObject);
51  virtual ~myTestClass();
52  int thedata;
53 };
54 
55 // ################## implementation (virtual functions) ##################
56 
57 myTestClass::~myTestClass() {}
58 
59 int myTestClass::XMLGetClassName(std::string& TopLevelTag,
60  double &Version) const {
61  TopLevelTag = "myTestClass";
62  Version = VERSIONOFTESTCLASS;
63  return 0;
64 }
65 
66 int myTestClass::XMLOut(const xmlNodePtr Node, XMLIO& XMLObject) const {
67  XMLObject.addAttribute(Node, "theData", thedata);
68  return 0;
69 }
70 
71 
72 int myTestClass::XMLIn(const xmlNodePtr Node, XMLIO& XMLObject) {
73  thedata = XMLObject.getAttributeValueInt(Node, "theData");
74  return 0;
75 }
76 /** \endcond */
77 // ########################## a small example program #######################
78 
79 int main (int /*argc*/, char ** /*argv*/)
80 {
81  // output
82  myTestClass x, y, z;
83  x.thedata = 42;
84  x.XMLWrite("backupofx.xml");
85  string str;
86  x.XMLWriteToString(str);
87  cout << "xml in string:\n" << str << endl;
88  // input
89  y.XMLRead("backupofx.xml");
90  z.XMLReadFromString(str);
91  // check result
92  int result(0);
93  if (x.thedata == y.thedata && x.thedata == z.thedata)
94  result=0;
95  else
96  result=-1;
97 
98  cout
99  <<"x.thedata="<<x.thedata<<endl
100  <<"y.thedata="<<y.thedata <<endl
101  <<"z.thedata="<<z.thedata <<endl
102  <<"result="<<result<<endl;
103  return result;
104 }
void addAttribute(const xmlNodePtr Node, const std::string &AttributeName, bool AttributeValue)
Add an attribute to a node.
Definition: XMLIO.cpp:156
int getAttributeValueInt(const xmlAttrPtr Attribute) const
Definition: XMLIO.cpp:728
Wrapper class for reading and writing XML files based on the XML library libxml2. ...
Definition: XMLIO.hh:72
Base class with interface for xml output.
Definition: XMLBase.hh:56