Rapid XML is a very fast header only C++ XML reader which is used within the MMM framework.
Process XML string
#include "XMLTools.h"
#include "RapidXML/rapidxml.hpp"
void myXMLParseMethod(std::string &xmlString)
{
try
{
rapidxml::xml_document<char> doc;
char* y = doc.allocate_string(xmlString.c_str());
doc.parse<0>(y);
rapidxml::xml_node<>* node = doc.first_node();
while (node)
{
std::string nodeName = XML::getLowerCase(node->name());
if (nodeName == "myCustomTag")
processTag(node);
node = node->next_sibling();
}
}
catch (rapidxml::parse_error& e)
{
}
}
Access XML hierarchy and attributes
bool processTag(rapidxml::xml_node<char>* tag)
{
rapidxml::xml_attribute<> *attr = tag->first_attribute("type", 0, false);
if (attr)
{
float f = XML::convertToFloat(attr->value());
std::string s = attr->value();
}
rapidxml::xml_node<>* node = tag->first_node();
while (node)
{
std::string nodeName = XML::getLowerCase(node->name());
node = node->next_sibling();
}
}
RapidXmlHelper
To process motions in MMM Data Format we are using two helper classes for easier access to the API.
The simox::xml::RapidXMLWrapper is used to read the information from an XML document. Each XML node is type of simox::xml::RapidXMLWrapperNode. If a node or attribute is not included an exception is thrown.
Here is an example:
#include "RapidXML/simox::xml::RapidXMLWrapper.h"
void exampleXMLParseMethod(std::string &xmlString)
{
simox::xml::RapidXMLWrapperPtr reader = simox::xml::RapidXMLWrapper::FromXmlString(xmlString);
simox::xml::RapidXMLWrapperNodePtr node = reader->getRoot();
std::string nodeName = node->name();
if (node->name() == "ExampleRootNode" && node->has_attribute("type") && node->attribute_value("type") == "Example") {
for (simox::xml::RapidXMLWrapperNodePtr child : node->nodes("ExampleChildNode") {
std::string s = child->value();
std::cout << s;
}
}
}
int main() {
exampleXMLParseMethod(
"<?xml version='1.0' encoding='utf-8'?>"
"<ExampleRootNode type='Example'>"
" <ExampleChildNode>Some </ExampleChildNode>"
" <ExampleChildNode2>non interesting </ExampleChildNode2>"
" <ExampleChildNode>content</ExampleChildNode>"
"</ExampleRootNode>"
);
}
The simox::xml::RapidXMLWrapper is used to convert C++ objects to an XML document. Each XML node is type of simox::xml::RapidXMLWrapperNode.
Here is an example:
#include "RapidXML/simox::xml::RapidXMLWrapper.h"
int main() {
simox::xml::RapidXMLWrapperPtr writer(new simox::xml::RapidXMLWrapper());
simox::xml::RapidXMLWrapperNodePtr node = writer->createRootNode("ExampleRootNode");
node->append_attribute("type", "Example");
simox::xml::RapidXMLWrapperNodePtr childNode = node->append_node("ExampleChildNode");
childNode->append_data_node("Some content");
std::cout << writer->print(true);
std::cout << writer->print(false);
}