Loading…

XML Reader

Some MQL4-adjacent data still arrives as XML rather than JSON - economic calendar feeds and a handful of legacy broker and data-vendor APIs among them. CXmlReader parses a document once into a flat array of SXmlNode structs, each carrying a parent index, the same design this catalogue's JsonParser uses for the same reason: MQL4 has no templates and no tagged-union type, so a flat array with parent pointers turns tree navigation into a linear scan instead of hand-rolled pointer chasing, and the parser never calls new. Elements and attributes are read back by PATH: GetText("events/event[1]/ title") or GetAttribute("events/event[1]", "impact"). A path segment is a tag name with an optional zero-based [n] to pick among repeated siblings - default 0, the first one - so a document with several same-named child elements (the overwhelmingly common shape of a feed: many <event> entries under one <events> parent) is walked with a simple loop over CountChildren() rather than a manual tree walk. The path may start with or without the root tag name; both "calendar/meta/generatedAt" and "meta/generatedAt" resolve to the same node when calendar is the document root, so callers do not have to remember which convention a particular call site used. This is a reader, not a general XML implementation, and says so plainly: no namespaces, no DTD or schema validation, no processing instructions beyond skipping the <?xml ...?> declaration and <!-- comments -->. That covers the actual shape of XML an MQL4 program receives without dragging in a specification MQL4 has no use for. The five predefined entities (&lt; &gt; &amp; &quot; &apos;) are decoded in both text and attribute values; anything else, including numeric &#NN; references, passes through literally rather than being guessed at or rejected. Self-closing tags (<event impact="low"/>) are supported and behave exactly like a normal element with no text and no children - GetText on one falls back to its default the same way it would for any other empty match. A mismatched or missing close tag is rejected with a specific error rather than silently closing the wrong element, which is where a naive regex-based XML reader usually goes wrong first. As with JsonParser, a failed Parse() leaves any previously parsed document completely intact - the parser builds into a temporary array and only replaces the live one on success, so a feed that arrives truncated or corrupted mid-download does not wipe out the last good calendar the EA is currently using. Notes and limitations - Read-only. There is no serializer - this header is for reading feeds and simple API responses, not for building XML documents. - No namespace awareness. A prefixed tag like <ns:event> is matched as the literal tag name "ns:event", not resolved against a namespace URI. - No DTD or schema validation, and no entity expansion beyond the five predefined XML entities. - Path resolution rescans from the root on every call, which is the right trade for feed-sized documents read a handful of times; cache values needed inside a tight per-tick loop rather than re-resolving paths there. - Not thread-safe, which matches MQL4's single-threaded-per-chart execution model; nothing here changes that.

Script · MT4 · v1.0 · Free

About XML Reader

XML Reader is a script for MetaTrader 4. It has 1 setting, such as Feed label. MQL source code included.

Files in the download: XmlReader.mqh, XmlReader.txt, XmlReaderDemo.mq4.

Settings

SettingDefaultWhat it does
Feed labeldemo feedLabel printed above the parsed document

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria