相关方法与属性:
TNativeXml.Create(...);
TNativeXml.CreateEx(...);
TNativeXml.CreateName(...);
Obj.New;
Obj.ReadFromString(...);
Obj.VersionString;
Obj.EncodingString;
测试代码:
uses NativeXml; //! const xmlFile = 'C:\Temp\xml_test_files\basic.xml'; //测试文件 {使用 Create 建立时可指定属主并随属主一同释放} procedure TForm1.Button1Click(Sender: TObject); var xml: TNativeXml; begin xml := TNativeXml.Create(Self); // xml := TNativeXml.Create(nil); // System.ReportMemoryLeaksOnShutdown := True; // xml.Free; end; {CreateEx} procedure TForm1.Button2Click(Sender: TObject); var xml: TNativeXml; begin xml := TNativeXml.CreateEx(False, //是否建立 Declaration 对象, 如: True, //是否建立跟节点 nil //指定属主 ); ShowMessageFmt('version: %s', [xml.VersionString]); //显示 XML 版本 ShowMessageFmt('encoding: %s', [xml.EncodingString]); //显示 XML 编码 xml.Free; xml := TNativeXml.CreateEx(True, true, nil); ShowMessageFmt('version: %s', [xml.VersionString]); //1.0 ShowMessageFmt('encoding: %s', [xml.EncodingString]); //UTF-8 xml.Free; end; {使用 CreateName 建立可直接指定根节点的名称} procedure TForm1.Button3Click(Sender: TObject); var xml: TNativeXml; begin xml := TNativeXml.CreateName('MyRoot'); ShowMessage(xml.Root.Name); //MyRoot xml.Free; end; {使用 New 方法清空并重新构建} procedure TForm1.Button4Click(Sender: TObject); var xml: TNativeXml; begin xml := TNativeXml.Create(nil); xml.LoadFromFile(xmlFile); ShowMessage(xml.WriteToString); xml.New; // xml.Clear; ShowMessage(xml.WriteToString); xml.Free; end; {使用 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;