Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleXMLIO.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  @example ExampleXMLIO.cpp
27  @relates XMLIO
28  @brief Example XMLIO operations
29  @ingroup g_examples
30  @author MIP
31 */
32 #include <Base/Common/XMLIO.hh>
33 
34 using namespace BIAS;
35 using namespace std;
36 
37 int main (int /*argc*/, char** /*argv*/)
38 {
39  // *****************************************
40  // Write a new XML-File
41  // *****************************************
42 
43  // remember some node pointers
44  xmlNodePtr rootNode;
45  xmlNodePtr currentNode2;
46  xmlNodePtr currentNode3;
47 
48  // create an instance of XMLIO and initialize it with a new XML-File
49  XMLIO myXML;
50 
51  // root node is named "XMLTest"
52  rootNode = myXML.create("XMLTest");
53 
54  // set some nodes and attributes
55  currentNode2 = myXML.addChildNode(rootNode, "Child1");
56 
57  myXML.addAttribute(currentNode2, "Attrib1", std::string("1234") );
58  myXML.addAttribute(currentNode2, "Attribute2", std::string("4321.1234") );
59  myXML.addAttribute(currentNode2, "NextAttribute", std::string("Content of attribute 3") );
60  myXML.addContent(currentNode2,
61  std::string("The content of a node is written between the opening and closing tag.") );
62 
63  currentNode3 = myXML.addChildNode(currentNode2, "ChildOfChild1");
64  myXML.addAttribute(currentNode3, "AttributeOfChildOfChild1", std::string("FULL") );
65  myXML.addAttribute(currentNode3, "AttributeWithUmlaute", std::string("özil") );
66 
67 
68 
69  myXML.addContent(currentNode3, "myContent wtith umlaute. Schöner Mist");
70 
71  currentNode2 = myXML.addChildNode(rootNode, "Child2");
72 
73  // results in an Error: Blanks or special chars not allowed in node
74  // and attribute names
75  myXML.addAttribute(currentNode2, "my Attribute", std::string("will produce an error") );
76  currentNode2 = myXML.addChildNode(rootNode, "Child 3");
77  currentNode2 = myXML.addChildNode(rootNode, "Child'3");
78 
79  // OK again...
80  currentNode2 = myXML.addChildNode(rootNode, "ThisIsChild3");
81  myXML.addAttribute(currentNode2, "Roundness", std::string("Child 3 is very round") );
82  myXML.addContent(currentNode2, "Just some content");
83 
84  // write the whole predefined structure
85  if (myXML.write("test.xml") <0) exit(1);
86 
87  // ********************************************************************
88  // now clear the whole thing and read it again
89 
90  xmlAttrPtr currAttr;
91 
92  myXML.clear();
93 
94  rootNode = myXML.read("test.xml");
95  if (rootNode == NULL) {
96  cout << "PARSING ERROR! Exiting XML-sample program...." << endl;
97  return 0;
98  }
99 
100  // Process the complete tree using loops
101  currentNode2 = myXML.getFirstChild(rootNode);
102 
103  while (currentNode2!=NULL) {
104 
105  cout << endl << myXML.getNodeName(currentNode2) << endl;
106  cout << " Cont.: '" << myXML.getNodeContentString(currentNode2) << "'" << endl;
107 
108  currAttr = myXML.getFirstAttribute(currentNode2);
109  while (currAttr!=NULL) {
110  cout << " Attr.: " << myXML.getAttributeName(currAttr) <<
111  " = '" << myXML.getAttributeValueString(currAttr) << "'" << endl;
112  currAttr = myXML.getNextAttribute();
113  }
114 
115  // only depth 2 -> too lazy for recursion
116  currentNode3 = myXML.getFirstChild(currentNode2);
117 
118  while (currentNode3!=NULL) {
119  cout << endl << " Child: " << myXML.getNodeName(currentNode3) << endl;
120 
121  string content = myXML.getNodeContentString(currentNode3);
122  cout << " Child Cont.: '" << content << endl;
123 
124 
125  currAttr = myXML.getFirstAttribute(currentNode3);
126  while (currAttr!=NULL) {
127  cout << " Child Attr.: " << myXML.getAttributeName(currAttr) <<
128  " = '" << myXML.getAttributeValueString(currAttr) << "'" << endl;
129  currAttr = myXML.getNextAttribute();
130  }
131  currentNode3 = myXML.getNextChild();
132  }
133 
134  currentNode2 = myXML.getNextChild(currentNode2);
135  }
136 
137  // distinct access to specific Nodes and Attributes
138  cout << endl << endl << "Distinct Access:" << endl;
139 
140  currentNode2 = myXML.getChild(rootNode, "ThisIsChild3");
141  if (currentNode2!=NULL) {
142  cout << "'Roundness' of 'ThisIsChild3': " <<
143  myXML.getAttributeValueString(currentNode2, "Roundness") << endl;
144  }
145 
146  currentNode2 = myXML.getChild(rootNode, "Child1");
147  if (currentNode2!=NULL) {
148  cout << "'Attrib1' of 'Child1': " <<
149  myXML.getAttributeValueInt(currentNode2, "Attrib1") << endl;
150  cout << "'Attribute2' of 'Child1': " <<
151  myXML.getAttributeValueDouble(currentNode2, "Attribute2") << endl;
152  }
153 
154  return 0;
155 
156 }
void addAttribute(const xmlNodePtr Node, const std::string &AttributeName, bool AttributeValue)
Add an attribute to a node.
Definition: XMLIO.cpp:156
std::string getAttributeName(const xmlAttrPtr Attribute) const
Get the name of a given Attribute.
Definition: XMLIO.cpp:690
xmlNodePtr getNextChild()
Get the next child of the parent specified in the last getFirstChild() call, the class remembers the ...
Definition: XMLIO.cpp:466
int write(const std::string &Filename, bool AutoAddCompressionSuffix=true) const
Write the whole tree that was constructed in memory to disk.
Definition: XMLIO.cpp:379
xmlNodePtr read(const std::string &Filename)
Read and parse an XML file from disk, DtD validation is not yet implemented.
Definition: XMLIO.cpp:416
xmlNodePtr getChild(const xmlNodePtr ParentNode, const std::string &ChildName)
Get a child of a Parent node by specifying the childs name, NULL is returned if the ParentNode has no...
Definition: XMLIO.cpp:489
xmlNodePtr create(const std::string &RootNodeName)
Create the base of a new XML-Tree in memory, already with a one and only root node.
Definition: XMLIO.cpp:88
xmlAttrPtr getFirstAttribute(const xmlNodePtr Node)
Get the first attribute of a given parent, or NULL for no attributes.
Definition: XMLIO.cpp:624
std::string getAttributeValueString(const xmlAttrPtr Attribute) const
Definition: XMLIO.cpp:716
int getAttributeValueInt(const xmlAttrPtr Attribute) const
Definition: XMLIO.cpp:728
void addContent(const xmlNodePtr Node, const std::string &Content)
Add content to a node.
Definition: XMLIO.cpp:254
Wrapper class for reading and writing XML files based on the XML library libxml2. ...
Definition: XMLIO.hh:72
xmlNodePtr addChildNode(const xmlNodePtr ParentNode, const std::string &NewNodeName)
Add a child node to an incoming node with the given name.
Definition: XMLIO.cpp:131
xmlNodePtr getFirstChild(const xmlNodePtr ParentNode)
Get the first child of a given parent, or NULL for no childs.
Definition: XMLIO.cpp:452
xmlAttrPtr getNextAttribute()
Get the next attribute of the parent specified in the last getFirstAttribute() call, the class remembers the last returned attribute.
Definition: XMLIO.cpp:638
double getAttributeValueDouble(const xmlAttrPtr Attribute) const
Definition: XMLIO.cpp:736
void clear()
Definition: XMLIO.cpp:74
std::string getNodeName(const xmlNodePtr Node) const
Get the name of a given Node.
Definition: XMLIO.cpp:543
std::string getNodeContentString(const xmlNodePtr Node) const
Get the content of a given Node.
Definition: XMLIO.cpp:554