A library for parsing of XML documents. Pure MQL5, it doesn’t uses any external libraries.
To use the library, just include it:
#include <Xml\XmlBase.mqh>
Example:
#include <Xml\XmlBase.mqh> CXmlDocument doc; void OnStart() { string file = "File.xml"; string err; if (doc.CreateFromText(file,err)) { CXmlElement * xmlItem = doc.FDocumentElement.GetChild(0); for (int i=0; i<xmlItem.GetChildCount(); i++) if (xmlItem.GetChild(i).GetName() == "LAYER") { CXmlElement* layer = xmlItem.GetChild(i); for (int j=0; j<layer.GetChildCount(); ++j) { if (layer.GetChild(j).GetName() == "NEURON") { /* .... */ } } } } }
Notes:
- The library was written when constuctors in MQL5 were without parameters;
- Only basic functions of Xml standard is supported;
- Plese inform me about errors, suggestions and bug fixes.
Main components:
CXmlDocument class provides methods for downloading of XML documents from file/string and save to file.
CreateX functions parses XML documents and create hierarchy of DOM model, the working with it can be done using FDocumentElement
class CXmlDocument { private: void DoElementTrimText(CXmlElement &aXmlItem) ; public: CXmlElement FDocumentElement; void CXmlDocument (); void ~CXmlDocument (); void Clear(); void CopyTo (CXmlDocument &xmlDoc); bool CreateFromText (const string& xml, string& err); bool CreateFromFile (const string filename, string& err); bool SaveToFile (const string filename); string GetXml(); };
CXmlElement class is a base interface of any XML document. It provides access to elements, attributes and content
class CXmlElement { private: string FName; CXmlAttribute* FAttributes[]; CXmlElement *FElements[]; string FText; CXmlElement* FParent; public: //--- constructor methods void CXmlElement (); void ~CXmlElement (); void Init (const string aName, const CXmlElement* aParent=NULL, const string aText=""); void CopyTo (CXmlElement &aDst); virtual void Clear (); //--- main service methods string GetName () const; void SetName (const string aName); string GetText () const; void SetText (const string aText); CXmlElement* GetParent () const; void SetParent (CXmlElement* aParent); //--- attribute service methods int GetAttributeCount () const; int GetAttributeIndex (CXmlAttribute* aAttr) const; CXmlAttribute* GetAttribute (const string aName) const; CXmlAttribute* GetAttribute (int aPos) const; string GetAttributeValue (const string aName) const; CXmlAttribute* AttributeInsertAt (CXmlAttribute* aAttr, int aPos); CXmlAttribute* AttributeAdd (CXmlAttribute* aAttr); CXmlAttribute* AttributeInsertAfter (CXmlAttribute* aAfter, CXmlAttribute* aAttr); CXmlAttribute* AttributeInsertBefore (CXmlAttribute* aBefore, CXmlAttribute* aAttr); CXmlAttribute* AttributeRemove (CXmlAttribute* aAttr); CXmlAttribute* AttributeRemove (int aPos); void AttributeDelete (CXmlAttribute* aAttr); void AttributeDelete (int aPos); void AttributeDeleteAll (); //--- child service methods int GetChildCount() const; int GetChildIndex (CXmlElement* aElement) const; CXmlElement* GetChild (const string aName) const; CXmlElement* GetChild (int aPos) const; string GetChildText (const string aName) const; CXmlElement* ChildInsertAt (CXmlElement* aElement, int aPos); CXmlElement* ChildAdd (CXmlElement* aElement); CXmlElement* ChildInsertAfter (CXmlElement* aAfter, CXmlElement* aElement); CXmlElement* ChildInsertBefore (CXmlElement* aBefore, CXmlElement* aElement); CXmlElement* ChildRemove (CXmlElement* aElement); CXmlElement* ChildRemove (int aPos); void ChildDelete (CXmlElement* aElement); void ChildDelete (int aPos); void ChildDeleteAll (); string GetXml(int aLevel); };
CXmlAttribute is a simple class for working with attributes
class CXmlAttribute { private: string FName; string FValue; public: //--- constructor methods void CXmlAttribute (); void ~CXmlAttribute (); void Init (const string aName, const string aValue); virtual void Clear (); virtual CXmlAttribute* Clone (); //--- service methods string GetName () const; void SetName (const string aName); string GetValue () const; void SetValue (const string aValue); };