Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
parameterlist/test/XML/cxx_main.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #include "Teuchos_ConfigDefs.hpp"
11 #include "Teuchos_XMLObject.hpp"
15 #include "Teuchos_Version.hpp"
16 #include <fstream>
17 
18 using std::string;
19 using Teuchos::XMLObject;
22 
23 /* Test of Teuchos XML handling classes */
24 
25 int main(int argc, char** argv)
26 {
27  std::cout << Teuchos::Teuchos_Version() << std::endl << std::endl;
28 
29  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
30 
31  try
32  {
33  /* create an XML object */
34  XMLObject problem("Problem");
35  XMLObject solver("Solver");
36  XMLObject prec("Preconditioner");
37 
38  solver.addAttribute("type", "gmres");
39  solver.addInt("maxiters", 1000);
40  solver.addInt("restarts", 100);
41  solver.addDouble("tol", 1.0e-10);
42 
43  solver.addChild(prec);
44 
45  prec.addAttribute("type", "ILUk");
46  prec.addInt("k", 2);
47 
48  problem.addChild(solver);
49 
50  int foundIndex = problem.findFirstChild("Solver");
51  if(foundIndex == -1)
52  {
53  std::cerr << "Find child didn't find the child!"
54  <<std::endl << std::endl;
55  return -1;
56  }
57 
58  const XMLObject foundChild = problem.getChild(foundIndex);
59  if(foundChild.getTag() != solver.getTag())
60  {
61  std::cerr << "Find child found the wrong tag!" << std::endl <<
62  "Found index was: " << foundIndex << std::endl <<
63  std::endl << std::endl;
64  return -1;
65  }
66 
67  if(problem.findFirstChild("NON EXSISTENT CHILD") != -1){
68  std::cerr << "First first child didn't return -1 when it was "
69  "suppose to!" <<std::endl << std::endl;
70  return -1;
71  }
72 
73  std::string str = problem.toString();
74  std::cerr << str << std::endl;
75 
76  /* parse XML in a std::string */
77  StringInputSource src(str);
78  XMLObject reread = src.getObject();
79 
80  std::cerr << reread << std::endl;
81 
82  /* write to a file, and then read and parse the file */
83  std::ofstream of("tmp.xml");
84  of << reread << std::endl;
85 
86  FileInputSource fileSrc("tmp.xml");
87  XMLObject fileXML = fileSrc.getObject();
88 
89  std::cerr << fileXML << std::endl;
90 
91  return 0;
92  }
93  catch(std::exception& e)
94  {
95  std::cerr << e.what() << std::endl;
96  }
97 }
const std::string & getTag() const
Return the tag of the current node.
void addChild(const XMLObject &child)
Add a child node to the node.
Teuchos header file which uses auto-configuration information to include necessary C++ headers...
Initialize, finalize, and query the global MPI session.
std::string toString() const
Represent this node and its children as a std::string.
Representation of an XML data tree. XMLObject is a ref-counted handle to a XMLObjectImplem object...
XMLObject getObject() const
Get an object by invoking the TreeBuildingXMLHandler on the input data.
Instantiation of XMLInputSource class for reading XML from a file.
void addAttribute(const std::string &name, T value)
Lookup whether or not Doubles are allowed.
std::string Teuchos_Version()
int main(int argc, char *argv[])
A MPI utilities class, providing methods for initializing, finalizing, and querying the global MPI se...
Definition of XMLInputSource derived class for reading XML from a std::string.
void addInt(const std::string &name, int val)
Add an int as an attribute.
Definition of XMLInputSource derived class for reading XML from a file.
int findFirstChild(std::string tagName) const
Returns the index of the first child found with the given tag name. Returns -1 if no child is found...
const XMLObject & getChild(int i) const
Return the i-th child node.
Instantiation of XMLInputSource class for reading XML from a std::string.
void addDouble(const std::string &name, double val)
Add a double as an attribute.
An object representation of a subset of XML data.