bpmn++
A BPMN parser library, written in C++
XML::XMLObject Class Reference

A class representing a node in an XML-tree. More...

Detailed Description

The XMLObject class allows to read and store an XML-tree. The root element can be created using

Each object has the following members:

  • xmlns : refers to the XML namespace
  • className : refers to the class it belong to
  • elementName : refers to the name used in the XML
  • prefix : refers to the namespace prefix in the XML
  • textContent : textual content of XML element without children
  • attributes : a list of attributes containing the namespace, prefix, attribute name, and attribute value
  • children : a list of child elements

Derived classes with dedicated members for attributes and children are automatically generated by schematic++ according to respective XML schema definition(s).

Each XMLObject can be converted to a string using stringify() and printed to an output stream using operator<<(std::ostream& os, const XMLObject* obj) and operator<<(std::ostream& os, const XMLObject& obj) .

Definition at line 115 of file XMLObject.h.

#include <XMLObject.h>

Inheritance diagram for XML::XMLObject:

Public Member Functions

virtual ~XMLObject ()
 
template<typename T >
T * is ()
 Returns a pointer of type T of the object. More...
 
template<typename T >
const T * is () const
 
template<typename T >
T * get ()
 Attempt to cast the current instance to the specified type T. More...
 
template<typename T >
const T * get () const
 Attempt to cast the current instance to the specified type T. More...
 
template<typename T >
std::vector< std::reference_wrapper< T > > find ()
 Find all descendants of type T. More...
 
template<typename T >
std::vector< std::reference_wrapper< const T > > find () const
 Find all descendants of type T. More...
 
std::string stringify () const
 Convert the XMLObject and its children to a string representation. More...
 
std::string format (std::string indentation="\t", unsigned int depth=0) const
 Creates formated string representing the XMLObject including its children. More...
 
template<typename T >
T & getRequiredChild ()
 Get a required child of type T. More...
 
template<typename T >
std::optional< std::reference_wrapper< T > > getOptionalChild ()
 Get an optional child of type T. More...
 
template<typename T >
std::vector< std::reference_wrapper< T > > getChildren ()
 Get all children of type T. More...
 
XMLObjectgetRequiredChildByName (const ElementName &elementName)
 Get a required child with the specified element name. More...
 
std::optional< std::reference_wrapper< XMLObject > > getOptionalChildByName (const ElementName &elementName)
 Get the optional child with the specified element name. More...
 
std::vector< std::reference_wrapper< XMLObject > > getChildrenByName (const ElementName &elementName)
 Get all children with the specified element name. More...
 
AttributegetRequiredAttributeByName (const AttributeName &attributeName)
 Get a required attribute with the specified attribute name. More...
 
std::optional< std::reference_wrapper< Attribute > > getOptionalAttributeByName (const AttributeName &attributeName)
 Get an optional attribute with the specified attribute name. More...
 

Static Public Member Functions

static XMLObjectcreateFromStream (std::istream &xmlStream)
 Create an XMLObject from the input stream. More...
 
static XMLObjectcreateFromString (const std::string &xmlString)
 Create an XMLObject from a string representation of XML. More...
 
static XMLObjectcreateFromFile (const std::string &filename)
 Create an XMLObject from an XML file. More...
 

Public Attributes

Namespace xmlns
 
const ClassName className
 
Namespace prefix
 
ElementName elementName
 
TextContent textContent
 Textual content of XML element without children. More...
 
Children children
 Child nodes of the XML element. More...
 
Attributes attributes
 

Static Public Attributes

static const Attributes defaults = {}
 Attributes of the XML element. More...
 

Protected Member Functions

 XMLObject (const Namespace &xmlns, const ClassName &className, const xercesc::DOMElement *element, const Attributes &defaultAttributes)
 

Static Protected Member Functions

static XMLObjectcreateObject (const xercesc::DOMElement *element)
 

Static Protected Attributes

static Factory factory
 

Friends

template<typename T >
XMLObjectcreateInstance (const Namespace &xmlns, const ClassName &className, const xercesc::DOMElement *element)
 Template function used to store in factory. More...
 

Constructor & Destructor Documentation

◆ ~XMLObject()

virtual XML::XMLObject::~XMLObject ( )
inlinevirtual

Definition at line 145 of file XMLObject.h.

◆ XMLObject()

XML::XMLObject::XMLObject ( const Namespace xmlns,
const ClassName className,
const xercesc::DOMElement *  element,
const Attributes defaultAttributes 
)
protected

Definition at line 123 of file XMLObject.cpp.

Member Function Documentation

◆ createFromFile()

XMLObject * XML::XMLObject::createFromFile ( const std::string &  filename)
static
Parameters
filenameThe path to the XML file.
Returns
A pointer to the created XMLObject.
Exceptions
std::runtime_errorif loading the file or parsing the XML fails.

Definition at line 80 of file XMLObject.cpp.

◆ createFromStream()

XMLObject * XML::XMLObject::createFromStream ( std::istream &  xmlStream)
static
Parameters
xmlStreamThe input stream containing the XML data.
Returns
A pointer to the created XMLObject.
Exceptions
std::runtime_errorif parsing the XML fails.

Definition at line 46 of file XMLObject.cpp.

◆ createFromString()

XMLObject * XML::XMLObject::createFromString ( const std::string &  xmlString)
static
Parameters
xmlStringThe string containing the XML data.
Returns
A pointer to the created XMLObject.
Exceptions
std::runtime_errorif parsing the XML fails.

Definition at line 74 of file XMLObject.cpp.

◆ createObject()

