在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
Delphi中解析Xml的控件-SimDesign NativeXml
正在学习,感觉应用很方便。无源代码的版本还是免费的。
SimDesign.NativeXml是一个delphi和bcb的XML控件,包含一个精巧的Delphi XML执行,使你能在程序中读写XML文档。 http://www.simdesign.nl/xml.html
网上有一个利用SimDesign.NativeXml生成XML的例子。
一个更加强大易用的XML库 -- NativeXML
原来一直使用Delphi自带的TXMLDocument类来进行XML文件的读写,虽然麻烦了一点,但功能上来说还不错。只是有一个地方让我实在不舒服 - 每次创建TXMLDocument类实例的时候必须传入TComponent对象,这就意味着,如果我把对XML文件的读写封装到一个类里面,那么在创建这个自定义类的时候就必须也传入TComponent对象。
我尝试过很多方法,但是都无法避免,最后试着上网找了找,于是就找到了NativeXML这个库。 下载之后马上打开Demo看了看,cool,创建TNativeXML的时候只需要传入xml文件路径就可以,再往下看就让我开始惊喜了。它已经把大多数操作都封装好了,而且还具有把任意对象序列化的能力。
比如,你能把整个Form通过 TsdXmlObjectWriter = class(TPersistent) 类存入一个XML文件,下次再读取。这样使得远程传输对象变得很简单了。
下面就是一个使用NativeXML库的例子:
目标XML结构: <bookshift> <book author = "test_author"> <date>2000-01-01</date> </book> </bookshift> delphi 代码: procedure WriteTest; var xml : TNativeXML; n_bs : TXMLNode; begin // 建立根节点
xml := TNativeXml.CreateName('bookshift'); xml.EncodingString := 'GB2312'; // 输出风格为易读 xml.XmlFormat := xfReadable; // 建立Book节点 n_bs := xml.Root.NodeNew('book'); // 写入Book节点的属性 n_bs.WriteAttribuiteString('author','test_author'); // 建立Book节点下属date节点并写入值 n_bs.WriteString('date','2000-01-01'); xml.SaveToFile('test.xml'); end;
打开test.xml文件看看吧,和上面的格式一摸一样。很简单吧,我用这个库重写了原来的XML访问类,大约节省了40%的代码量。SimDesign.NativeXml自带的读取XML文档的例子。 注意:以下代码转贴时有误,未经测试 { Example 3 Demo unit that shows how to import XML data into record structure. It is a "real-life" example written for one of our customers. It shows how to use the method "NodesByName", creating a temporary list of node elements with the same name. It uses NativeXml to handle the conversion. This was the original request for the conversion: " Data should be placed in a record with this structure: type FieldRecord= record FieldId: String; FieldReq: String; FieldType: String; FieldCap: String; DependField: String; MetaType: String; MetaData: String; End;
De enums and the hashtables must be stored in MetaData, in the following manner:
Enums: enumvalue _id1 + ',' + enum_caption1 + ";" + enumvalue _id2 + ',' + enum_caption2 + ";" + etc..... HashTable: codeid1 + ',' + hashdata1 + ';' + codeid2 + ',' + hashdata2 + etc......
If there's no MetaData the field should remain empty. "
Author: Nils Haeck, Simdesign
http://www.simdesign.nl More info at:
http://www.simdesign.nl/xml.html } unit Example3Main;
interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, NativeXml, ComCtrls; type // This is the record structure FieldRecord= record FieldId: String; FieldReq: String; FieldType: String; FieldCap: String; DependField: String; MetaType: String; MetaData: String; end;
////////////////////////////////////////////////////////////////// TForm1 = class(TForm) mmXML: TMemo; btnImportXML: TButton; lbStatus: TLabel; Label1: TLabel; lvTable: TListView; Label2: TLabel; Label3: TLabel; procedure btnImportXMLClick(Sender: TObject); procedure lvTableData(Sender: TObject; Item: TListItem); private public Fields: array of FieldRecord; // This is a dynamic array of records procedure LoadFieldFromNode(var AField: FieldRecord; ANode: TXmlNode); end; var Form1: TForm1; implementation {$R *.DFM} procedure TForm1.btnImportXMLClick(Sender: TObject); // Read XML from TMemo and convert into record structure var i: integer; ADoc: TNativeXml; ANode: TXmlNode; AList: TList; begin ADoc := TNativeXml.Create; try // Read from memo ADoc.ReadFromString(mmXML.Text); // A temporary list to hold references to the record elements AList := TList.Create; try ANode := ADoc.Root.NodeByName('fields'); if not assigned(ANode) then exit; // List of nodes that are named "field" ANode.NodesByName('field', AList); // Set dynamic array length SetLength(Fields, AList.Count); // Import each element for i := 0 to AList.Count - 1 do LoadFieldFromNode(Fields[I], AList[I]); // Show table lvTable.Items.Count := AList.Count; lvTable.Invalidate; // Status lbStatus.Caption := Format('Imported %d records', [AList.Count]); finally AList.Free; end; finally ADoc.Free; end; end; procedure TForm1.LoadFieldFromNode(var AField: FieldRecord; ANode: TXmlNode); // Load one field from the XML element ANode var i: integer; AMeta: TXmlNode; AEnum: TXmlNode; AList: TList;
全部评论
专题导读
热门推荐
热门话题
阅读排行榜
|
请发表评论