Examples of Working with XML Files

Consider a few simple examples of working with XML files using the XML assembly in the Fore language.

Loading XML File

To load an XML file:

  1. Initialize an object of one of the available classes: DOMDocument, DOMDocument40, DOMDocument60, FreeThreadedDOMDocument, FreeThreadedDOMDocument60. The first three classes are used work with various MSXML versions (3.0, 4.0, and 6.0 respectively). The last two classes are used if the application is developed that provides for multithreaded access to shared data.

  2. The obtained object can be cast to one of the following interfaces: IXmlDomDocument, IXmlDomDocument2, or IXmlDomDocument3. IXmlDomDocument - contains base properties and methods for working with XML structures; IXmlDomDocument2 - an extension of the IXmlDomDocument interface that supports caching schemes and audit functions, including support for XPath expressions; IXmlDomDocument3 - a development of IXmlDomDocument2 that includes two new methods that are importNode and validateNode.

  3. Using the load or loadXML method of the IXmlDomDocument interface (methods description is provided in MSDN), the user can load an XML document to further work with it.

NOTE. Starting with Prognoz Platform 8.0 working with XML files is executed via MSXML services 6.0.

Sub Sample1;
Var
    XMLDoc: IXMLDOMDocument;
    XMLName: String = "c:\Data.xml";
    Loaded: Boolean;
Begin
    XMLDoc := New DOMDocument.Create;
    Loaded := XMLDoc.load(XMLName);
    If Loaded Then
        //Further work with loaded document
        //...
        XMLDoc.save(XMLName);
    End If;
End Sub Sample1;


Sub Sample2(strXML: String);
Var
    XMLDoc: IXMLDOMDocument3;
    Loaded: Boolean;
Begin
    XMLDoc := New DOMDocument60.Create;
    Loaded := XMLDoc.loadXML(strXML);
    If Loaded Then
        //Further work with loaded document
    End If;
End Sub Sample2;

Working with Nodes

To get necessary nodes, use appropriate properties and methods of the IXmlDomDocument interface:

Sub Sample3;
Var
    XMLDoc: IXMLDOMDocument;
    Loaded: Boolean;
    Nodes: IXmlDomNodeList;
    Node, NewNode: IXmlDomNode;
    NewAttr: IXmlDomAttribute;
Begin
    //Load document
    //...
    If Loaded Then
        //Root node
        Node := XMLDOc.documentElement;
        //Get child nodes of root node
        Nodes := Node.childNodes;
        //Get required nodes in accordance with XPath query
        Nodes := XMLDOc.selectNodes("<XPath query for nodes selection>");
        //New node
        NewNode := XMLDOc.createNode("element""Comments""");
        //New attribute for node
        NewAttr := XMLDOc.createAttribute("text");
        NewAttr.value := "Comment...";
        NewNode.attributes.setNamedItem(NewAttr);
        //Add created node to tree
        Node.appendChild(NewNode);
        //Get required node in accordance with XPath query
        Node := XMLDOc.selectSingleNode("<XPath query for node selection>");
        //...
End Sub Sample3;

Advanced

See below various examples of working with XML files:

Example
Working with XML files via MSXML 6.0
Working with the CDATA section

See also:

XML Assembly