• XML


    一、使用     

          1) Copy the NativeXml files from the NativeXml directory into your project folder. Copy additional
              files in the General directory to your project folder or some appropriate general folder you want.

          2) Include the clause "uses NativeXml" in your unit.

              If you want to use NativeXml in all your projects, you can also add the NativeXml folder to your library path.

      最简单的方法是将General 下的 文件和 nativexml 下的 文件复制到一个目录下,然后加入到 library路径,这样以后用到就不用拷来拷去了。

      在(实现)部分   uses NativeXml; //必须的。

      以下代码选自万一的博客 :

    uses NativeXml; //!

    const xmlFile = 'C:Tempxml_test_filesasic.xml'; //测试文件

    {原样打开}
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      Memo1.Lines.LoadFromFile(xmlFile);
    end;

    {使用 NativeXml 打开}
    procedure TForm1.Button1Click(Sender: TObject); var
      xml: TNativeXml;
    begin
      xml := TNativeXml.Create(nil);
      xml.LoadFromFile(xmlFile);
      Memo1.Text := xml.WriteToString;
      xml.Free;
    end;

    {使用缩进}
    procedure TForm1.Button2Click(Sender: TObject);
    var
      xml: TNativeXml;
    begin
      xml := TNativeXml.Create(nil);
      xml.LoadFromFile(xmlFile);
      xml.XmlFormat := xfReadable; //格式枚举:xfCompact(压缩)、xfReadable(缩进)、xfPreserve(好像暂未处理)
      Memo1.Text := xml.WriteToString;
      xml.Free;
    end;

    {指定缩进文本}
    procedure TForm1.Button3Click(Sender: TObject);
    var
      xml: TNativeXml;
    begin
      xml := TNativeXml.Create(nil);
      xml.LoadFromFile(xmlFile);
      xml.IndentString := '    '; //IndentString 属性的默认值是 #9
      xml.XmlFormat := xfReadable;
      Memo1.Text := xml.WriteToString;
      xml.Free;
    end;


    二、NativeXML对象的创建

         1、 XML:=TNativeXML.Create();//aowner可以为nil,可以是Self(这样可以随着宿主的释放而释放)

         2、xml := TNativeXml.CreateEx(False, //是否建立 Declaration 对象, 如:VersionString//显示 XML 版本;EncodingString //显示 XML 编码
                                 True,  //是否建立跟节点
                                 nil    //指定属主
                                 );

         3、{使用 CreateName 建立可直接指定根节点的名称}    xml:=TNativeXml.CreateName('MyRoot'); 

         4、创那个键之后可以{使用 New 方法清空并重新构建}
    procedure TForm1.Button4Click(Sender: TObject);
    var
      xml: TNativeXml;
    begin
      xml := TNativeXml.Create(nil);
      xml.LoadFromFile(xmlFile);
      ShowMessage(xml.WriteToString);
      xml.New;   //  XML文件清空了相当于  xml.Clear;。
      ShowMessage(xml.WriteToString);
      xml.Free;
    end;

    5、{使用 ReadFromString 从文本读入}
    procedure TForm1.Button5Click(Sender: TObject);
    const
      str = '<?xml version="1.0"?><Root><Items><item>1</item><item>2</item></Items></Root>';
    var
      xml: TNativeXml;
    begin
      xml := TNativeXml.Create(nil);
      xml.ReadFromString(str);
      xml.XmlFormat := xfReadable;
      Memo1.Text := xml.WriteToString;
    end

    三、保存XML文件

          1、xml.ExternalEncoding := seUTF8;  //指定编码,默认为 UTF8   
          2、xml.SaveToFile('Name.xml'); //保存

    四、XML节点{定义根节点   root:TSdElement}

      TsdElement = class(TsdContainerNode)
      private
        FNameID: integer;
      protected
        function GetName: Utf8String; override;
        function GetValue: Utf8String; override;
        procedure SetName(const Value: Utf8String); override;
        procedure SetValue(const Value: Utf8String); override;
        procedure ParseIntermediateData(P: TsdXmlParser); override;
        procedure CopyFrom(ANode: TObject); override;
      public
        // NodeAdd overrides the default addition in a container node: direct nodes
        // first, then subelements
        //function NodeAdd(ANode: TXmlNode): integer; override;
        function ParseStream(P: TsdXmlParser): TXmlNode; override;
        procedure WriteStream(S: TStream); override;
        function ElementType: TsdElementType; override;
      end;

    六、事件(摘自 万一)

    unit Unit1;
    interface
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, NativeXml{!};
    type
      TForm1 = class(TForm)
        Button1: TButton;
        Button2: TButton;
        Memo1: TMemo;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
      private
        procedure XmlEvent(Sender: TObject; ANode: TXmlNode); //TsdXmlNodeEvent 事件
      public
      end;
    var
      Form1: TForm1;
    implementation
    {$R *.dfm}
    const
      xmlFile = 'C:Tempxml_test_filesCDATA_section.xml'; //测试文件
    {测试 OnNodeNew 事件:开始载入节点时发生}
    procedure TForm1.Button1Click(Sender: TObject);
    var
      xml: TNativeXml;
    begin
      Memo1.Clear;
      xml := TNativeXml.Create(nil);
      xml.OnNodeNew := XmlEvent;
      xml.LoadFromFile(xmlFile);
      xml.Free;
    end;
    {测试 OnNodeLoaded 事件:节点载入完成时发生}
    procedure TForm1.Button2Click(Sender: TObject);
    var
      xml: TNativeXml;
    begin
      Memo1.Clear;
      xml := TNativeXml.Create(nil);
      xml.OnNodeLoaded := XmlEvent;
      xml.LoadFromFile(xmlFile);
      xml.Free;
    end;
    {将被调用的事件, OnNodeNew 和 OnNodeLoaded 事件的参数格式是一样的}
    procedure TForm1.XmlEvent(Sender: TObject; ANode: TXmlNode);
    var
      depth: string;
    begin
      if ANode.ElementType = xeElement then         //xeElement 是 TsdElementType 枚举的类型之一
      begin
        depth := StringOfChar(#9, ANode.TreeDepth); //TreeDepth 是节点深度
        Memo1.Lines.Add(depth + ANode.Name);
      end;
    end;
    end.

    NativeXml的方法属性:ANativeXml  := TNativeXml.Create(Self);//创建

    ANativeXml.XmlFormat        := xfReadable; //格式

    ANativeXml.EncodingString   := 'UTF-8';//编码

    ANativeXml.ExternalEncoding := seUTF8;

    ANativeXml.LoadFromFile();//从文件夹中导入XML文件

    ANativeXml.SaveToFile();//XML文件保存到文件夹

    节点的方法属性:

    ANativeXml.Root;//XML文件的根节点,注意一切都从根开始

    ANativeXml.Root.NodeByName(‘节点名’);//通过节点名获得节点

    NativeXml.Root.Attributes;//节点的属性对象

    NativeXml.Root.ChildContainers;//节点的子节点对象

    NativeXml.Root.NodeNew();//增加新的子节点

    NativeXml.Root.Name;//节点名

    NativeXml.Root.Value;//节点值

    七、添加节点

    根节点的添加,可以在创建时直接指定根节点:

    var

    XmlDoc: TnativeXml;

    Node:TxmlNode;

    Begin

    XmlDoc := TNativeXml.CreateName('Data'); //创建根节点的语法

       XmlDoc.XmlFormat := xfReadable;

    ... ...

    添加节点有以下几种方法:

    Node:=xmlDoc.Root.NodeNew(‘BasicDataNo’);

    Node.value:=’1’;

    XmlDoc.Root.NodeNew(‘name’).value:=’sam’;

    xmlDoc.Root.WriteString(‘sex’,’male);

    ____________________________________

    更加完整的参考万一的博客,在这里引用一下:

    uses NativeXml; 
     
    procedure TForm1.Button1Click(Sender: TObject); 
    var 
      xml: TNativeXml; 
      node: TXmlNode; 
    begin 
      xml := TNativeXml.CreateName('List'); 
      xml.XmlFormat := xfReadable; 
     
      // 
      node := xml.Root.NodeNew('item'); 
      node.Value := '张三'; 
      // 
      with xml.Root.NodeNew('item') do 
      begin 
        Value := '李四'; 
      end; 
      // 
      xml.Root.NodeNew('item').Value := '王五'; 
     
      Memo1.Text := xml.WriteToString; 
      xml.Free; 
    end; 
    {*************************************** 
    <?xml version="1.0" encoding="UTF-8"?> 
    <List> 
    	<item>张三</item> 
    	<item>李四</item> 
    	<item>王五</item> 
    </List> 
    *****************************************} 
     
    procedure TForm1.Button2Click(Sender: TObject); 
    var 
      xml: TNativeXml; 
    begin 
      xml := TNativeXml.CreateName('List'); 
      xml.XmlFormat := xfReadable; 
     
      xml.Root.WriteString('N1', '张三'); 
      xml.Root.WriteString('N2', '李四'); 
      xml.Root.WriteString('N2', '王五'); 
     
      Memo1.Text := xml.WriteToString; 
      xml.Free; 
    end; 
    {*************************************** 
    <?xml version="1.0" encoding="UTF-8"?> 
    <List> 
    	<N1>张三</N1> 
    	<N2>王五</N2> 
    </List> 
    *****************************************} 
     
    procedure TForm1.Button3Click(Sender: TObject); 
    var 
      xml: TNativeXml; 
    begin 
      xml := TNativeXml.CreateName('List'); 
      xml.XmlFormat := xfReadable; 
     
      xml.Root.NodeNew('item').WriteString('name', '张三'); 
      xml.Root.NodeNew('item').WriteString('name', '李四'); 
      xml.Root.NodeNew('item').WriteString('name', '王五'); 
     
      Memo1.Text := xml.WriteToString; 
      xml.Free; 
    end; 
    {*************************************** 
    <?xml version="1.0" encoding="UTF-8"?> 
    <List> 
    	<item> 
    		<name>张三</name> 
    	</item> 
    	<item> 
    		<name>李四</name> 
    	</item> 
    	<item> 
    		<name>王五</name> 
    	</item> 
    </List> 
    *****************************************} 
     
    procedure TForm1.Button4Click(Sender: TObject); 
    var 
      xml: TNativeXml; 
    begin 
      xml := TNativeXml.CreateName('List'); 
      xml.XmlFormat := xfReadable; 
     
      with xml.Root.NodeNew('item') do 
      begin 
        WriteString('name', '张三'); 
        WriteInteger('age', 11); 
      end; 
     
      with xml.Root.NodeNew('item') do 
      begin 
        WriteString('name', '李四'); 
        WriteInteger('age', 22);    //WriteFloat()、WriteDateTime()、WriteHex()、WriteBool()... 
      end; 
     
      with xml.Root.NodeNew('item') do 
      begin 
        WriteString('name', '王五'); 
        WriteInteger('age', 33); 
      end; 
     
      Memo1.Text := xml.WriteToString; 
      xml.Free; 
    end; 
    {*************************************** 
    <?xml version="1.0" encoding="UTF-8"?> 
    <List> 
    	<item> 
    		<name>张三</name> 
    		<age>11</age> 
    	</item> 
    	<item> 
    		<name>李四</name> 
    		<age>22</age> 
    	</item> 
    	<item> 
    		<name>王五</name> 
    		<age>33</age> 
    	</item> 
    </List> 
    *****************************************} 
     
    procedure TForm1.Button5Click(Sender: TObject); 
    var 
      xml: TNativeXml; 
    begin 
      xml := TNativeXml.CreateName('List'); 
      xml.XmlFormat := xfReadable; 
     
      with xml.Root.NodeNewAtIndex(0, 'item') do 
      begin 
        WriteString('name', '张三'); 
        WriteInteger('age', 11); 
      end; 
     
      with xml.Root.NodeNewAtIndex(0, 'item') do 
      begin 
        WriteString('name', '李四'); 
        WriteInteger('age', 22); 
      end; 
     
      with xml.Root.NodeNewAtIndex(0, 'item') do 
      begin 
        WriteString('name', '王五'); 
        WriteInteger('age', 33); 
      end; 
     
      Memo1.Text := xml.WriteToString; 
      xml.Free; 
    end; 
    {*************************************** 
    <?xml version="1.0" encoding="UTF-8"?> 
    <List> 
    	<item> 
    		<name>王五</name> 
    		<age>33</age> 
    	</item> 
    	<item> 
    		<name>李四</name> 
    		<age>22</age> 
    	</item> 
    	<item> 
    		<name>张三</name> 
    		<age>11</age> 
    	</item> 
    </List> 
    *****************************************} 
     
    procedure TForm1.Button6Click(Sender: TObject); 
    var 
      xml: TNativeXml; 
      node: TXmlNode; 
    begin 
      xml := TNativeXml.CreateName('List'); 
      xml.XmlFormat := xfReadable; 
     
      node := xml.NodeNew('item'); 
      node.WriteString('name', '张三'); 
      node.WriteInteger('age', 11); 
      xml.Root.NodeAdd(node); 
     
      xml.NodeNewEx('item', node);      //NodeNewEx 
      node.WriteString('name', '李四'); 
      node.WriteInteger('age', 22); 
      xml.Root.NodeAdd(node); 
     
      node := xml.NodeNew('item'); 
      node.WriteString('name', '王五'); 
      node.WriteInteger('age', 33); 
      xml.Root.NodeAdd(node); 
     
      Memo1.Text := xml.WriteToString; 
      xml.Free; 
    end; 
    {*************************************** 
    <?xml version="1.0" encoding="UTF-8"?> 
    <List> 
    	<item> 
    		<name>张三</name> 
    		<age>11</age> 
    	</item> 
    	<item> 
    		<name>李四</name> 
    		<age>22</age> 
    	</item> 
    	<item> 
    		<name>王五</name> 
    		<age>33</age> 
    	</item> 
    </List> 
    *****************************************} 
     
    procedure TForm1.Button7Click(Sender: TObject); 
    var 
      xml: TNativeXml; 
      node: TXmlNode; 
    begin 
      xml := TNativeXml.CreateName('List'); 
      xml.XmlFormat := xfReadable; 
     
      node := xml.NodeNew('item', [xml.NodeNewText('name', '张三'), xml.NodeNewInt('age', 11)]); 
      xml.Root.NodeAdd(node); 
     
      node := xml.NodeNew('item', [xml.NodeNewText('name', '李四'), xml.NodeNewInt('age', 22)]); 
      xml.Root.NodeAdd(node); 
     
      node := xml.NodeNew('item', [xml.NodeNewText('name', '王五'), xml.NodeNewInt('age', 33)]); 
      xml.Root.NodeAdd(node); 
     
      Memo1.Text := xml.WriteToString; 
      xml.Free; 
    end; 
    {结果同上} 
     
    procedure TForm1.Button8Click(Sender: TObject); 
    var 
      xml: TNativeXml; 
      node: TXmlNode; 
    begin 
      xml := TNativeXml.CreateName('List'); 
      xml.XmlFormat := xfReadable; 
     
      xml.Root.NodesAdd([ 
         xml.NodeNew('item', [xml.NodeNewText('name', '张三'), xml.NodeNewInt('age', 11)]), 
         xml.NodeNew('item', [xml.NodeNewText('name', '李四'), xml.NodeNewInt('age', 22)]), 
         xml.NodeNew('item', [xml.NodeNewText('name', '王五'), xml.NodeNewInt('age', 33)]) 
      ]); 
     
      Memo1.Text := xml.WriteToString; 
      xml.Free; 
    end; 
    {结果同上} 
     
    procedure TForm1.Button9Click(Sender: TObject); 
    var 
      xml: TNativeXml; 
      node: TXmlNode; 
    begin 
      xml := TNativeXml.CreateName('List'); 
      xml.XmlFormat := xfReadable; 
     
      node := xml.NodeNew('A', [xml.NodeNew('B', [xml.NodeNew('C', [xml.NodeNewText('D', 'E')])])]); 
      xml.Root.NodeAdd(node); 
     
      Memo1.Text := xml.WriteToString; 
      xml.Free; 
    end; 
    {*************************************** 
    <?xml version="1.0" encoding="UTF-8"?> 
    <List> 
    	<A> 
    		<B> 
    			<C> 
    				<D>E</D> 
    			</C> 
    		</B> 
    	</A> 
    </List> *****************************************}
    八、属性、注释以及内容的添加

    添加属性:

     node := xml.Root.NodeNew('item');

    node.AttributeAdd('ID', '1');

    node.AttributeAdd('age', '11');

    node.Value := '张三';

    node := xml.NodeNewTextAttr('item', '张三', [xml.AttrText('ID', '1'), xml.AttrInt('age', 11)]);

    xml.Root.NodeAdd(node);

    {更详细代码,引自万一}

    uses NativeXml; 
     
    procedure TForm1.Button1Click(Sender: TObject); 
    var 
      xml: TNativeXml; 
      node: TXmlNode; 
    begin 
      xml := TNativeXml.CreateName('List'); 
      xml.XmlFormat := xfReadable; 
     
      node := xml.Root.NodeNew('item'); 
      node.AttributeAdd('ID', '1'); 
      node.AttributeAdd('age', '11'); 
      node.Value := '张三'; 
     
      with xml.Root.NodeNew('item') do 
      begin 
        AttributeAdd('ID', '2'); 
        AttributeAdd('age', '22'); 
        Value := '李四'; 
      end; 
     
      with xml.Root.NodeNew('item') do 
      begin 
        AttributeAdd('ID', '3'); 
        AttributeAdd('age', '33'); 
        Value := '王五'; 
      end; 
     
      Memo1.Text := xml.WriteToString; 
      xml.Free; 
    end; 
    {*************************************** 
    <?xml version="1.0" encoding="UTF-8"?> 
    <List> 
    	<item ID="1" age="11">张三</item> 
    	<item ID="2" age="22">李四</item> 
    	<item ID="3" age="33">王五</item> 
    </List> 
    *****************************************} 
     
    procedure TForm1.Button2Click(Sender: TObject); 
    var 
      xml: TNativeXml; 
      node: TXmlNode; 
      attr1,attr2: TsdAttribute; 
    begin 
      xml := TNativeXml.CreateName('List'); 
      xml.XmlFormat := xfReadable; 
     
      attr1 := TsdAttribute.Create(xml); 
      attr1.Name := 'ID'; 
      attr1.Value := '1'; 
      attr2 := TsdAttribute.Create(xml); 
      attr2.Name := 'age'; 
      attr2.ValueAsInteger := 11; 
      node := xml.Root.NodeNew('item'); 
      node.AttributeAdd(attr1); 
      node.AttributeAdd(attr2); 
      node.Value := '张三'; 
     
      attr1 := TsdAttribute.CreateName(xml, 'ID'); 
      attr1.Value := '2'; 
      attr2 := TsdAttribute.CreateNameValue(xml, 'age', '22'); 
      node := xml.Root.NodeNew('item'); 
      node.AttributesAdd([attr1, attr2]); 
      node.Value := '李四'; 
     
      node := xml.Root.NodeNew('item'); 
      node.AttributesAdd([TsdAttribute.CreateNameValue(xml, 'ID', '3'), TsdAttribute.CreateNameValue(xml, 'age', '33')]); 
      node.Value := '王五'; 
     
      Memo1.Text := xml.WriteToString; 
      xml.Free; 
    end; 
    {结果同上} 
     
    procedure TForm1.Button3Click(Sender: TObject); 
    var 
      xml: TNativeXml; 
      node: TXmlNode; 
      attr1,attr2: TsdAttribute; 
    begin 
      xml := TNativeXml.CreateName('List'); 
      xml.XmlFormat := xfReadable; 
     
      attr1 := xml.AttrText('ID', '1'); 
      attr2 := xml.AttrInt('age', 11); 
      node := xml.Root.NodeNew('item'); 
      node.AttributesAdd([attr1, attr2]); 
      node.Value := '张三'; 
     
      node := xml.Root.NodeNew('item'); 
      node.AttributesAdd([xml.AttrText('ID', '2'), xml.AttrInt('age', 22)]); 
      node.Value := '李四'; 
     
      with xml.Root.NodeNew('item') do begin 
        AttributesAdd([xml.AttrText('ID', '3'), xml.AttrInt('age', 33)]); 
        Value := '王五'; 
      end; 
     
      Memo1.Text := xml.WriteToString; 
      xml.Free; 
    end; 
    {结果同上} 
     
    procedure TForm1.Button4Click(Sender: TObject); 
    var 
      xml: TNativeXml; 
      node: TXmlNode; 
    begin 
      xml := TNativeXml.CreateName('List'); 
      xml.XmlFormat := xfReadable; 
     
      node := xml.NodeNewTextAttr('item', '张三', [xml.AttrText('ID', '1'), xml.AttrInt('age', 11)]); 
      xml.Root.NodeAdd(node); 
     
      node := xml.NodeNewTextAttr('item', '李四', [xml.AttrText('ID', '2'), xml.AttrInt('age', 22)]); 
      xml.Root.NodeAdd(node); 
     
      node := xml.NodeNewTextAttr('item', '王五', [xml.AttrText('ID', '3'), xml.AttrInt('age', 33)]); 
      xml.Root.NodeAdd(node); 
     
      Memo1.Text := xml.WriteToString; 
      xml.Free; 
    end; 
    {结果同上} 
     
    procedure TForm1.Button5Click(Sender: TObject); 
    var 
      xml: TNativeXml; 
    begin 
      xml := TNativeXml.CreateName('List'); 
      xml.XmlFormat := xfReadable; 
     
      xml.Root.NodesAdd([ 
        xml.NodeNewTextAttr('item', '张三', [xml.AttrText('ID', '1'), xml.AttrInt('age', 11)]), 
        xml.NodeNewTextAttr('item', '李四', [xml.AttrText('ID', '2'), xml.AttrInt('age', 22)]), 
        xml.NodeNewTextAttr('item', '王五', [xml.AttrText('ID', '3'), xml.AttrInt('age', 33)]) 
      ]); 
     
      Memo1.Text := xml.WriteToString; 
      xml.Free; 
    end; 
    {结果同上} 
     
    procedure TForm1.Button6Click(Sender: TObject); 
    var 
      xml: TNativeXml; 
      node: TXmlNode; 
    begin 
      xml := TNativeXml.CreateName('List'); 
      xml.XmlFormat := xfReadable; 
     
      node := xml.NodeNewAttr('item', [xml.AttrText('name', '张三'), xml.AttrInt('age', 11)]); 
      xml.Root.NodeAdd(node); 
     
      node := xml.NodeNewAttr('item', [xml.AttrText('name', '李四'), xml.AttrInt('age', 22)]); 
      xml.Root.NodeAdd(node); 
     
      node := xml.NodeNewAttr('item', [xml.AttrText('name', '王五'), xml.AttrInt('age', 33)]); 
      xml.Root.NodeAdd(node); 
     
      Memo1.Text := xml.WriteToString; 
      xml.Free; 
    end; 
    {*************************************** 
    <?xml version="1.0" encoding="UTF-8"?> 
    <List> 
    	<item name="张三" age="11"/> 
    	<item name="李四" age="22"/> 
    	<item name="王五" age="33"/> 
    </List> 
    *****************************************} 
     
    procedure TForm1.Button7Click(Sender: TObject); 
    var 
      xml: TNativeXml; 
      node: TXmlNode; 
    begin 
      xml := TNativeXml.CreateName('List'); 
      xml.XmlFormat := xfReadable; 
     
      xml.Root.NodesAdd([ 
        xml.NodeNewAttr('item', [xml.AttrText('name', '张三'), xml.AttrInt('age', 11)]), 
        xml.NodeNewAttr('item', [xml.AttrText('name', '李四'), xml.AttrInt('age', 22)]), 
        xml.NodeNewAttr('item', [xml.AttrText('name', '王五'), xml.AttrInt('age', 33)]) 
      ]); 
     
      Memo1.Text := xml.WriteToString; 
      xml.Free; 
    end; 
    {结果同上}

     

    添加注释:

    node := xml.NodeNewTextType('注释名称', '注释内容', xeComment);

    xml.Root.NodeAdd(node);

    添加CDATA内容

    node.NodeAdd(xml.NodeNewTextType('DataName', 'XXXXXXXXXXXX', xeCData));

    {更详细代码 引自万一}

    uses NativeXml;
    
    procedure TForm1.Button1Click(Sender: TObject);
    var
      xml: TNativeXml;
      node: TXmlNode;
    begin
      xml := TNativeXml.CreateName('List');
      xml.XmlFormat := xfReadable;
    
      node := xml.NodeNewTextType('注释名称', '注释内容', xeComment);
      xml.Root.NodeAdd(node);
    
      node := xml.NodeNewTextType('item', '张三', xeElement);
      xml.Root.NodeAdd(node);
    
      Memo1.Text := xml.WriteToString;
      xml.Free;
    end;
    {***************************************
    <?xml version="1.0" encoding="UTF-8"?>
    <List><!--注释内容-->
    	<item>张三</item>
    </List>
    *****************************************}
    
    procedure TForm1.Button2Click(Sender: TObject);
    var
      xml: TNativeXml;
      node: TXmlNode;
    begin
      xml := TNativeXml.CreateName('List');
      xml.XmlFormat := xfReadable;
    
      node := xml.NodeNewTextType('item', '张三', xeElement, [xml.AttrText('ID', '1'), xml.AttrInt('age', 11)]);
      xml.Root.NodeAdd(node);
    
      Memo1.Text := xml.WriteToString;
      xml.Free;
    end;
    {***************************************
    <?xml version="1.0" encoding="UTF-8"?>
    <List>
    	<item ID="1" age="11">张三</item>
    </List>
    *****************************************}
    
    procedure TForm1.Button3Click(Sender: TObject);
    var
      xml: TNativeXml;
      node: TXmlNode;
    begin
      xml := TNativeXml.CreateName('List');
      xml.XmlFormat := xfReadable;
    
      node := xml.NodeNewTextTypeAttr('item', '', xeElement, [xml.AttrText('ID', '1')], [xml.NodeNewText('name', '张三'), xml.NodeNewInt('age', 11)]);
      xml.Root.NodeAdd(node);
    
      Memo1.Text := xml.WriteToString;
      xml.Free;
    end;
    {***************************************
    <?xml version="1.0" encoding="UTF-8"?>
    <List>
    	<item ID="1">
    		<name>张三</name>
    		<age>11</age>
    	</item>
    </List>
    *****************************************}
    
    procedure TForm1.Button4Click(Sender: TObject);
    var
      xml: TNativeXml;
      node: TXmlNode;
    begin
      xml := TNativeXml.CreateName('List');
      xml.XmlFormat := xfReadable;
    
      node := xml.Root.NodeNew('Data1');
      node.NodeAdd(xml.NodeNewTextType('DataName', 'XXXXXXXXXXXX', xeCData));
    
      Memo1.Text := xml.WriteToString;
      xml.Free;
    end;
    {***************************************
    <?xml version="1.0" encoding="UTF-8"?>
    <List>
    	<Data1><![CDATA[XXXXXXXXXXXX]]></Data1>
    </List>
    *****************************************}
    
    procedure TForm1.Button5Click(Sender: TObject);
    var
      xml: TNativeXml;
    begin
      xml := TNativeXml.CreateName('List');
      xml.XmlFormat := xfReadable;
    
      xml.Root.NodeAdd(xml.NodeNewTextType('aaa', 'AAAAAAA', xeElement));
      xml.Root.NodeAdd(xml.NodeNewTextType('bbb', 'BBBBBBB', xeAttribute));
      xml.Root.NodeAdd(xml.NodeNewTextType('ccc', 'CCCCCCC', xeComment));
      xml.Root.NodeAdd(xml.NodeNewTextType('ddd', 'DDDDDDD', xeCData));
      xml.Root.NodeAdd(xml.NodeNewTextType('eee', 'EEEEEEE', xeCondSection));
      xml.Root.NodeAdd(xml.NodeNewTextType('fff', 'FFFFFFF', xeStylesheet));
      xml.Root.NodeAdd(xml.NodeNewTextType('ggg', 'GGGGGGG', xeDocType));
      xml.Root.NodeAdd(xml.NodeNewTextType('hhh', 'HHHHHHH', xeDtdElement));
      xml.Root.NodeAdd(xml.NodeNewTextType('iii', 'IIIIIII', xeDtdAttList));
      xml.Root.NodeAdd(xml.NodeNewTextType('jjj', 'JJJJJJJ', xeDtdEntity));
      xml.Root.NodeAdd(xml.NodeNewTextType('kkk', 'KKKKKKK', xeDtdNotation));
      xml.Root.NodeAdd(xml.NodeNewTextType('lll', 'LLLLLLL', xeInstruction));
      xml.Root.NodeAdd(xml.NodeNewTextType('mmm', 'MMMMMMM', xeCharData));
      xml.Root.NodeAdd(xml.NodeNewTextType('nnn', 'NNNNNNN', xeWhiteSpace));
      xml.Root.NodeAdd(xml.NodeNewTextType('ooo', 'OOOOOOO', xeQuotedText));
    
      Memo1.Text := xml.WriteToString;
      xml.Free;
    end;
    {***************************************
    <?xml version="1.0" encoding="UTF-8"?>
    <List bbb="BBBBBBB"NNNNNNN>
    	<aaa>AAAAAAA</aaa>
    	<!--CCCCCCC-->
    	<![CDATA[DDDDDDD]]>
    	<!--EEEEEEE-->
    		<?xml-stylesheetFFFFFFF?>
    
     [
    GGGGGGG]>
    <!ELEMENT hhh HHHHHHH>
    <!ATTLIST iii IIIIIII>
    <!ENTITY jjj JJJJJJJ>
    <!NOTATION kkk KKKKKKK>
    		<?LLLLLLL?>
    
    	MMMMMMM
    	"OOOOOOO"
    </List>
    *****************************************}

     

    九、NativeXml 读取

    NativeXml打开xml文本

    Var

    Xml:TnativeXml;

    Begin

    Xml:=TnativeXml.Create(nil);

    Xml.loadformFile(‘xml_file_path’);

    End;

    读取declaration

      declaration: TsdDeclaration;

      declaration := xml.RootNodes[0] as TsdDeclaration;

      ShowMessageFmt('%s, %s', [declaration.Version, declaration.Encoding]);  //1.0, UTF-8

    节点信息以及读取

    xml.RootNodes[1]代表根节点,与xml.Root相同

    xml.Root.Name:根节点名称

    xml.Root.NodeCount:根节点子节点的总数

    xml.Root.WriteToString读取根节点下的所有内容

    xml.Root[0]:第一个子节点

  • 相关阅读:
    Js:返回上一页
    select设置选中项/select的级联
    js闭包
    bootstrap
    My97DatePicker使用 、layDate 日期与时间组件
    IO流
    java之String工具类和File类
    Python学习之路文件file常用方法
    Python学习之路-集合set的常用方法
    Python学习之路-字典外传
  • 原文地址:https://www.cnblogs.com/chenmfly/p/4535552.html
Copyright © 2020-2023  润新知