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 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, the user can load an XML document to work with it.

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


Sub Sample2(strXML: String);
Var
    XMLDoc: IXMLDOMDocument3;
    Loaded: Boolean;
Begin
    XMLDoc := New FreeThreadedDOMDocument60.Create;
    Loaded := XMLDoc.loadXML(strXML);
    If Loaded Then
        //Further work with loaded document
    End If;
    // Allow access to file
    Dispose XMLDoc;
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 If;
    Dispose XMLDoc;
End Sub Sample3;

Advanced

See below various examples of working with XML files:

Example
Replacing Nodes in Different Documents
Working with the CDATA section

See also:

XML Assembly