RAD Studio
|
Once you have created a transformation file that indicates how to transform an XML document into a data packet, you can create data packets for any XML document that conforms to the schema used in the transformation. These data packets can then be assigned to a client dataset and saved to a file so that they form the basis of a file-based database application.
The TXMLTransform component transforms an XML document into a data packet according to the mapping in a transformation file.
There are three ways to specify the source XML document:
There are two ways to specify the transformation that converts the XML document into a data packet:
To cause TXMLTransform to perform its transformation and generate a data packet, you need only read the Data property. For example, the following code uses an XML document and transformation file to generate a data packet, which is then assigned to a client dataset:
XMLTransform1.SourceXMLFile := 'CustomerDocument.xml'; XMLTransform1.TransformationFile := 'CustXMLToCustTable.xtr'; ClientDataSet1.XMLData := XMLTransform1.Data;
XMLTransform1->SourceXMLFile = "CustomerDocument.xml"; XMLTransform1->TransformationFile = "CustXMLToCustTable.xtr"; ClientDataSet1->XMLData = XMLTransform1->Data;
When you define a transformation using xmlmapper.exe, you can specify that some of the nodes in the XML document are "user-defined." User-defined nodes are nodes for which you want to provide the transformation in code rather than relying on a straightforward node-value-to-field-value translation.
You can provide the code to translate user-defined nodes using the OnTranslate event. The OnTranslate event handler is called every time the TXMLTransform component encounters a user-defined node in the XML document. In the OnTranslate event handler, you can read the source document and specify the resulting value for the field in the data packet.
For example, the following OnTranslate event handler converts a node in the XML document with the following form
<FullName> <Title> </Title> <FirstName> </FirstName> <LastName> </LastName> </FullName>
into a single field value:
procedure TForm1.XMLTransform1Translate(Sender: TObject; Id: String; SrcNode: IDOMNode; var Value: String; DestNode: IDOMNode); var CurNode: IDOMNode; begin if Id = 'FullName' then begin Value = ''; if SrcNode.hasChildNodes then begin CurNode := SrcNode.firstChild; Value := Value + CurNode.nodeValue; while CurNode <> SrcNode.lastChild do begin CurNode := CurNode.nextSibling; Value := Value + ' '; Value := Value + CurNode.nodeValue; end; end; end; end;
void __fastcall TForm1::XMLTransform1Translate(TObject *Sender, AnsiString Id, _di_IDOMNode SrcNode, AnsiString &Value, _di_IDOMNode DestNode) { if (Id == "FullName") { Value = ""; if (SrcNode.hasChildNodes) { _di_IXMLDOMNode CurNode = SrcNode.firstChild; Value = SrcValue + AnsiString(CurNode.nodeValue); while (CurNode != SrcNode.lastChild) { CurNode = CurNode.nextSibling; Value = Value + AnsiString(" "); Value = Value + AnsiString(CurNode.nodeValue); } } } }
Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
|
What do you think about this topic? Send feedback!
|