XMLParser Class
The XMLParser class handles reading, parsing and writing CDP XML files / strings. More...
Header: | #include <XMLParser> |
Public Functions
XMLParser(void) | |
XMLParser(IErrorReporter *pReporter) | |
virtual | ~XMLParser(void) |
void | CallCallback() |
void | Clear() |
virtual void | ClearLastErrorMessage() |
void | CreateElement(std::string &strLocation, XMLElementEx *pChildToInsert) |
void | DeleteElement(XMLPrimitive *pElement) |
void | DeleteWhenDestroyed(XMLPrimitive *pPrim) |
XMLElementEx * | FindFirstElement(const char *pzName, int nMaxLevel = -1) |
XMLElementEx * | FindFirstElement(const std::string &name, int nMaxLevel = -1) |
XMLElementEx * | FindFirstElement(XMLPrimitive *pStartAt, const char *pzName, const char *pzAttributeName, const char *pzAttributeValue = nullptr, const char *pzSecondAttributeName = nullptr, const char *pzSecondAttributeValue = nullptr) |
XMLElementEx * | FindNamedElementInTree(const std::string &nameAttributeValue, XMLElementEx *pStartAt) |
XMLString | GetChildElementValue(const char *pElement) |
XMLPrimitive * | GetElement(const char *pzSearchString, XMLPrimitive *pStartFromElement = nullptr, int maxLevel = 0) |
XMLElementEx * | GetElement(const std::string &searchstring, XMLPrimitive *pStartFromElement = nullptr, int maxLevel = 0) |
const std::string & | GetFileName() |
XMLPrimitive * | GetFirstPrimitive() |
virtual const char * | GetLastErrorMessage() |
XMLPrimitive * | GetNextPrimitive() |
unsigned int | GetReadBufferSize() |
unsigned int | GetWriteBufferSize() |
XMLAttribute * | ParseAttribute(const char *pzBuffer) |
XMLAttribute * | ParseAttribute(const std::string &buffer) |
XMLPrimitive * | ParseNewXML(const char *pzBuffer) |
XMLPrimitive * | ParseNewXML(const std::string &buffer) |
XMLPrimitive * | ParseNewXMLAndInsertInTree(const char *pzBuffer, const char *pzInElement, int nLevelHint) |
bool | ReadXMLFile(const char *fileName) |
bool | ReadXMLFile(const std::string &filename) |
void | ResetNextPrimitive() |
bool | Set(const char *pzXml) |
void | Set(XMLPrimitive *pNew, bool bDelete = false) |
virtual void | SetLastErrorMessage(const char *message, ...) |
void | SetXMLCallback(IXMLCallback *pComp, IXMLCALLBACK_FUNC_PTR pFunction = & IXMLCallback::XMLParserCallback) |
void | WriteXMLBuffer(char *pzBuffer) |
bool | WriteXMLFile(const char *fileName = nullptr, bool debugEx = false) |
void | WriteXMLString(XMLBuffer &str) |
Detailed Description
The XMLParser class handles reading, parsing and writing CDP XML files / strings.
It generates a tree of elements with bi-directional access.
Usage
The recommended usage is to parse the XML using callbacks. This is the least cpu-intensive method, as the XML tree is first built up and then traversed only once. Example:
XMLParser parser; ... class MyClass: public IXMLCallback { ... virtual void XMLParserCallback(XMLPrimitive* p); } ... parser.SetXMLCallback(this); if(parser.ReadXMLFile("filename.xml")) { // MyClass::XMParserCallback has now been called with all elements and attributes in the XML tree. } else printf("XML parsing error: %s\n",parser.GetLastErrorMessage());
Inside the XMLParserCallback, each XMLPrimitive can be examined and processed by looking at the Name or the Type of the XMLPrimitive:
MyClass::XMLParserCallback(XMLPrimitive* p) { if(p->GetType()==e_XMLElement) { XMLElementEx* pEx = static_cast<XMLElementEx*>(p); printf("Found %s with value %s\n",pEx->GetName().c_str(),pEx->GetValue().c_str()); } else if (p->GetName()=="somethingSpecial") { printf("Found 'somethingSpecial'!\n"); } }
An alternative way to parse the XML is to not set a callback, but to still call ReadXMLFile() or Set(). The XML tree can then manually be traversed by calling GetElement() or doing GetChild(), GetParent(), GetNext() and GetPrevious().
Note: Traversing the tree manually may be slower than callback-based parsing, unless you know the exact structure of the XML. If the position of an element is not known, the tree must be searched from the start to find it, which may end up increasing the search-time exponentially compared to using a callback.
Errors
If errors are found during parsing, ReadXMLFile() or Set() will return false, and any callbacks will not be invoked. An error message can be retrieved by calling GetLastErrorMessage(). The error message can be cleared by calling Clear().
Member Function Documentation
XMLParser::XMLParser(void)
Constructs the XML parser.
XMLParser::XMLParser(IErrorReporter *pReporter)
Constructs the XMLParser, taking pReporter as handler for error messages.
[virtual]
XMLParser::~XMLParser(void)
Destructs the parser and deletes attached memory and error reporter.
void XMLParser::CallCallback()
Calls the previously set callback recursively with all primitives in the tree. Children are processed first, then attributes, then next items.
See also SetXMLCallback.
void XMLParser::Clear()
Deallocates memory and clears the parser object.
[virtual]
void XMLParser::ClearLastErrorMessage()
Clears the last error message.
void XMLParser::CreateElement(std::string &strLocation, XMLElementEx *pChildToInsert)
Inserts the provided pChildToInsert at the provided strLocation. Uses FindFirstElement() to find the element to call InsertChildLast() on. If strLocation is not found, it is created.
void XMLParser::DeleteElement(XMLPrimitive *pElement)
Unlinks the item from the tree and deletes it.
void XMLParser::DeleteWhenDestroyed(XMLPrimitive *pPrim)
Adds the pointer to a list of items that are deleted when the XMLParser destructor is invoked.
XMLElementEx *XMLParser::FindFirstElement(const char *pzName, int nMaxLevel = -1)
Searches the XML tree down to nMaxLevel for pzName and returns the first primitive of type e_XMLElement, or nullptr if not found.
XMLElementEx *XMLParser::FindFirstElement(const std::string &name, int nMaxLevel = -1)
Searches the XML tree down to nMaxLevel for name and returns the first primitive of type e_XMLElement, or nullptr if not found.
XMLElementEx *XMLParser::FindFirstElement(XMLPrimitive *pStartAt, const char *pzName, const char *pzAttributeName, const char *pzAttributeValue = nullptr, const char *pzSecondAttributeName = nullptr, const char *pzSecondAttributeValue = nullptr)
Returns the first XMLElementEx* that matches the criteria, or nullptr if not found.
XMLElementEx *XMLParser::FindNamedElementInTree(const std::string &nameAttributeValue, XMLElementEx *pStartAt)
Searches through elements (breadth first) to find an element matching the (optionally dot-separated) element name.
Starts at first parser element unless pStartAt is specified.
Example: Assuming this structure:
<MyObject Name='One' Value='1'> <Something Name='Two' SomeAttribute='2'/> </MyObject>
FindNamedElement("One.Two") would return the element pointing to <Something .. >
XMLString XMLParser::GetChildElementValue(const char *pElement)
Gets the value of a child element named pElement, or empty value if not found.
XMLPrimitive *XMLParser::GetElement(const char *pzSearchString, XMLPrimitive *pStartFromElement = nullptr, int maxLevel = 0)
Gets a primitive matching pzSearchString, given pStartFromElement and maxLevel.
The search string takes the following form:
"Model/Signal"
"Model/Signals/Signal[@Name='Joystick']"
"Model/StateTransitions/StateTransition[@FromState='Null' & @ToState='Running']"
"Component/IOConfig/Module[@Name='AIO16_21']/Channels/CDPChannel[@Name='AIO16_21_7']/Scaling/Point[@In='7500']"
If pStartFromElement is nullptr, search starts at beginning.
XMLElementEx *XMLParser::GetElement(const std::string &searchstring, XMLPrimitive *pStartFromElement = nullptr, int maxLevel = 0)
This function overloads GetElement().
Gets an XMLElementEx* from a search string.
const std::string &XMLParser::GetFileName()
Gets the filename as set by ReadXMLFile().
See also ReadXMLFile.
XMLPrimitive *XMLParser::GetFirstPrimitive()
Gets the first XML primitive.
[virtual]
const char *XMLParser::GetLastErrorMessage()
Gets the last set error message.
XMLPrimitive *XMLParser::GetNextPrimitive()
Gets the next primitive, and then updates an internal member to point to the next primitive. Can be called repeatedly to retrieve all elements in the tree. Children are processed before next nodes.
unsigned int XMLParser::GetReadBufferSize()
Gets the buffer size allocated for the XML file.
unsigned int XMLParser::GetWriteBufferSize()
Gets the buffer size required for a successful write.
XMLAttribute *XMLParser::ParseAttribute(const char *pzBuffer)
Parses an xml attribute from the supplied pzBuffer and returns it (or nullptr on error). It is the callers responsibility to manage the returned attribute
See also DeleteWhenDestroyed.
XMLAttribute *XMLParser::ParseAttribute(const std::string &buffer)
Parses an xml attribute from the supplied buffer and returns it (or nullptr on error). It is the callers responsibility to manage the returned attribute
See also DeleteWhenDestroyed.
XMLPrimitive *XMLParser::ParseNewXML(const char *pzBuffer)
Creates an XML tree from the supplied pzBuffer, or returns nullptr on failure. Calls callback for each primitive if the callback has been set on the parser object. Note that it is the callers responsibility to manage the returned items.
See also DeleteWhenDestroyed.
XMLPrimitive *XMLParser::ParseNewXML(const std::string &buffer)
Creates an XML tree from the supplied buffer.
See also ParseNewXML(const char* pzBuffer) and DeleteWhenDestroyed.
XMLPrimitive *XMLParser::ParseNewXMLAndInsertInTree(const char *pzBuffer, const char *pzInElement, int nLevelHint)
Parses pzBuffer using ParseNewXML() and if found, inserts the xml as the last child of the supplied pzInElement. If not found, it inserts the parsed items as the last item at supplied nLevelHint. Returns nullptr on failure. Note that when items are inserted into the tree, the XMLParser takes ownership of the items and will manage(delete) them as required.
See also ParseNewXML.
bool XMLParser::ReadXMLFile(const char *fileName)
Reads fileName in to a local buffer and calls Set() on it. Returns false if an error occurred.
See also Set().
bool XMLParser::ReadXMLFile(const std::string &filename)
Reads XMLFile from filename.
See also Set().
void XMLParser::ResetNextPrimitive()
Resets the internal primitive ptr used by GetNextPrimitive() so that it will start at the first primitive.
See also GetNextPrimitive().
bool XMLParser::Set(const char *pzXml)
Clears any error-messages and sets the parser content by parsing the XML primitives from pzXml. On success, if the callback is set: callback is called on each primitive in the tree. On failure, false is returned.
See also CallCallback() and GetLastErrorMessage().
void XMLParser::Set(XMLPrimitive *pNew, bool bDelete = false)
Sets a new start-primitive to pNew and bDelete specifies if XMLParser should take ownership of pNew and delete it in destructor.
[virtual]
void XMLParser::SetLastErrorMessage(const char *message, ...)
Formats the error message and adds it to the error-reporter.
void XMLParser::SetXMLCallback(IXMLCallback *pComp, IXMLCALLBACK_FUNC_PTR pFunction = & IXMLCallback::XMLParserCallback)
Adds a callback function that will be called for each primitive upon a successful call to Set().
void XMLParser::WriteXMLBuffer(char *pzBuffer)
Writes the parser content to the supplied pzBuffer.
bool XMLParser::WriteXMLFile(const char *fileName = nullptr, bool debugEx = false)
Writes the parser content to fileName. If debugEx is set, issues are reported on the success/failure of file writes.
void XMLParser::WriteXMLString(XMLBuffer &str)
Appends the xml tree to the supplied string.
Get started with CDP Studio today
Let us help you take your great ideas and turn them into the products your customer will love.