Examples of Working with XML Files

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

Loading XML File

To load an XML file, you need to do the following:

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

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

  3. Using the load or loadXML method of the IXmlDomDocument interface (methods description is provided in MSDN), you 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 the 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 the loaded document
    End If;
End Sub Sample2;

Working with Nodes

To get desired 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
    //Loading document
    //...
    If Loaded Then
        //Root node
        Node := XMLDOc.documentElement;
        //Getting children of the root node
        Nodes := Node.childNodes;
        //Getting desired nodes in accordance with XPath query
        Nodes := XMLDOc.selectNodes("<XPath query for nodes selection>");
        //New node
        NewNode := XMLDOc.createNode("element""Comments""");
        //New attribute for the node
        NewAttr := XMLDOc.createAttribute("text");
        NewAttr.value := "Comment...";
        NewNode.attributes.setNamedItem(NewAttr);
        //Adding created node to the tree
        Node.appendChild(NewNode);
        //Getting desired node in accordance with XPath query
        Node := XMLDOc.selectSingleNode("<XPath query to select a node>");
        //...
End Sub Sample3;

More

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