MDAL
mdal_xml.hpp
1 /*
2  MDAL - Mesh Data Abstraction Library (MIT License)
3  Copyright (C) 2019 Peter Petrik (zilolv at gmail dot com)
4 */
5 
6 #ifndef MDAL_XML_HPP
7 #define MDAL_XML_HPP
8 
9 #include <string>
10 #include <vector>
11 #include <stdio.h>
12 #include <libxml/parser.h>
13 #include <libxml/tree.h>
14 
16 class XMLFile
17 {
18  public:
22  ~XMLFile();
23 
25  void openFile( const std::string &fileName );
26 
29  xmlNodePtr getCheckChild( xmlNodePtr parent, const std::string &name, bool force = true ) const;
30 
33  xmlNodePtr getCheckSibling( xmlNodePtr parent, const std::string &name, bool force = true ) const;
34 
36  xmlNodePtr getCheckRoot( const std::string &name ) const;
37 
39  bool checkAttribute( xmlNodePtr parent, const std::string &name, const std::string &expectedVal ) const;
40 
42  void checkAttribute( xmlNodePtr parent, const std::string &name, const std::string &expectedVal, const std::string &err ) const;
43 
45  void checkEqual( const xmlChar *xmlString, const std::string &str, const std::string &err ) const;
46 
48  bool checkEqual( const xmlChar *xmlString, const std::string &str ) const;
49 
51  std::string attribute( xmlNodePtr node, std::string name ) const;
52 
54  double queryDoubleAttribute( xmlNodePtr elem, std::string name ) const;
55 
57  size_t querySizeTAttribute( xmlNodePtr elem, std::string name ) const;
58 
60  std::string content( xmlNodePtr node ) const;
61 
63  xmlNodePtr root() const;
64 
66  std::string toString( const xmlChar *xmlString ) const;
67 
68  private:
70  [[ noreturn ]] void error( const std::string &str ) const;
71 
72  xmlDocPtr mXmlDoc;
73  std::string mFileName;
74 };
75 
76 #endif // MDAL_XML_HPP
C++ Wrapper around libxml2 library.
Definition: mdal_xml.hpp:17
~XMLFile()
Closes the file.
Definition: mdal_xml.cpp:40
XMLFile()
Create file with invalid handle.
std::string attribute(xmlNodePtr node, std::string name) const
Gets string attribute.
Definition: mdal_xml.cpp:188
xmlNodePtr getCheckSibling(xmlNodePtr parent, const std::string &name, bool force=true) const
Gets next sibling with a correct name if force, throws an exception if not found.
Definition: mdal_xml.cpp:108
size_t querySizeTAttribute(xmlNodePtr elem, std::string name) const
Gets size_t attribute.
Definition: mdal_xml.cpp:181
std::string content(xmlNodePtr node) const
Gets element text.
Definition: mdal_xml.cpp:203
double queryDoubleAttribute(xmlNodePtr elem, std::string name) const
Gets double attribute.
Definition: mdal_xml.cpp:174
std::string toString(const xmlChar *xmlString) const
Converts xmlString to string.
Definition: mdal_xml.cpp:164
xmlNodePtr getCheckChild(xmlNodePtr parent, const std::string &name, bool force=true) const
Gets next child with a correct name if force, throws an exception if not found.
Definition: mdal_xml.cpp:88
bool checkAttribute(xmlNodePtr parent, const std::string &name, const std::string &expectedVal) const
Checks if attribute matches the string.
Definition: mdal_xml.cpp:128
void openFile(const std::string &fileName)
Opens the XML file.
Definition: mdal_xml.cpp:54
xmlNodePtr root() const
Gets root element.
Definition: mdal_xml.cpp:153
void checkEqual(const xmlChar *xmlString, const std::string &str, const std::string &err) const
Checks strings are equal. Throws exception.
Definition: mdal_xml.cpp:64
xmlNodePtr getCheckRoot(const std::string &name) const
Gets root element and check that has correct name.
Definition: mdal_xml.cpp:81