Using Code That the XML Data Binding Wizard Generates

From RAD Studio
Jump to: navigation, search

Go Up to Abstracting XML Documents with the Data Binding Wizard

Once the wizard has generated a set of interfaces and implementation classes, you can use them to work with XML documents that match the structure of the document or schema you supplied to the wizard. Just as when you are using only the built-in XML components, your starting point is the Xml.XMLDoc.TXMLDocument component that appears in the Internet category of the Tool Palette.

To work with an XML document, use the following steps:

  1. Obtain an interface for the root node of your XML document. You can do this in one of three ways:
Method Description

Place a TXMLDocument component in your form or data module. Bind the TXMLDocument to an XML document by setting the FileName property.

(As an alternative approach, you can use a string of XML by setting the XML property at run time.) Then, In your code, call the global function that the wizard created to obtain an interface for the root node of the XML document. For example, if the root element of the XML document was the tag <StockList>, by default, the wizard generates a function Getstocklist, which returns an IXMLStockListType. interface:

      StockList: IXMLStockListType;
      XMLDocument1.FileName := 'Stocks.xml';
      StockList := Getstocklist(XMLDocument1);     
   XMLDocument1->FileName := "Stocks.xml";
    _di_IStockListType StockList = GetStockListType(XMLDocument1);

Call the generated Load... function

Call the generated Load... function to create and bind the TXMLDocument instance and obtain its interface all in one step. For example, using the same XML document described above:

      StockList: IXMLStockListType;
      StockList := Loadstocklist('Stocks.xml');     
    _di_IStockListType StockList = LoadStockListType("Stocks.xml");

Call the generated New... function

Call the generated New... function to create the TXMLDocument instance for an empty document when you want to create all the data in your application:

      StockList: IXMLStockListType;
      StockList := Newstocklist;     
    _di_IStockListType StockList = NewStockListType();
  1. This interface has properties that correspond to the subnodes of the document's root element, as well as properties that correspond to that root element's attributes. You can use these to traverse the hierarchy of the XML document, modify the data in the document, and so on.
  2. To save any changes you make using the interfaces generated by the wizard, call the TXMLDocument component's SaveToFile method or read its XML property.

Tip: If you set the Options property of the TXMLDocument object to include doAutoSave, then you do not need to explicitly call the SaveToFile method.

See Also