在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
Delphi操作XMl,只要使用 NativeXml。我是用的版本是4.02。NativeXML的使用方法比较简单,但是功能很强大。 XE2的话,要在simdesign.inc后面加上: // Delphi XE2 / 16 {$ifdef VER230} {$define D7UP} {$define D10UP} {$define D15UP} {$endif} 一、使用 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:\Temp\xml_test_files\basic.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:\Temp\xml_test_files\CDATA_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: 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, ' |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论