Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Catalog · How the catalog works
Anything · Lazarus or Delphi
Random projectRandom project

XML-Parser

For parse XML code in various functions and variants.

View on GitHub

Member rating

No ratings yet.

Sign in to rate this project.

README

:computer: XML-Parser

Compiler
Components None
Description
Last Update
License Freeware

Extensible Markup Language (XML) is a markup language and file format for storing, transmitting, and reconstructing data. It defines a set of rules for encoding documents in a format that is both human-readable and machine-readable. The World Wide Web Consortium's XML 1.0 Specification of 1998 and several other related specifications—all of them free open standards—define XML.

The design goals of XML emphasize simplicity, generality, and usability across the Internet. It is a textual data format with strong support via Unicode for different human languages. Although the design of XML focuses on documents, the language is widely used for the representation of arbitrary data structures, such as those used in web services.

An XML parser is a software component that reads in XML (eXtensible Markup Language) documents, checks them for syntax errors, and translates them into a format readable by programs (often an object model). Since XML is inherently just a raw text stream, the parser converts this text into a hierarchical structure that your application can work with.

Export Formats: XML; XSL; RTF; DOC; INI; TXT; HTML;

There are two fundamental types of XML parsers:

  • Tree-based parsers (DOM)

    The parser reads the entire XML document into memory and creates a tree-like structure (Document Object Model/DOM).

    • Advantages

      You can jump back and forth within the tree as you please, and edit, add, or delete elements.

    • Disadvantages

      High memory usage. With extremely large files, this can lead to memory overflows.

    • Typical libraries

      XmlDoc (Delphi) XmlDocument (C#/.NET), DOMDocument (JavaScript), xml.dom.minidom (Python).

  • Event-based parsers (SAX)

    These parsers read the XML document sequentially from top to bottom (stream-based). They generate events (e.g., "start element found," "text found").

    • Advantages

      Extremely fast and memory-efficient, as the tree is not kept in memory. Ideal for very large files.

    • Disadvantages

      Access is unidirectional (no jumping back). Complex searches are more cumbersome to program.

    • Typical libraries

      XmlDocReader (Delphi) XmlReader (C#/.NET), xml.sax (Python), SAXParser (Java).

Parse Example

xml
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
    <book id="1">
        <title>Harry Potter and the Sorcerer's Stone</title>
        <author>J.K. Rowling</author>
        <year>1997</year>
        <price>24.99</price>
    </book>
    <book id="2">
        <title>The Hobbit</title>
        <author>J.R.R. Tolkien</author>
        <year>1937</year>
        <price>15.00</price>
    </book>
</bookstore>

Keywords:

The strings entered in the "KeyWords.ini" file are displayed in a default color. Generally, XML tags are green, but other colors can be selected.

If values are changed, added, or removed, the same change must be applied in the "Keywords.pas" unit. Otherwise, color errors will occur.

</br>

pascal
procedure HightLight_Syntax(ARE : TRichEdit);
begin

  {...}

  HighLight_Others('<root>', #13, clRed);  //
  HighLight_Others('</root>', #13, clRed); //
  HighLight_Others('<body>', #13, $00d94fdb);  //
  HighLight_Others('</body>', #13, $00d94fdb); //
  HighLight_Others('
  HighLight_Others('', #13, $005f912d); //
  HighLight_Others('<?', '?>', clOlive);
  HighLight_Others('<?xml', '>', clTeal);  //
  HighLight_Others('http', '<', clNavy);   //
  HighLight_Others('', clGray);
end;

The specifications must match in both units Unit1.pas and Keywords.pas. The code is designed so that the keywords are used for the XML tags, which in this case are displayed in blue. That would be important for the HTML export if it is to be displayed in color.

Tag list (PDF) : https://www.iso20022.org/sites/default/files/media/file/XML_Tags.pdf

Text Formatting:

Formatting XML text usually involves either indenting it for better readability (pretty-printing) or preserving text formatting (such as line breaks and whitespace) within the data.

  • Line Feed (LF): &#xA;
  • Carriage Return (CR): &#13; or &#xD;
  • Tab: &#x9;

To structure text with line breaks and ensure the parser maintains them:

xml
<message>
    Line 1 of text &#xA;Line 2 of text &#xA;Line 3 of text
</message>

</br>

Applications

XML has come into common use for the interchange of data over the Internet. Hundreds of document formats using XML syntax have been developed, including RSS, Atom, Office Open XML, OpenDocument, SVG, COLLADA, and XHTML. XML also provides the base language for communication protocols such as SOAP and XMPP. It is one of the message exchange formats used in the Asynchronous JavaScript and XML (AJAX) programming technique.