📄 omnixmldatabase.pas
字号:
DatasetRowToXMLNode(ds, myNode, doNotExport, options);
ds.Next;
end; //while
finally
try ds.GotoBookmark(startPos); except end;
ds.FreeBookmark(startPos);
end;
finally ds.EnableControls; end;
end; { DatasetToXML }
{:@param ds Dataset to be exported.
@param xmlDocument Textual representation of the XML document holding data
from the ds, stored in the stream.
@param doNotExport Semicolon-delimited list of fields that should not be
exported. No extraneous whitespace should be used.
@param outputFormat XML document formatting.
@param options Export options.
@since 2001-09-09
}
procedure DatasetToXMLDocument(ds: TDataSet; xmlDocument: TStream;
const rootTag: string; const doNotExport: string;
outputFormat: TOutputFormat; options: TOmniXMLDatabaseOptions);
var
xml: IXMLDocument;
begin
if Trim(rootTag) = '' then
raise Exception.Create('DatasetToXMLDocument: rootTag must not be empty');
xml := CreateXMLDoc;
xml.AppendChild(xml.CreateProcessingInstruction('xml', 'version="1.0"'));
xml.AppendChild(xml.CreateElement(rootTag));
DatasetToXML(ds, xml.DocumentElement, doNotExport, options);
xmlDocument.Position := 0;
xmlDocument.Size := 0;
XMLSaveToStream(xml, xmlDocument, outputFormat);
end; { DatasetToXMLDocument }
{:@param ds Dataset to be exported.
@param xmlFileName Name of the file to receive XML document.
@param doNotExport Semicolon-delimited list of fields that should not be
exported. No extraneous whitespace should be used.
@param outputFormat XML document formatting.
@param options Export options.
@since 2003-03-28
}
procedure DatasetToXMLFile(ds: TDataSet; const xmlFileName: string;
const rootTag: string; const doNotExport: string;
outputFormat: TOutputFormat; options: TOmniXMLDatabaseOptions);
var
fs: TFileStream;
begin
if Trim(rootTag) = '' then
raise Exception.Create('DatasetToXMLFile: rootTag must not be empty');
fs := TFileStream.Create(xmlFileName, fmCreate);
try
DatasetToXMLDocument(ds, fs, rootTag, doNotExport, outputFormat, options);
finally FreeAndNil(fs); end;
end; { DatasetToXMLFile }
{:@param ds Dataset to be exported.
@param doNotExport Semicolon-delimited list of fields that should not be
exported. No extraneous whitespace should be used.
@param outputFormat XML document formatting.
@param options Export options.
@returns Textual representation of the XML document holding data from the ds.
@since 2003-03-28
}
function DatasetToXMLString(ds: TDataSet; const rootTag: string;
const doNotExport: string; outputFormat: TOutputFormat;
options: TOmniXMLDatabaseOptions): string;
var
ss: TStringStream;
begin
if Trim(rootTag) = '' then
raise Exception.Create('DatasetToXMLString: rootTag must not be empty');
ss := TStringStream.Create('');
try
DatasetToXMLDocument(ds, ss, rootTag, doNotExport, outputFormat, options);
Result := ss.DataString;
finally FreeAndNil(ss); end;
end; { DatasetToXMLString }
{:@param dsNode Node holding the data to be exported to the dataset.
@param ds Dataset to take data from the dsNode.
@param doNotImport Semicolon-delimited list of fields that should not be
imported. No extraneous whitespace should be used.
@param options Import options.
@since 2001-09-09
}
procedure XMLNodeToDatasetRow(dsNode: IXMLNode; ds: TDataSet;
const doNotImport: string; options: TOmniXMLDatabaseOptions);
var
field : TField;
fieldClass : TFieldClass;
fieldElement: IXMLElement;
fieldNode : IXMLNode;
memStr : TMemoryStream;
myNode : IXMLNode;
myNodes : IXMLNodeList;
nodeData : WideString;
nodeName : string;
notImport : string;
function FindField(nodeName: string): TField;
var
iField: integer;
begin
for iField := 0 to ds.Fields.Count-1 do begin
Result := ds.Fields[iField];
if AnsiSameText(CleanupNodeName(Result.FieldName), nodeName) then
Exit;
end; //for
Result := nil;
end; { FindField }
begin { XMLNodeToDatasetRow }
if doNotImport = '' then
notImport := ''
else
notImport := ';' + doNotImport + ';';
ds.Append;
myNodes := dsNode.ChildNodes;
myNodes.Reset;
repeat
myNode := myNodes.NextNode;
if assigned(myNode) and (myNode.NodeType = ELEMENT_NODE) then begin
nodeName := myNode.NodeName;
field := FindField(nodeName);
if (notImport = '') or (Pos(';'+field.FieldName+';', notImport) = 0) then begin
if not assigned(field) then begin
if not (odbIgnoreMissingColumns in options) then
raise EOmniXMLDatabase.CreateFmt('Field %s does not exist in the table', [nodeName]);
end
else begin
fieldNode := dsNode.SelectSingleNode(nodeName);
field.Clear;
if assigned(fieldNode) and
Supports(fieldNode, IXMLElement, fieldElement) and
(fieldElement.GetAttribute(CXMLFieldIsEmpty) <> XMLBoolToStr(true)) then
begin
nodeData := fieldNode.Text;
fieldClass := ds.FieldDefs[field.Index].FieldClass;
if (fieldClass = TStringField) or
(fieldClass = TStringField) or
(fieldClass = TWideStringField) or
(fieldClass = TGuidField) then
field.AsString := nodeData
else if
(fieldClass = TSmallIntField) or
(fieldClass = TWordField) or
(fieldClass = TIntegerField) then
field.AsInteger := XMLStrToIntDef(nodeData, 0)
else if fieldClass = TLargeIntField then
(field as TLargeintField).AsLargeInt := XMLStrToInt64Def(nodeData, 0)
else if
(fieldClass = TBCDField) or
(fieldClass = TCurrencyField) then
field.AsCurrency := XMLStrToRealDef(nodeData, 0)
else if fieldClass = TFloatField then
field.AsFloat := XMLStrToRealDef(nodeData, 0)
else if fieldClass = TDateField then
field.AsDateTime := XMLStrToDateDef(nodeData, 0)
else if fieldClass = TTimeField then
field.AsDateTime := XMLStrToTimeDef(nodeData, 0)
else if fieldClass = TDateTimeField then
field.AsDateTime := XMLStrToDateTimeDef(nodeData, 0)
else if fieldClass = TBooleanField then
field.AsBoolean := XMLStrToBoolDef(nodeData, false)
else if
(fieldClass = TBlobField) or
(fieldClass = TGraphicField) or
(fieldClass = TMemoField) then
begin
memStr := TMemoryStream.Create;
try
GetNodeTextBinary(dsNode, nodeName, memStr);
memStr.Position := 0;
(field as TBLOBField).LoadFromStream(memStr);
finally FreeAndNil(memStr); end;
end
else if
(fieldClass = TBytesField) or
(fieldClass = TVarBytesField) then
begin
memStr := TMemoryStream.Create;
try
GetNodeTextBinary(dsNode, nodeName, memStr);
memStr.Position := 0;
field.Size := memStr.Size;
(field as TBytesField).SetData(memStr.Memory);
finally FreeAndNil(memStr); end;
end
else if fieldClass = TAutoIncField then
// skipped
else begin
if not (odbIgnoreUnsupportedColumns in options) then
raise EOmniXMLDatabase.CreateFmt('Unsupported column type in column %s', [field.FieldName]);
end;
end;
end;
end; //if notImport ...
end;
until not assigned(myNode);
ds.Post;
end; { XMLNodeToDatasetRow }
{:@param parentNode Node that holds CXMLDatasetRows children that will be
exported to the dataset.
@param ds Dataset to take data from the parentNode.
@param doNotImport Semicolon-delimited list of fields that should not be
imported. No extraneous whitespace should be used.
@param options Import options.
@since 2001-09-09
}
procedure XMLToDataset(parentNode: IXMLNode; ds: TDataSet;
const doNotImport: string; options: TOmniXMLDatabaseOptions);
var
myNode : IXMLNode;
myNodes: IXMLNodeList;
begin
if assigned(parentNode) then begin
myNodes := parentNode.SelectNodes(CXMLDatasetRows);
myNodes.Reset;
ds.DisableControls;
try
repeat
myNode := myNodes.NextNode;
if assigned(myNode) and (myNode.NodeType = ELEMENT_NODE) then
XMLNodeToDatasetRow(myNode, ds, doNotImport, options);
until not assigned(myNode);
finally ds.EnableControls; end;
end;
end; { XMLToDataset }
{:@param xmlDocument Textual representation of the XML document, stored in the
stream.
@param ds Dataset to take data from the XML document.
@param doNotImport Semicolon-delimited list of fields that should not be
imported. No extraneous whitespace should be used.
@param options Import options.
@since 2001-09-09
}
procedure XMLDocumentToDataset(xmlDocument: TStream; ds: TDataSet;
const doNotImport: string; options: TOmniXMLDatabaseOptions);
var
xml: IXMLDocument;
begin
xml := CreateXMLDoc;
xmlDocument.Position := 0;
if XMLLoadFromStream(xml, xmlDocument) then begin
if assigned(xml.DocumentElement) then
XMLToDataset(xml.DocumentElement, ds, doNotImport, options);
end
else
raise EOmniXMLDatabase.CreateFmt(
'Failed to parse XML document. Error occured at character %d line %d. Reason: %s',
[xml.ParseError.LinePos, xml.ParseError.LinePos, xml.ParseError.Reason]);
end; { XMLDocumentToDataset }
{:@param xmlFileName Name of the file containing XML document.
@param ds Dataset to take data from the XML document.
@param doNotImport Semicolon-delimited list of fields that should not be
imported. No extraneous whitespace should be used.
@param options Import options.
@since 2003-03-28
}
procedure XMLFileToDataset(const xmlFileName: string; ds: TDataSet;
const doNotImport: string; options: TOmniXMLDatabaseOptions);
var
fs: TFileStream;
begin
fs := TFileStream.Create(xmlFileName, fmOpenRead);
try
XMLDocumentToDataset(fs, ds, doNotImport, options);
finally FreeAndNil(fs); end;
end; { XMLFileToDataset }
{:@param xmlString String containing XML document.
@param ds Dataset to take data from the XML document.
@param doNotImport Semicolon-delimited list of fields that should not be
imported. No extraneous whitespace should be used.
@param options Import options.
@since 2003-03-28
}
procedure XMLStringToDataset(const xmlString: string; ds: TDataSet;
const doNotImport: string; options: TOmniXMLDatabaseOptions);
var
ss: TStringStream;
begin
ss := TStringStream.Create(xmlString);
try
XMLDocumentToDataset(ss, ds, doNotImport, options);
finally FreeAndNil(ss); end;
end; { XMLStringToDataset }
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -