Download MetaTrader 5

Libraries: EasyXML - XML Parser

To add comments, please log in or register
Having troubles understanding the code? Articles can help you!
Automated-Trading
Admin
99085
Automated-Trading 2013.12.25 11:47 

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

Jordi Bassaganas
3527
Jordi Bassaganas 2014.04.03 11:23  

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.

meyti
57
meyti 2016.05.06 05:27  

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"? 

meyti
57
meyti 2016.05.09 15:26  

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,"",""); 

blackhawk
39
blackhawk 2017.01.18 18:03  

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

To add comments, please log in or register