如何在OmniXML中创建简单的XML [英] How to create simple XML in OmniXML

查看:154
本文介绍了如何在OmniXML中创建简单的XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建这个结构xml

i want to create this structure xml

<root ver="" file=""> 
  <row> 
    <id></id> 
    <name></name> 
    <surname></surname> 
    <time></time> 
    <old></old> 
    <subject></subject> 
  </row> 
</root> 

当我做主要结构,我从在线数据库获取数据,我想添加到我的xml文件

when i do the main structure i get the data from online database and i want to add it in my xml file


  1. 如何创建主体结构

  2. 如何添加新行每次更新数据库

  3. 如何排序名称节点的基础xml
  1. how can i create the main structure
  2. how can i add a new Row in every update the database made
  3. how can i sort the xml, base of the name node

请帮助

谢谢

推荐答案

您可以使用创建继承定义你的xml的类

You can use create inherited classes which define your 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;

之后编写一个过程来导出数据,例如

After that write a procedure to export the data e.g.

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;

这篇关于如何在OmniXML中创建简单的XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