我想创建这个结构xml
<root ver="" file="">
<row>
<id></id>
<name></name>
<surname></surname>
<time></time>
<old></old>
<subject></subject>
</row>
</root>
当我做主要结构时,我从在线数据库获取数据,我想将其添加到我的xml文件中
请帮忙
谢谢
答案 0 :(得分:4)
使用GpFluentXML:
var
root : IXMLNode;
rowID: integer;
xml : IGpFluentXmlBuilder;
begin
xml := CreateFluentXml
.AddChild('root')
['ver', '']
['file', '']
.Anchor(root);
for rowID := 1 to 10 do begin
xml
.Jump(root)
.AddChild('row')
.AddChild('id', rowID)
.AddSibling('name', '')
.AddSibling('surname', '')
.AddSibling('time', '')
.AddSibling('old', '')
.AddSibling('subject', '');
end;
XMLSaveToFile(xml.Xml, 'filename', ofIndent);
end;
使用OmniXMLUtils(OmniXML发行版的一部分):
var
row : IXMLNode;
rowID: integer;
xml : IXMLDocument;
begin
xml := ConstructXMLDocument('root');
SetNodeAttr(xml.DocumentElement, 'ver', '');
SetNodeAttr(xml.DocumentElement, 'file', '');
for rowID := 1 to 10 do begin
row := AppendNode(xml, 'row');
SetNodeTextInt(row, 'id', rowID);
SetNodeText(row, 'name', '');
SetNodeText(row, 'surname', '');
SetNodeText(row, 'time', '');
SetNodeText(row, 'old', '');
SetNodeText(row, 'subject', '');
end;
XMLSaveToFile(xml, 'filename', ofIndent);
end;
对于排序,您不对XML进行排序;在将数据添加到文档之前对数据进行排序。
答案 1 :(得分:4)
您可以使用定义xml的创建继承类
interface
uses
OmniXML, OmniXMLProperties;
type
TRow = class(TGpXMLData)
public
constructor Create(Node: IXMLNode); override;
property Id: integer index 0 read GetXMLPropInt write SetXMLPropInt;
property Name: WideString index 1 read GetXMLPropWide write SetXMLPropWide;
property Surname: WideString index 2 read GetXMLPropWide write SetXMLPropWide;
property Time: WideString index 3 read GetXMLPropWide write SetXMLPropWide;
property Old: WideString index 4 read GetXMLPropWide write SetXMLPropWide;
property Subject: WideString index 5 read GetXMLPropWide write SetXMLPropWide;
end;
TRows = class(TGpXMLList)
protected
function GetRow(Value: integer): TRow;
public
constructor Create(ParentNode: IXMLNode); reintroduce;
function Add: TRow; reintroduce;
property Rows[Value: integer]: TRow read GetRow; default;
end;
TRootsXml = class(TGpXmlDocList)
private
fRows: TRows;
public
constructor Create; reintroduce;
destructor Destroy; override;
property Ver: WideString index 0 read GetXMLAttrPropWide write SetXMLAttrPropWide;
property RootFile: WideString index 1 read GetXMLAttrPropWide write SetXMLAttrPropWide;
property Rows: TRows read fRows;
end;
implementation
constructor TRow.Create(Node: IXMLNode);
begin
inherited;
InitChildNodes(['id', 'name', 'surname', 'time', 'old', 'subjects'],
['', '', '', '', '', '']);
end;
constructor TRows.Create(parentNode: IXMLNode);
begin
inherited Create(parentNode, '', 'row', TRow);
end;
function TRows.Add: TRow;
begin
result := TRow(inherited Add);
end;
function TRows.GetRow(Value: Integer): TRow;
begin
Result := TRow(inherited Items[Value]);
end;
constructor TRootsXml.Create;
var
xmlPI: IXMLProcessingInstruction;
begin
inherited Create('Root', '', '', nil);
xmlPI := XMLDoc.CreateProcessingInstruction('xml', 'version="1.0" encoding="utf-8"');
XMLDoc.InsertBefore(xmlPI, node);
InitChildNodes(['ver', 'file'], ['', '']);
fRows := TRows.Create(node);
end;
destructor TRootsXml.Destroy;
begin
fRows.free;
inherited;
end;
之后写一个导出数据的程序,例如
procedure TExport.Exportrows;
var
rootsXml: TRootsXml;
row: TRow;
i: integer;
begin
rootsXml := TRootsXml.Create;
try
rootsXml.Ver := 'version 27';
rootsXml.RootFile := 'fred.exe';
for i := 1 to 10 do
begin
row := rootsXml.Rows.Add;
row.Id := i;
row.Name := 'fred';
row.Surname := 'Flintstone';
row.Time := 'late';
row.Old := 'very';
row.Subject := 'Delphi';
end;
rootsXml.SaveToFile('c:\test\test.xml', ofIndent);
finally
rootsXml.free;
end;
end;
答案 2 :(得分:3)
使用标准组件TXMLDocument :(如果您没有找到其他解决方案,也许您可以将TXMLDocument生成的xml复制到您的OmniXML:/)
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, xmldom, XMLIntf, StdCtrls, msxmldom, XMLDoc;
type
TForm1 = class(TForm)
xmldoc1: TXMLDocument;
btn1: TButton;
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.btn1Click(Sender: TObject);
var
I: Integer;
begin
xmldoc1.XML.Text := '<root ver="" file=""></root>'; //<row><id></id><name></name><surname></surname><time></time><old></old><subject></subject></row>';
xmldoc1.Active := True;
for I := 0 to 9 do
begin
with xmldoc1.ChildNodes['root'].AddChild('row') do
begin
AddChild('name').Text := 'NAME' + IntToStr(I);
AddChild('surname').Text := 'SURNAME';
AddChild('time').Text := TimeToStr(Now);
AddChild('old').Text := IntToStr(20);
AddChild('subject').Text := 'SUBJECT';
end;
end;
ShowMessage(xmldoc1.XML.Text);
end;
end.
使用OmniXML,这样的事情应该有效:
uses OmniXML;
var
aChild: IXMLElement;
aRow : IXMLElement;
rowID : Integer;
XMLDoc: IXMLDocument;
begin
XMLDoc := CreateXMLDoc;
XMLDoc.DocumentElement := XMLDoc.CreateElement('root');
XMLDoc.DocumentElement.SetAttribute('ver', 'value');
XMLDoc.DocumentElement.SetAttribute('file', 'value');
for rowID := 1 to 10 do begin
aRow := XMLDoc.CreateElement('row');
XMLDoc.DocumentElement.AppendChild(aRow);
aChild := XMLDoc.CreateElement('id');
aChild.Text := IntToStr(rowID);
aRow.AppendChild(aChild);
aChild := XMLDoc.CreateElement('name');
aChild.Text := 'NAME';
aRow.AppendChild(aChild);
aChild := XMLDoc.CreateElement('surname');
aChild.Text := 'SURNAME';
aRow.AppendChild(aChild);
//...
end;
XMLDoc.Save('filename', ofIndent);
end;