Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Public Member Functions | Private Attributes | List of all members
XMLDocument Class Reference

Small XML Document wrapper class. More...

#include <ored/utilities/xmlutils.hpp>

+ Collaboration diagram for XMLDocument:

Public Member Functions

 XMLDocument ()
 create an empty doc. More...
 
 XMLDocument (const string &filename)
 load an xml doc from the given file More...
 
 ~XMLDocument ()
 destructor More...
 
void fromXMLString (const string &xmlString)
 load a document from a hard-coded string More...
 
void toFile (const string &filename) const
 save the XML Document to the given file. More...
 
std::string toString () const
 return the XML Document as a string. More...
 
XMLNodegetFirstNode (const string &name) const
 
void appendNode (XMLNode *)
 
XMLNodeallocNode (const string &nodeName)
 util functions that wrap rapidxml More...
 
XMLNodeallocNode (const string &nodeName, const string &nodeValue)
 
char * allocString (const string &str)
 
rapidxml::xml_document< char > * doc ()
 

Private Attributes

rapidxml::xml_document< char > * _doc
 
char * _buffer
 

Detailed Description

Small XML Document wrapper class.

Definition at line 65 of file xmlutils.hpp.

Constructor & Destructor Documentation

◆ XMLDocument() [1/2]

create an empty doc.

Definition at line 75 of file xmlutils.cpp.

rapidxml::xml_document< char > * _doc
Definition: xmlutils.hpp:94
XML Document.
Definition: xmlutils.hpp:47

◆ XMLDocument() [2/2]

XMLDocument ( const string &  filename)

load an xml doc from the given file

Definition at line 77 of file xmlutils.cpp.

78 // Need to load the entire file into memory to pass to doc.parse().
79 ifstream t(fileName.c_str());
80 QL_REQUIRE(t.is_open(), "Failed to open file " << fileName);
81 t.seekg(0, std::ios::end); // go to the end
82 Size length = static_cast<Size>(t.tellg()); // report location (this is the length)
83 QL_REQUIRE(length > 0, "File " << fileName << " is empty.");
84 t.seekg(0, std::ios::beg); // go back to the beginning
85 _buffer = new char[length + 1]; // allocate memory for a buffer of appropriate dimension
86 t.read(_buffer, length); // read the whole file into the buffer
87 _buffer[static_cast<int>(t.gcount())] = '\0';
88 t.close(); // close file handle
89 try {
90 _doc->parse<0>(_buffer);
91 } catch (const rapidxml::parse_error& pe) {
92 handle_rapidxml_parse_error(pe);
93 }
94}

◆ ~XMLDocument()

destructor

Definition at line 96 of file xmlutils.cpp.

96 {
97 if (_buffer != NULL)
98 delete[] _buffer;
99 if (_doc != NULL)
100 delete _doc;
101}

Member Function Documentation

◆ fromXMLString()

void fromXMLString ( const string &  xmlString)

load a document from a hard-coded string

Definition at line 103 of file xmlutils.cpp.

103 {
104 QL_REQUIRE(!_buffer, "XML Document is already loaded");
105 Size length = xmlString.size();
106 _buffer = new char[length + 1];
107 strcpy(_buffer, xmlString.c_str());
108 _buffer[length] = '\0';
109 try {
110 _doc->parse<0>(_buffer);
111 } catch (const rapidxml::parse_error& pe) {
112 handle_rapidxml_parse_error(pe);
113 }
114}
+ Here is the caller graph for this function:

◆ toFile()

void toFile ( const string &  filename) const

save the XML Document to the given file.

Definition at line 120 of file xmlutils.cpp.

120 {
121 std::ofstream ofs(fileName.c_str());
122 ofs << *_doc;
123 ofs.close();
124}
+ Here is the caller graph for this function:

◆ toString()

string toString ( ) const

return the XML Document as a string.

Definition at line 126 of file xmlutils.cpp.

126 {
127 ostringstream oss;
128 oss << *_doc;
129 return oss.str();
130}
+ Here is the caller graph for this function:

◆ getFirstNode()

XMLNode * getFirstNode ( const string &  name) const

Definition at line 116 of file xmlutils.cpp.

116{ return _doc->first_node(name == "" ? NULL : name.c_str()); }
string name
+ Here is the caller graph for this function:

◆ appendNode()

void appendNode ( XMLNode node)

Definition at line 118 of file xmlutils.cpp.

118{ _doc->append_node(node); }
+ Here is the caller graph for this function:

◆ allocNode() [1/2]

XMLNode * allocNode ( const string &  nodeName)

util functions that wrap rapidxml

Definition at line 132 of file xmlutils.cpp.

132 {
133 XMLNode* n = _doc->allocate_node(node_element, allocString(nodeName));
134 QL_REQUIRE(n, "Failed to allocate XMLNode for " << nodeName);
135 return n;
136}
char * allocString(const string &str)
Definition: xmlutils.cpp:144
rapidxml::xml_node< char > XMLNode
Definition: xmlutils.hpp:60
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ allocNode() [2/2]

XMLNode * allocNode ( const string &  nodeName,
const string &  nodeValue 
)

Definition at line 138 of file xmlutils.cpp.

138 {
139 XMLNode* n = _doc->allocate_node(node_element, allocString(nodeName), allocString(nodeValue));
140 QL_REQUIRE(n, "Failed to allocate XMLNode for " << nodeName);
141 return n;
142}
+ Here is the call graph for this function:

◆ allocString()

char * allocString ( const string &  str)

Definition at line 144 of file xmlutils.cpp.

144 {
145 char* s = _doc->allocate_string(str.c_str());
146 QL_REQUIRE(s, "Failed to allocate string for " << str);
147 return s;
148}
+ Here is the caller graph for this function:

◆ doc()

rapidxml::xml_document< char > * doc ( )

Definition at line 91 of file xmlutils.hpp.

91{ return _doc; }
+ Here is the caller graph for this function:

Member Data Documentation

◆ _doc

rapidxml::xml_document<char>* _doc
private

Definition at line 94 of file xmlutils.hpp.

◆ _buffer

char* _buffer
private

Definition at line 95 of file xmlutils.hpp.