任何人都有使用OMNIXML加载xml的示例代码 [英] Anyone has sample code to load an xml using OMNIXML
问题描述
我正在寻找一些实际的工作示例代码,使用普通的OmniXml(或OmniXmlUtils)单元读取XML。我不想使用TOmniXMLWriter类,如其他地方所述,但是想使用普通的Delphi代码加载一个xml文件。
I am looking for some actual working sample code to read an XML using the plain OmniXml (or OmniXmlUtils) unit. I don't want to use the TOmniXMLWriter class , as explained elsewhere , but want to load an xml file using plain Delphi code.
我搜索了OmniXml站点,但是样本
I searched the OmniXml site but the samples there are as good as non-existing.
提前感谢。
推荐答案
从文件或流或字符串加载文档
Loading the document from file or from stream or string
xml := CreateXMLDoc
xml.Load(FileName);
xml.LoadFromStream(XMLAsStream);
xml.LoadXML(XMLAsString)
选择单个节点(第5个小孩) p>
Selecting a single node (5th child):
MyNode := XML.ChildNodes.Item[4];
MyNode := XML.SelectSingleNode('Node[5]');
使用XPath选择
MyNode := XML.SelectSingleNode('/MyNodes/SpecificNodes/Node[5]');
选择节点收集
MyNodes := XML.SelectNodes('/MyNodes/SpecificNodes/Node');
如果您从 http://www.omnixml.com/download.html ,那里有一个名为demo的目录,其中包含所有演示文稿。他们将几乎解释你的一切。
If you downloaded from http://www.omnixml.com/download.html then there is a directory called demo inside, that contains all the demos. They will explain almost everything to you.
如果您决定在OmniXML之上使用SimpleStorage,请使用SimpleStorage向您展示您的上层示例。 p>
If you decide to use SimpleStorage on top of OmniXML, let me show you how your upper example would look, using SimpleStorage.
xml := StorageFromFile(rssFileName)
for channel in xml.Elements('channel') do
begin
ListBox1.Items.Add('['+channel.Get('title')AsStringDef+']')
for Item in channel.Elements('item') do
ListBox1.Items.Add(' <'+ Item .Get('title')AsStringDef+'>')
end; //for iChannel
没有看到有多少样板代码已经消失(21行代码缩小到7为相同的功能)。没有必要检查节点是否存在等等,并且枚举器有很大的帮助。我强烈建议您使用这种方法,因为它更清晰。
No do you see how much boilerplate code was gone (21 lines of code shrunk to 7 for the same functionality). There is no need to check if the node exists etc.. and enumerators help greatly. I strongly suggest you use this approach because it is so much clearer.
这篇关于任何人都有使用OMNIXML加载xml的示例代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!