登录 用户中心() [退出] 后台管理 注册
   
您的位置: 首页 >> 程序员学前班[不再更新,只读] >> 主题: [CLQ的Delphi大全教程] xml 操作及其误区     [回主站]     [分站链接]
标题
[CLQ的Delphi大全教程] xml 操作及其误区
clq
浏览(4) + 2007-04-08 19:54:05 发表 编辑

关键字:

[CLQ的Delphi大全教程] xml 操作及其误区

[以下环境均为 delphi 7]

我对xml一向不以为然,不过也实在没有什么更好的处理方式.毕竟它已经是被接受的标准.

delphi对xml的操作主要由 TXMLDocument 控件来完成. 首先说解码,与通常的delphi控件的简洁易懂不同,这个控件实在是通用,不看帮助我一般都用不下去 :) .我们参照的帮助中并没有例子,它的例子在 delphi 的帮助"Working with XML nodes"主题中,要点几次链接才找得到.为我方便大家我就贴在这里吧

--------------------------------------------------
DevGuide: Creating Internet and Intranet Applications
Working with XML nodes

Topic Groups See Also

Once an XML document has been parsed by a DOM implementation, the data it represents is available as a hierarchy of nodes. Each node corresponds to a tagged element in the document. For example, given the following XML:





Borland
15.375
BORL
100


Pfizer
42.75
PFE
25



TXMLDocument would generate a hierarchy of nodes as follows: The root of the hierarchy would be the StockHoldings node. StockHoldings would have two child nodes, which correspond to the two Stock tags. Each of these two child nodes would have four child nodes of its own (name, price, symbol, and shares). Those four child nodes would act as leaf nodes. The text they contain would appear as the value of each of the leaf nodes.

Note

This division into nodes differs slightly from the way a DOM implementation generates nodes for an XML document. In particular, a DOM parser treats all tagged elements as internal nodes. Additional nodes (of type text node) are created for the values of the name,
price, symbol, and shares nodes. These text nodes then appear as the children of the name, price, symbol, and shares nodes.

Each node is accessed through an IXMLNode interface, starting with the root node, which is the value of the XML document component's DocumentElement property.

Working with a node's value

Given an IXMLNode interface, you can check whether it represents an internal node or a leaf node by checking the IsTextElement property.

If it represents a leaf node, you can read or set its value using the Text property.
If it represents an internal node, you can access its child nodes using the ChildNodes property.

Thus, for example, using the XML document above, you can read the price of Borland's stock as follows:

BorlandStock := XMLDocument1.DocumentElement.ChildNodes[0];
Price := BorlandStock.ChildNodes['price'].Text;

Working with a node's attributes

If the node includes any attributes, you can work with them using the Attributes property. You can read or change an attribute value by specifying an existing attribute name. You can add new attributes by specifying a new attribute name when you set the Attributes property:

BorlandStock := XMLDocument1.DocumentElement.ChildNodes[0];
BorlandStock.ChildNodes['shares'].Attributes['type'] := 'common';

Adding and deleting child nodes

You can add child nodes using the AddChild method. AddChild creates new nodes that correspond to tagged elements in the XML document. Such nodes are called element nodes.

To create a new element node, specify the name that appears in the new tag and, optionally, the position where the new node should appear. For example, the following code adds a new stock listing to the document above:

var
NewStock: IXMLNode;
ValueNode: IXMLNode;
begin
NewStock := XMLDocument1.DocumentElement.AddChild('stock');
NewStock.Attributes['exchange'] := 'NASDAQ';
ValueNode := NewStock.AddChild('name');
ValueNode.Text := 'Cisco Systems'
ValueNode := NewStock.AddChild('price');
ValueNode.Text := '62.375';
ValueNode := NewStock.AddChild('symbol');
ValueNode.Text := 'CSCO';
ValueNode := NewStock.AddChild('shares');
ValueNode.Text := '25';

end;

An overloaded version of AddChild lets you specify the namespace URI in which the tag name is defined.

You can delete child nodes using the methods of the ChildNodes property. ChildNodes is an IXMLNodeList interface, which manages the children of a node. You can use its Delete method to delete a single child node that is identified by position or by name. For example, the following code deletes the last stock listed in the document above:

StockList := XMLDocument1.DocumentElement;
StockList.ChildNodes.Delete(StockList.ChildNodes.Count - 1);

--------------------------------------------------
别以为看了这个帮助就一切ok了.如果你在线程里面调用这个控件它就会提示您某某没有初始化的错误,原因是这个控件默认是用微软的ole控件来完成功能的 -- 也就是它的 DOMVendor 属性默认为 MSXML.这个就是它的xml处理引擎.所以如果你在windows环境下要在线程中写上 ComObj 单元中的ole初始化函数 OleInitialize(nil).

在linux下,也就是kylix下.(我用的是可以直接编译delphi 7 的clx代码的 kylix3 .)您还有更多的麻烦,这里的默认引擎已经是 Xerces XML 了,对于有回车换行的xml它好象都会出错(这和java中差不多).那好,我们都去掉回车吧,这下没问题了吧.且慢,您还不能直接使用中文.因为这个控件的默认编码是utf8,用中文它可会报错哦.

所以您的xml中还在说明中文的字符集,比如这样


\

注意!是gb2312,如果是gbk,对不起它不引啦.

所以说这些xml中的细微之处实际上造成了xml文本的不通用,也许您要那我就要个通用的,都用 Xerces XML 引擎好了.这个当然也可以,不过在windows下这是调用一个C语言的开源包实现的,您一定要带在一个dll,这个dll是什么除了我之外还真没别人会告诉您,那就是 xercesxmldom.dll 在安装了delphi 7 的系统中会存在于您的系统目录中,所以您用delphi写好的程序一定要在干净的机器上测试过--可惜我自己经常由于懒惰不这样干,惭愧呀惭愧 :)

山翼
2007-5-17 23:54:52 发表 编辑

我生成的文件(Open XML格式的)总在这句后面加了个回车#10#00

而应用的系统却不认这种,如何去掉回车?


总数:1 页次:1/1 首页 尾页  
总数:1 页次:1/1 首页 尾页  


所在合集/目录



发表评论:
文本/html模式切换 插入图片 文本/html模式切换


附件:



NEWBT官方QQ群1: 276678893
可求档连环画,漫画;询问文本处理大师等软件使用技巧;求档softhub软件下载及使用技巧.
但不可"开车",严禁国家敏感话题,不可求档涉及版权的文档软件.
验证问题说明申请入群原因即可.

Copyright © 2005-2020 clq, All Rights Reserved
版权所有
桂ICP备15002303号-1