XMLObject * XML::XMLObject::createObject ( const xercesc::DOMElement *  element)
staticprotected

Definition at line 113 of file XMLObject.cpp.

◆ find() [1/2]

template<typename T >
std::vector<std::reference_wrapper<T> > XML::XMLObject::find ( )
inline
Returns
A vector of references to descendants of type T.

Definition at line 222 of file XMLObject.h.

◆ find() [2/2]

template<typename T >
std::vector<std::reference_wrapper<const T> > XML::XMLObject::find ( ) const
inline
Returns
A vector of const references to descendants of type T.

Definition at line 235 of file XMLObject.h.

◆ format()

std::string XML::XMLObject::format ( std::string  indentation = "\t",
unsigned int  depth = 0 
) const
Returns
A formated string representing the XMLObject.

Definition at line 222 of file XMLObject.cpp.

◆ get() [1/2]

template<typename T >
T* XML::XMLObject::get ( )
inline
Returns
A pointer to the casted object.
Exceptions
std::runtime_errorif the cast fails.

Definition at line 171 of file XMLObject.h.

◆ get() [2/2]

template<typename T >
const T* XML::XMLObject::get ( ) const
inline
Returns
A pointer to the casted object.
Exceptions
std::runtime_errorif the cast fails.

Definition at line 184 of file XMLObject.h.

◆ getChildren()

template<typename T >
std::vector< std::reference_wrapper<T> > XML::XMLObject::getChildren ( )
inline
Returns
A vector of references to the children of type T.

Definition at line 301 of file XMLObject.h.

◆ getChildrenByName()

std::vector< std::reference_wrapper< XMLObject > > XML::XMLObject::getChildrenByName ( const ElementName elementName)
Parameters
elementNameThe name of the child elements without namespace prefix.
Returns
A vector of references to the children with the specified element name.

Definition at line 177 of file XMLObject.cpp.

◆ getOptionalAttributeByName()

std::optional< std::reference_wrapper< Attribute > > XML::XMLObject::getOptionalAttributeByName ( const AttributeName attributeName)
Parameters
attributeNameThe name of the attribute without namespace prefix.
Returns
An optional containing a reference to the optional attribute if found, or std::nullopt if the optional attribute is not found.

Definition at line 197 of file XMLObject.cpp.

◆ getOptionalChild()

template<typename T >
std::optional< std::reference_wrapper<T> > XML::XMLObject::getOptionalChild ( )
inline
Returns
An optional containing a reference to the optional child if found, or std::nullopt if the optional child is not found.

Definition at line 287 of file XMLObject.h.

◆ getOptionalChildByName()

std::optional< std::reference_wrapper< XMLObject > > XML::XMLObject::getOptionalChildByName ( const ElementName elementName)
Parameters
elementNameThe name of the child element without namespace prefix.
Returns
An optional containing a reference to the optional child if found, or std::nullopt if the optional child is not found.

Definition at line 168 of file XMLObject.cpp.

◆ getRequiredAttributeByName()

Attribute & XML::XMLObject::getRequiredAttributeByName ( const AttributeName attributeName)
Parameters
attributeNameThe name of the attribute without namespace prefix.
Returns
A reference to the required attribute.
Exceptions
std::runtime_errorif the required attribute is not found.

Definition at line 187 of file XMLObject.cpp.

◆ getRequiredChild()

template<typename T >
T& XML::XMLObject::getRequiredChild ( )
inline
Returns
A reference to the required child.
Exceptions
std::runtime_errorif the required child is not found.

Definition at line 272 of file XMLObject.h.

◆ getRequiredChildByName()

XMLObject & XML::XMLObject::getRequiredChildByName ( const ElementName elementName)
Parameters
elementNameThe name of the child element without namespace prefix.
Returns
A reference to the required child.
Exceptions
std::runtime_errorif the required child is not found.

Definition at line 159 of file XMLObject.cpp.

◆ is() [1/2]

template<typename T >
T* XML::XMLObject::is ( )
inline

Definition at line 158 of file XMLObject.h.

◆ is() [2/2]

template<typename T >
const T* XML::XMLObject::is ( ) const
inline

Definition at line 162 of file XMLObject.h.

◆ stringify()

std::string XML::XMLObject::stringify ( ) const
Returns
The string representation of the XMLObject.

Definition at line 207 of file XMLObject.cpp.

Friends And Related Function Documentation

◆ createInstance

template<typename T >
XMLObject* createInstance ( const Namespace xmlns,
const ClassName className,
const xercesc::DOMElement *  element 
)
friend

Definition at line 84 of file XMLObject.h.

Member Data Documentation

◆ attributes

Attributes XML::XMLObject::attributes

Definition at line 249 of file XMLObject.h.

◆ children

Children XML::XMLObject::children

Definition at line 248 of file XMLObject.h.

◆ className

const ClassName XML::XMLObject::className

Definition at line 243 of file XMLObject.h.

◆ defaults

const Attributes XML::XMLObject::defaults = {}
inlinestatic

Definition at line 250 of file XMLObject.h.

◆ elementName

ElementName XML::XMLObject::elementName

Definition at line 245 of file XMLObject.h.

◆ factory

Factory XML::XMLObject::factory
inlinestaticprotected

Definition at line 155 of file XMLObject.h.

◆ prefix

Namespace XML::XMLObject::prefix

Definition at line 244 of file XMLObject.h.

◆ textContent

TextContent XML::XMLObject::textContent

Definition at line 247 of file XMLObject.h.

◆ xmlns

Namespace XML::XMLObject::xmlns

Definition at line 242 of file XMLObject.h.


The documentation for this class was generated from the following files: