Libraries: EasyXML - XML Parser

 

EasyXML - XML Parser:

EasyXML is a native MQL5 XML Parsing Library.

It can parse XML from three different sources: URL, File and String Input. It is completey object oriented an strives to integrate neately with MQL5, as it utilizes CObject and CArrayObj from the MQL5 Standard library to store the DOM.

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
// Create object of class CEasyXml
   CEasyXml EasyXmlDocument;

// Set debugging
   EasyXmlDocument.setDebugging(false);

// Example: Walking through the dom tree inline
   if(EasyXmlDocument.loadXmlFromUrl("http://www.forexfactory.com/ffcal_week_this.xml"))
     {
      CEasyXmlNode *RootNode=EasyXmlDocument.getDocumentRoot();

      //iterate through root node
      for(int i=0; i<RootNode.Children().Total(); i++)
        {
         CEasyXmlNode *ChildNode=RootNode.Children().At(i);
         Print(IntegerToString(i)+" "+ChildNode.getName());

         //iterate through child nodes
         for(int j=0; j<ChildNode.Children().Total(); j++)
           {
            CEasyXmlNode *SubNode=ChildNode.Children().At(j);
            Print(IntegerToString(i)+"-"+IntegerToString(j)+"   "+SubNode.getName()+" | "+SubNode.getValue());
           }
        }
     }

  }

Output:

GP      0       14:38:35.295    Example_Inline_Parsing (AUDCAD,H1)      0 event
PK      0       14:38:35.295    Example_Inline_Parsing (AUDCAD,H1)      0-0   title | Bank Holiday
DL      0       14:38:35.295    Example_Inline_Parsing (AUDCAD,H1)      0-1   country | JPY
ND      0       14:38:35.295    Example_Inline_Parsing (AUDCAD,H1)      0-2   date | 12-23-2013
KI      0       14:38:35.295    Example_Inline_Parsing (AUDCAD,H1)      0-3   time | 12:00am
HO      0       14:38:35.295    Example_Inline_Parsing (AUDCAD,H1)      0-4   impact | Holiday
HI      0       14:38:35.295    Example_Inline_Parsing (AUDCAD,H1)      0-5   forecast | 
ON      0       14:38:35.295    Example_Inline_Parsing (AUDCAD,H1)      0-6   previous | 
JF      0       14:38:35.295    Example_Inline_Parsing (AUDCAD,H1)      1 event
IL      0       14:38:35.295    Example_Inline_Parsing (AUDCAD,H1)      1-0   title | UBS Consumption Indicator
IP      0       14:38:35.295    Example_Inline_Parsing (AUDCAD,H1)      1-1   country | CHF
EH      0       14:38:35.295    Example_Inline_Parsing (AUDCAD,H1)      1-2   date | 12-23-2013
HN      0       14:38:35.295    Example_Inline_Parsing (AUDCAD,H1)      1-3   time | 7:00am
GK      0       14:38:35.295    Example_Inline_Parsing (AUDCAD,H1)      1-4   impact | Low
ML      0       14:38:35.295    Example_Inline_Parsing (AUDCAD,H1)      1-5   forecast | 
ID      0       14:38:35.295    Example_Inline_Parsing (AUDCAD,H1)      1-6   previous | 1.28
EK      0       14:38:35.295    Example_Inline_Parsing (AUDCAD,H1)      2 event
HN      0       14:38:35.295    Example_Inline_Parsing (AUDCAD,H1)      2-0   title | German Import Prices m/m
QE      0       14:38:35.295    Example_Inline_Parsing (AUDCAD,H1)      2-1   country | EUR
PO      0       14:38:35.295    Example_Inline_Parsing (AUDCAD,H1)      2-2   date | 12-23-2013
EE      0       14:38:35.295    Example_Inline_Parsing (AUDCAD,H1)      2-3   time | 7:00am
NF      0       14:38:35.295    Example_Inline_Parsing (AUDCAD,H1)      2-4   impact | Low
MS      0       14:38:35.295    Example_Inline_Parsing (AUDCAD,H1)      2-5   forecast | 0.4%
HK      0       14:38:35.295    Example_Inline_Parsing (AUDCAD,H1)      2-6   previous | -0.7%
DO      0       14:38:35.295    Example_Inline_Parsing (AUDCAD,H1)      3 event
OJ      0       14:38:35.295    Example_Inline_Parsing (AUDCAD,H1)      3-0   title | Personal Income m/m
FN      0       14:38:35.295    Example_Inline_Parsing (AUDCAD,H1)      3-1   country | USD
KR      0       14:38:35.295    Example_Inline_Parsing (AUDCAD,H1)      3-2   date | 12-23-2013
NI      0       14:38:35.295    Example_Inline_Parsing (AUDCAD,H1)      3-3   time | 1:30pm
QM      0       14:38:35.295    Example_Inline_Parsing (AUDCAD,H1)      3-4   impact | Low
RF      0       14:38:35.295    Example_Inline_Parsing (AUDCAD,H1)      3-5   forecast | 0.4%
IN      0       14:38:35.295    Example_Inline_Parsing (AUDCAD,H1)      3-6   previous | -0.1%
MS      0       14:38:35.295    Example_Inline_Parsing (AUDCAD,H1)      4 event
CQ      0       14:38:35.295    Example_Inline_Parsing (AUDCAD,H1)      4-0   title | Personal Spending m/m
OR      0       14:38:35.295    Example_Inline_Parsing (AUDCAD,H1)      4-1   country | USD
JF      0       14:38:35.295    Example_Inline_Parsing (AUDCAD,H1)      4-2   date | 12-23-2013
GM      0       14:38:35.295    Example_Inline_Parsing (AUDCAD,H1)      4-3   time | 1:30pm
KG      0       14:38:35.295    Example_Inline_Parsing (AUDCAD,H1)      4-4   impact | Medium
NJ      0       14:38:35.295    Example_Inline_Parsing (AUDCAD,H1)      4-5   forecast | 0.5%
OP      0       14:38:35.295    Example_Inline_Parsing (AUDCAD,H1)      4-6   previous | 0.3%
JH      0       14:38:35.295    Example_Inline_Parsing (AUDCAD,H1)      5 event
....

