Provides XML data management services.
The XML class provides functionality to create and maintain XML data files. It is capable of parsing and validating XML files with or without correct structure, and can perform optional parsing behaviours such as stripping comments during processing.
Data can be loaded into an XML object either by specifying a file Path or by giving it an XML Statement. If multiple XML statements need to be processed then reset the Path or Statement field after initialisation and the XML object will rebuild itself. This saves on allocating multiple XML objects for batch processing.
Successfully processed data can be read back by scanning the array referenced in the Tags field. The array contains an XMLTag structure for each tag parsed from the original XML statement. For more information on how to scan this information, refer to the Tags field. C++ developers are recommended to interact with Tags directly, which is represented as pf::vector<XMLTag>
. Note that adding new Tags is a volatile action that can destabilise the object (taking a complete copy of the tags may be warranted instead).
The XML class consists of the following fields:
Access | Name | Type | Comment | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Flags | XMF | Optional flags. | |||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||
Modified | INT | A timestamp of when the XML data was last modified. | |||||||||||||||||||||||||||||||||
The Modified field provides an artificial timestamp value of when the XML data was last modified (e.g. by a tag insert or update). Storing the current Modified value and making comparisons later makes it easy to determine that a change has been made. A rough idea of the total number of change requests can also be calculated by subtracting out the difference. | |||||||||||||||||||||||||||||||||||
Path | STRING | Set this field if the XML document originates from a file source. | |||||||||||||||||||||||||||||||||
XML documents can be loaded from the file system by specifying a file path in this field. If set post-initialisation, all currently loaded data will be cleared and the file will be parsed automatically. The XML class supports LoadFile(), so an XML file can be pre-cached by the program if it is frequently used during a program's life cycle. | |||||||||||||||||||||||||||||||||||
ReadOnly | INT | Prevents modifications and enables caching for a loaded XML data source. | |||||||||||||||||||||||||||||||||
This field can be set to | |||||||||||||||||||||||||||||||||||
Source | OBJECTPTR | Set this field if the XML data is to be sourced from another object. | |||||||||||||||||||||||||||||||||
An XML document can be loaded from another object by referencing it here, on the condition that the object's class supports the Read action. If set post-initialisation, all currently loaded data will be cleared and the source object will be parsed automatically. | |||||||||||||||||||||||||||||||||||
Start | INT | Set a starting cursor to affect the starting point for some XML operations. | |||||||||||||||||||||||||||||||||
When using any XML function that creates an XML string (e.g. SaveToObject), the XML object will include the entire XML tree by default. Defining the Start value will restrict processing to a specific tag and its children. The Start field currently affects the SaveToObject() action and the Statement field. | |||||||||||||||||||||||||||||||||||
Statement | STRING | XML data is processed through this field. | |||||||||||||||||||||||||||||||||
Set the Statement field to parse an XML formatted data string through the object. If this field is set after initialisation then the XML object will clear any existing data first. Be aware that setting this field with an invalid statement will result in an empty XML object. Reading the Statement field will return a serialised string of XML data. By default all tags will be included in the statement unless a predefined starting position is set by the Start field. The string result is an allocation that must be freed. | |||||||||||||||||||||||||||||||||||
Tags | STRUCT [] | Points to an array of tags loaded into an XML object. | |||||||||||||||||||||||||||||||||
The successful parsing of XML data will make the information available via the Tags array. The array is presented as a series of XMLTag structures. Each XMLTag will also have at least one attribute set in the Developers may treat the entire tag hierarchy as readable, but writes should be accomplished with the available XML methods. |
The following actions are currently supported:
Name | Comment | |||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Clear | Clears all of the data held in an XML object. | |||||||||||||||||||||||||||||||
GetKey | Retrieves data from an xml object. | |||||||||||||||||||||||||||||||
ERR acGetKey(*Object, CSTRING Key, STRING Value, LONG Size)
The XML class uses key-values for the execution of XPath queries. Documentation of the XPath standard is out of the scope for this document, however the following examples illustrate the majority of uses for this query language and a number of special instructions that we support:
The | ||||||||||||||||||||||||||||||||
Reset | Clears the information held in an XML object. | |||||||||||||||||||||||||||||||
SaveToObject | Saves XML data to a storage object (e.g. File). | |||||||||||||||||||||||||||||||
ERR acSaveToObject(*Object, OBJECTID Dest, CLASSID ClassID)
| ||||||||||||||||||||||||||||||||
SetKey | Sets attributes and content in the XML tree using XPaths. | |||||||||||||||||||||||||||||||
ERR acSetKey(*Object, CSTRING Key, CSTRING Value)
Use SetKey to add tag attributes and content using XPaths. The XPath is specified in the It is not possible to add new tags using this action - it is only possible to update existing tags. Please note that making changes to the XML tree will render all previously obtained tag pointers and indexes invalid. Error Codes
|
The following methods are currently supported:
Name | Comment | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Count | Count all tags that match a given XPath. | ||||||||||||||||||||||
ERR xml::Count(OBJECTPTR Object, CSTRING XPath, LONG * Result)
This method will count all tags that match a given Error Codes
| |||||||||||||||||||||||
Filter | Filters the XML data down to a single tag and its children. | ||||||||||||||||||||||
ERR xml::Filter(OBJECTPTR Object, CSTRING XPath)
The Filter() method is used to reduce the amount of data in an XML tree, filtering out all data exclusive to the targeted tag and its children. This is useful for speeding up XPath queries where interest is limited to only one area of the XML tree, or for reducing the memory footprint of large trees. Data that has been filtered out by this method is permanently removed. Error Codes
| |||||||||||||||||||||||
FindTag | Searches for a tag via XPath. | ||||||||||||||||||||||
ERR xml::FindTag(OBJECTPTR Object, CSTRING XPath, FUNCTION * Callback, LONG * Result)
This method will return the first tag that matches the search string specified in The Error Codes
| |||||||||||||||||||||||
GetAttrib | Retrieves the value of an XML attribute. | ||||||||||||||||||||||
ERR xml::GetAttrib(OBJECTPTR Object, LONG Index, CSTRING Attrib, CSTRING * Value)
The GetAttrib method scans a tag for a specific attribute and returns it. A tag It is recommended that C/C++ programs bypass this method and access the XMLAttrib structure directly. Error Codes
| |||||||||||||||||||||||
GetContent | Extracts the content of an XML tag. | ||||||||||||||||||||||
ERR xml::GetContent(OBJECTPTR Object, LONG Index, STRING Buffer, LONG Length)
The GetContent method is used to extract the string content from an XML tag. It will extract content that is immediately embedded within the XML tag and will not perform deep analysis of the tag structure (refer to Serialise() for deep extraction). Consider the following structure: <body> Hello <bold>my</bold> friend! </body> This will produce the result Error Codes
| |||||||||||||||||||||||
GetTag | Returns a pointer to the XMLTag structure for a given tag index. | ||||||||||||||||||||||
ERR xml::GetTag(OBJECTPTR Object, LONG Index, struct XMLTag ** Result)
This method will return the XMLTag structure for a given tag Error Codes
| |||||||||||||||||||||||
InsertContent | Inserts XML content into the XML tree. | ||||||||||||||||||||||
ERR xml::InsertContent(OBJECTPTR Object, LONG Index, XMI Where, CSTRING Content, LONG * Result)
The InsertContent method will insert content strings into any position within the XML tree. A content string must be provided in the To modify existing content, call SetAttrib() instead. Error Codes
| |||||||||||||||||||||||
InsertXML | Parse an XML string and insert it in the XML tree. | ||||||||||||||||||||||
ERR xml::InsertXML(OBJECTPTR Object, LONG Index, XMI Where, CSTRING XML, LONG * Result)
The InsertXML() method is used to translate and insert a new set of XML tags into any position within the XML tree. A standard XML statement must be provided in the XML parameter and the target insertion point is specified in the Index parameter. An insertion point relative to the target index must be specified in the Error Codes
| |||||||||||||||||||||||
InsertXPath | Inserts an XML statement in an XML tree. | ||||||||||||||||||||||
ERR xml::InsertXPath(OBJECTPTR Object, CSTRING XPath, XMI Where, CSTRING XML, LONG * Result)
The InsertXPath method is used to translate and insert a new set of XML tags into any position within the XML tree. A standard XML statement must be provided in the XML parameter and the target insertion point is referenced as a valid Error Codes
| |||||||||||||||||||||||
MoveTags | Move an XML tag group to a new position in the XML tree. | ||||||||||||||||||||||
ERR xml::MoveTags(OBJECTPTR Object, LONG Index, LONG Total, LONG DestIndex, XMI Where)
This method is used to move XML tags within the XML tree structure. It supports the movement of single and groups of tags from one index to another. The client must supply the index of the tag that will be moved and the index of the target tag. All child tags of the source will be included in the move. An insertion point relative to the target index must be specified in the Error Codes
| |||||||||||||||||||||||
RemoveTag | Removes tag(s) from the XML structure. | ||||||||||||||||||||||
ERR xml::RemoveTag(OBJECTPTR Object, LONG Index, LONG Total)
The RemoveTag method is used to remove one or more tags from an XML structure. Child tags will automatically be discarded as a consequence of using this method, in order to maintain a valid XML structure. This method is capable of deleting multiple tags if the This method is volatile and will destabilise any cached address pointers that have been acquired from the XML object. Error Codes
| |||||||||||||||||||||||
RemoveXPath | Removes tag(s) from the XML structure, using an xpath lookup. | ||||||||||||||||||||||
ERR xml::RemoveXPath(OBJECTPTR Object, CSTRING XPath, LONG Limit)
The RemoveXPath method is used to remove one or more tags from an XML structure. Child tags will automatically be discarded as a consequence of using this method, in order to maintain a valid XML structure. Individual tag attributes can also be removed if an attribute is referenced at the end of the The removal routine will be repeated so that each tag that matches the XPath will be deleted, or the This method is volatile and will destabilise any cached address pointers that have been acquired from the XML object. Error Codes
| |||||||||||||||||||||||
Serialise | Serialise part of the XML tree to an XML string. | ||||||||||||||||||||||
ERR xml::Serialise(OBJECTPTR Object, LONG Index, XMF Flags, STRING * Result)
The Serialise() method will serialise all or part of the XML data tree to a string. The string will be allocated as a memory block and stored in the Result parameter. It must be freed once the data is no longer required. Error Codes
| |||||||||||||||||||||||
SetAttrib | Adds, updates and removes XML attributes. | ||||||||||||||||||||||
ERR xml::SetAttrib(OBJECTPTR Object, LONG Index, LONG Attrib, CSTRING Name, CSTRING Value)
This method is used to update and add attributes to existing XML tags, as well as adding or modifying content. The data for the attribute is defined in the NOTE: The attribute at position 0 declares the name of the tag and should not normally be accompanied with a value declaration. However, if the tag represents content within its parent, then the Name must be set to Error Codes
| |||||||||||||||||||||||
Sort | Sorts XML tags to your specifications. | ||||||||||||||||||||||
ERR xml::Sort(OBJECTPTR Object, CSTRING XPath, CSTRING Sort, XSF Flags)
The Sort method is used to sort a single branch of XML tags in ascending or descending order. An The Error Codes
|
Standard flags for the XML class.
Name | Description |
---|---|
XMF::ALL_CONTENT | By default the XML parser will trim whitespace (such as return codes, spaces and tabs) found in the XML content between tags. Setting this flag turns off this feature, allowing all whitespace to be included. |
XMF::INCLUDE_COMMENTS | By default, comments are stripped from all XML input. This flag ensures that they are retained. |
XMF::INCLUDE_SIBLINGS | Include siblings when building an XML string (GetXMLString() only) |
XMF::INDENT | Indent the output of serialised XML to improve readability. |
XMF::LOCK_REMOVE | Prevents removal of tags from the XML tree. This specifically affects the RemoveTag and RemoveXPath methods. |
XMF::LOG_ALL | Print extra log messages. |
XMF::NEW | Creates an empty XML object on initialisation - if the Path field has been set, the source file will not be loaded. |
XMF::NO_ESCAPE | Turns off escape code conversion. |
XMF::PARSE_ENTITY | Entity references in the DTD will be parsed automatically. |
XMF::PARSE_HTML | Automatically parse HTML escape codes. |
XMF::READABLE | Indent the output of serialised XML to improve readability. |
XMF::STRIP_CDATA | Do not echo CDATA sections. Note that this option is used as a parameter, not an object flag. |
XMF::STRIP_CONTENT | Strip all content from incoming XML data. |
XMF::STRIP_HEADERS | XML headers found in the source data will not be included in the parsed results. |
XMF::WELL_FORMED | By default, the XML class will accept badly structured XML data. By setting this flag, all processed XML statements must be well formed (tags must balance) or an ERR::BadData error will be returned during processing. |
Tag insertion options.
Name | Description |
---|---|
XMI::CHILD | Insert as the first child of the target. |
XMI::CHILD_END | Insert as the last child of the target. |
XMI::NEXT | Insert as the next tag of the target. |
XMI::PREV | Insert as the previous tag of the target. |
For SetAttrib()
Name | Description |
---|---|
XMS::NEW | Adds a new attribute. Note that if the attribute already exists, this will result in at least two attributes of the same name in the tag. |
XMS::UPDATE | As for UPDATE_ONLY , but if the attribute does not exist, it will be created. |
XMS::UPDATE_ONLY | SetAttrib will find the target attribute and update it. It is not possible to rename the attribute when using this technique. ERR::Search is returned if the attribute cannot be found. |
Options for the Sort method.
Name | Description |
---|---|
XSF::CHECK_SORT | Tells the algorithm to check for a 'sort' attribute in each analysed tag and if found, the algorithm will use that as the sort value instead of that indicated in the Attrib field. |
XSF::DESC | Sort in descending order. |
Field | Type | Description |
---|---|---|
Name | std::string | Name of the attribute |
Value | std::string | Value of the attribute |
Field | Type | Description |
---|---|---|
ID | LONG | Unique ID assigned to the tag on creation |
ParentID | LONG | Unique ID of the parent tag |
LineNo | LONG | Line number on which this tag was encountered |
Flags | XTF | Optional flags |
Attribs | pf::vector<XMLAttrib> | Array of attributes for this tag |
Children | pf::vector<XMLTag> | Array of child tags |
Class Info | |
---|---|
ID | ID_XML |
Category | Data |
Include | modules/xml.h |
Version | 1 |