Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleXMLIO.cpp

Example XMLIO operations

Author
MIP
/*
This file is part of the BIAS library (Basic ImageAlgorithmS).
Copyright (C) 2003-2009 (see file CONTACT for details)
Multimediale Systeme der Informationsverarbeitung
Institut fuer Informatik
Christian-Albrechts-Universitaet Kiel
BIAS is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
BIAS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with BIAS; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/**
@example ExampleXMLIO.cpp
@relates XMLIO
@brief Example XMLIO operations
@ingroup g_examples
@author MIP
*/
#include <Base/Common/XMLIO.hh>
using namespace BIAS;
using namespace std;
int main (int /*argc*/, char** /*argv*/)
{
// *****************************************
// Write a new XML-File
// *****************************************
// remember some node pointers
xmlNodePtr rootNode;
xmlNodePtr currentNode2;
xmlNodePtr currentNode3;
// create an instance of XMLIO and initialize it with a new XML-File
XMLIO myXML;
// root node is named "XMLTest"
rootNode = myXML.create("XMLTest");
// set some nodes and attributes
currentNode2 = myXML.addChildNode(rootNode, "Child1");
myXML.addAttribute(currentNode2, "Attrib1", std::string("1234") );
myXML.addAttribute(currentNode2, "Attribute2", std::string("4321.1234") );
myXML.addAttribute(currentNode2, "NextAttribute", std::string("Content of attribute 3") );
myXML.addContent(currentNode2,
std::string("The content of a node is written between the opening and closing tag.") );
currentNode3 = myXML.addChildNode(currentNode2, "ChildOfChild1");
myXML.addAttribute(currentNode3, "AttributeOfChildOfChild1", std::string("FULL") );
myXML.addAttribute(currentNode3, "AttributeWithUmlaute", std::string("özil") );
myXML.addContent(currentNode3, "myContent wtith umlaute. Schöner Mist");
currentNode2 = myXML.addChildNode(rootNode, "Child2");
// results in an Error: Blanks or special chars not allowed in node
// and attribute names
myXML.addAttribute(currentNode2, "my Attribute", std::string("will produce an error") );
currentNode2 = myXML.addChildNode(rootNode, "Child 3");
currentNode2 = myXML.addChildNode(rootNode, "Child'3");
// OK again...
currentNode2 = myXML.addChildNode(rootNode, "ThisIsChild3");
myXML.addAttribute(currentNode2, "Roundness", std::string("Child 3 is very round") );
myXML.addContent(currentNode2, "Just some content");
// write the whole predefined structure
if (myXML.write("test.xml") <0) exit(1);
// ********************************************************************
// now clear the whole thing and read it again
xmlAttrPtr currAttr;
myXML.clear();
rootNode = myXML.read("test.xml");
if (rootNode == NULL) {
cout << "PARSING ERROR! Exiting XML-sample program...." << endl;
return 0;
}
// Process the complete tree using loops
currentNode2 = myXML.getFirstChild(rootNode);
while (currentNode2!=NULL) {
cout << endl << myXML.getNodeName(currentNode2) << endl;
cout << " Cont.: '" << myXML.getNodeContentString(currentNode2) << "'" << endl;
currAttr = myXML.getFirstAttribute(currentNode2);
while (currAttr!=NULL) {
cout << " Attr.: " << myXML.getAttributeName(currAttr) <<
" = '" << myXML.getAttributeValueString(currAttr) << "'" << endl;
currAttr = myXML.getNextAttribute();
}
// only depth 2 -> too lazy for recursion
currentNode3 = myXML.getFirstChild(currentNode2);
while (currentNode3!=NULL) {
cout << endl << " Child: " << myXML.getNodeName(currentNode3) << endl;
string content = myXML.getNodeContentString(currentNode3);
cout << " Child Cont.: '" << content << endl;
currAttr = myXML.getFirstAttribute(currentNode3);
while (currAttr!=NULL) {
cout << " Child Attr.: " << myXML.getAttributeName(currAttr) <<
" = '" << myXML.getAttributeValueString(currAttr) << "'" << endl;
currAttr = myXML.getNextAttribute();
}
currentNode3 = myXML.getNextChild();
}
currentNode2 = myXML.getNextChild(currentNode2);
}
// distinct access to specific Nodes and Attributes
cout << endl << endl << "Distinct Access:" << endl;
currentNode2 = myXML.getChild(rootNode, "ThisIsChild3");
if (currentNode2!=NULL) {
cout << "'Roundness' of 'ThisIsChild3': " <<
myXML.getAttributeValueString(currentNode2, "Roundness") << endl;
}
currentNode2 = myXML.getChild(rootNode, "Child1");
if (currentNode2!=NULL) {
cout << "'Attrib1' of 'Child1': " <<
myXML.getAttributeValueInt(currentNode2, "Attrib1") << endl;
cout << "'Attribute2' of 'Child1': " <<
myXML.getAttributeValueDouble(currentNode2, "Attribute2") << endl;
}
return 0;
}