Author: Paul van Hemmen

 

I was able to compile and run the code on the first attempt in my new MT5.

  1. 0 wininet problems!
  2. Very good code

Great contribution!, thank you.

 

Thanks for good library, 

when i tried to pars my document from "File Input" method i got this error :*** Invalid Character found. Should be the beginning of an open tag P:0 ***.

after a headache and searching around the world i realized that the encoding that parser accepts is "UCS-2 LE BOM" so it was better mentioning this in the description, 

is there any way to change this default encoding as my XMl Writer using "UTF-8-BOM"? 

 

hi again, i found a solution for my case may it helps others,:)

i changed one line "CEasyXml::loadXmlFromFile(string pFilename)" function belongs to EasyXML Class :

 int hFile=FileOpen(pFilename,FILE_READ|FILE_ANSI,CP_UTF8); 

this reads my file correctly with encoding format of "UTF-8-BOM".

but still needs to remove "BOM"Bytes Of Orders="" characters from the string  with :

StringReplace(pText,"",""); 

 

Hi, I'm trying to get the data on this page: https://www.usda.gov/oce/commodity/wasde/latest.xml but I ger this error with the script example_inline_parsing:  *** Invalid Character found. Should be the beginning of an open tag P:0 ***
I'm just replacing the original url with the one above and the script doesn't work.

Any Idea?

Thank you

 

There seems to be a bug where self closing tags are used.


So in many xml editors, when there is an empty tag, the tag is automatically converted to a self-closing tag.  E.g.

<element></element> 

becomes

<element/>  or  


<element />

automatically.

and according to w3 XML elements, it is legal to use self-closing tags.   So I am unsure why they would not be supported when importing xml documents.  Hopefully @Liquinaut  can address this.

Luckily in my case, the amount of self-closing tags is relatively small.  But what if the document has hundreds or thousands of such tags.  are users expected to somehow convert these tags into open + close tags??


edit:  For those looking to convert self-closing tags into separated tags, you may want to use this RegEx find/replace

Find:  (<)(\w*)(.*)(/>)

Replace: <$2$3></$2>

[This is with .NET regEx Engine used in TextCrawler3.  Other engines might use a different format; experiment].  Preserves all attributes within the original tag, if any

XML Elements
XML Elements
  • www.w3schools.com
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
 

Hello I am using the library but I get the error in easyxmlnode.mqh of invalid pointer in this function:

CEasyXmlNode *CEasyXmlNode::createSibling(CEasyXmlNode *pSiblingNode)
  {
   pSiblingNode.Prev(GetPointer(this));
   GetPointer(this).Next(pSiblingNode);

   ParentNode.createChild(pSiblingNode);
   return ParentNode.LastChild();
  }

Do you know how can I solve the error?

 
mappi1589 #:

Hello I am using the library but I get the error in easyxmlnode.mqh of invalid pointer in this function:

Do you know how can I solve the error?

What is your XML input ?
 
a forex factory calendar xml, it works almost everytime but randomly it show that error. Can I do something to avoid the error or run the code again if it detect that error again?
 
mappi1589 #:
a forex factory calendar xml, it works almost everytime but randomly it show that error. Can I do something to avoid the error or run the code again if it detect that error again?

You could tweak the library to catch this problem and return NULL in createSibling(), but you would have to fix the calling point to deal with it. And possibly you could have other issues.

This error probably appears because you get a wrong XML answer from time to time, so in my opinion the best solution is to check for that beforehand.

 
Alain Verleyen #:

You could tweak the library to catch this problem and return NULL in createSibling(), but you would have to fix the calling point to deal with it. And possibly you could have other issues.

This error probably appears because you get a wrong XML answer from time to time, so in my opinion the best solution is to check for that beforehand.

Thank you for your answer Alain, ok I will check if I can solve the error by checking the xml answer.

Reason: