iDynTree::XMLParser class

XML Parser class.

Use this class to parse XML files. It currently supports the following features

  • XSD validation
  • Extensibility (by providing a different root object for the XML hierarchy.

Common usage

// Allocate parser
std::shared_ptr<XMLParser> parser = std::make_shared<XMLParser>();
// If needed, you can customize the class representing the XML Document
// just set a function returning a subclass of XMLElement
parser->setDocumentFactory([]{ return std::shared_ptr<XMLDocument>(new MyCustomXMLDocument); });
// Parse
parser->parseXMLFile(filename);
// You can retrieve the Document after the parsing
std::shared_ptr<const XMLDocument> document = parser->document();

Description

Validation support

The parser supports XSD schema validation. This options is controlled by two methods:

  • setValidateXML accepts a boolean, to enable the validation
  • setSchemaLocation specifies the location of the XSD schema.

If both variables are set, the validation will take place before parsing. Note that currently the errors are output to standard error (or output?) and not handled directly in the code. It might be possible to handle those in code though (feature request).

Todos:

Possible feature requests:

  • parse entities out of context (xmlSAXParseEntity)
  • Validation for in memory parsing
  • Use libxml in memory tree (that can be modified and dumped to file) instead of custom tree

Constructors, destructors, conversion operators

XMLParser()
Default constructor.
~XMLParser()
Destructor.

Public functions

auto keepTreeInMemory() const -> bool
Returns true if the parsed tree is ketp in memory.
void setKeepTreeInMemory(bool keepTreeInMemory)
Set if the parse tree should be kept in memory.
void setPackageDirs(const std::vector<std::string>& packageDirs)
auto packageDirs() const -> const std::vector<std::string>&
auto logParsing() const -> bool
Returns true if the parser logs the parsing to standard output.
void setLogParsing(bool enableLogging)
Set the logging option of the parser.
auto validateXML() const -> bool
Returns true if the validation option is enabled.
void setValidateXML(bool validate)
Set the option to perform Schema validation.
auto schemaLocation() const -> std::string
Returns the current schema location used for validation.
void setSchemaLocation(std::string schemaLocation)
Sets the XSD schema location.
auto parseXMLFile(std::string absoluteFileName) -> bool
Parse the specified XML file.
auto parseXMLString(std::string xmlString) -> bool
Parse the specified XML document string.
void setDocumentFactory(std::function<std::shared_ptr<XMLDocument>(XMLParserState&)> factory)
Set the factory function responsible of creating a new XMLDocument element.
auto document() const -> std::shared_ptr<const XMLDocument>
Returns the current parsed document.

Function documentation

bool iDynTree::XMLParser::keepTreeInMemory() const

Returns true if the parsed tree is ketp in memory.

Returns true if the parsed tree is kept on memory.

void iDynTree::XMLParser::setKeepTreeInMemory(bool keepTreeInMemory)

Set if the parse tree should be kept in memory.

Parameters
keepTreeInMemory true if the tree should be kept in memory.

bool iDynTree::XMLParser::logParsing() const

Returns true if the parser logs the parsing to standard output.

Returns true if log to standard output is enabled.

void iDynTree::XMLParser::setLogParsing(bool enableLogging)

Set the logging option of the parser.

Parameters
enableLogging true if the parser should log to standard output.

see logParsing

bool iDynTree::XMLParser::validateXML() const

Returns true if the validation option is enabled.

Returns true if the XML should be validated against a schema.

void iDynTree::XMLParser::setValidateXML(bool validate)

Set the option to perform Schema validation.

Parameters
validate true if the XML should be validated against a XSD.

std::string iDynTree::XMLParser::schemaLocation() const

Returns the current schema location used for validation.

Returns the current schema location.

void iDynTree::XMLParser::setSchemaLocation(std::string schemaLocation)

Sets the XSD schema location.

Parameters
schemaLocation the new XSD schema location.

bool iDynTree::XMLParser::parseXMLFile(std::string absoluteFileName)

Parse the specified XML file.

Parameters
absoluteFileName the XML file to be parsed.
Returns true if the document is valid and successfully parsed.

If the validation option is enabled, the XML file will be also validated against the specified XSD schema.

bool iDynTree::XMLParser::parseXMLString(std::string xmlString)

Parse the specified XML document string.

Parameters
xmlString string containing a valid XML content.
Returns true if the XML document is valid and successfully parsed.

void iDynTree::XMLParser::setDocumentFactory(std::function<std::shared_ptr<XMLDocument>(XMLParserState&)> factory)

Set the factory function responsible of creating a new XMLDocument element.

Parameters
factory the function that will be called for instantiating a new XMLDocument object.

By specifying a new factory function, it is possible to change how the XML document will be represented in memory. The signature of the function is (XMLParserState&) -> std::shared_ptr<XMLDocument>, i.e. a function accepting a reference to the parser state and returning a std::shared_ptr to an XMLDocument object.

std::shared_ptr<const XMLDocument> iDynTree::XMLParser::document() const

Returns the current parsed document.

Returns the parsed document.