delphi读取xml文件_使用Delphi读取和处理XML文件(RSS提要)
delphi读取xml文件
博客? 联合组织? ( Blog? Syndication? )
Depending upon who you talk to, a blog is a personal Web diary, a collection of short, dated discussions with commentary, or a way of publishing news and information. Well, the About Delphi Programming Home page acts as a blog.
根据与您交谈的人的不同,博客是个人的网络日记,简短的,过时的讨论集(包括评论)或发布新闻和信息的方式。 好吧,“关于Delphi编程主页”充当博客。
The Stay Up-To-Date page hosts the link to the the XML file that can be used for Really Simple Syndication (RSS).
保持最新页面托管指向XML文件的链接,该文件可用于Really Simple Syndication(RSS)。
关于Delphi编程博客提要 ( About Delphi Programming Blog Feed )
Now about parsing the XML file that lists the latest additions to this site.
现在有关解析XML文件,该文件列出了该站点的最新内容。
Here are the basics of the About Delphi Programming RSS:
以下是关于Delphi编程RSS的基础知识:
- It is XML. This means it must be well-formed, include a prolog and DTD, and all elements must be closed. 它是XML。 这意味着它必须格式正确,包括序言和DTD,并且所有元素都必须关闭。
- The first element in the document is the element. This includes a mandatory version attribute. 文档中的第一个元素是元素。 这包括必需的版本属性。
- The next element is the element. This is the main container for all RSS data. 下一个元素是元素。 这是所有RSS数据的主要容器。
- The element is the title, either of the entire site (if it's at the top) or of the current item (if it's within an ). 元素是整个网站(如果位于顶部)或当前项目(如果位于中)的标题。
- The element indicates the URL of the Web page that corresponds to the RSS feed, or if it's within an , the URL to that item. 元素指示与RSS提要相对应的网页的URL,或者如果该元素位于内,则指向该URL。
- The element describes the RSS feed or the item. 元素描述RSS提要或项目。
- The element is the meat of the feed. These are all the headlines (), URL () and description () that will be in your feed. 元素是饲料的肉。 这些都是您的Feed中的所有标题(),URL()和description()。
TXMLDocument组件 ( The TXMLDocument Component )
To be able to display the latest headlines inside a Delphi project, you first need to download the XML file. Since this XML file is updated on a day by day basic (new entries added) you'll need code designed to save the contents of a specified URL to a file.
为了能够显示Delphi项目中的最新标题,您首先需要下载XML文件。 由于此XML文件每天都会进行基本更新(添加了新条目),因此您需要使用代码来将指定URL的内容保存到文件中。
TXMLDocument组件 ( The TXMLDocument component )
In general, here are the steps that describe how to use TXMLDocument:
通常,以下是描述如何使用TXMLDocument的步骤:
- Add a TXMLDocument component to your form. 将TXMLDocument组件添加到您的窗体。
- If the XML document is stored in a file, set the FileName property to the name of that file. 如果XML文档存储在文件中,请将FileName属性设置为该文件的名称。
- Set the Active property to True. 将活动属性设置为True。
- The data XML represents is available as a hierarchy of nodes. Use methods designed to return and work with a node in an XML document (like ChildNodes.First). XML表示的数据可用作节点的层次结构。 使用旨在返回并使用XML文档中的节点的方法(例如ChildNodes.First)。
解析XML,Delphi方式 ( Parsing XML, Delphi way )
Create a new Delphi project and drop a TListView (Name: 'LV') component on a form. Add a TButton (Name : 'btnRefresh') and a TXMLDocument (Name : 'XMLDoc'). Next, add three columns to the ListView component (Title, Link and Description). Finally, add the code to download the XML file, parse it with TXMLDocument and display inside the ListView in the button's OnClick event handler.
创建一个新的Delphi项目并将一个TListView(名称:'LV')组件放在窗体上。 添加一个TButton(名称:“ btnRefresh”)和一个TXMLDocument(名称:“ XMLDoc”)。 接下来,将三列添加到ListView组件(标题,链接和描述)。 最后,添加代码以下载XML文件,使用TXMLDocument对其进行解析,并显示在按钮的OnClick事件处理程序中的ListView中。
Below you can find the portion of that code.
您可以在下面找到该代码的一部分。
var
StartItemNode : IXMLNode;
ANode : IXMLNode;
STitle, sDesc, sLink : WideString;
begin
...
//points to local XML file in "original" code
XMLDoc.FileName := 'http://0.tqn.com/6/g/delphi/b/index.xml';
XMLDoc.Active:=True;
StartItemNode := XMLDoc.DocumentElement.ChildNodes.First.ChildNodes.FindNode('item') ;
ANode := StartItemNode;
repeat
STitle := ANode.ChildNodes['title'].Text;
sLink := ANode.ChildNodes['link'].Text;
sDesc := ANode.ChildNodes['description'].Text;
//add to list view
with LV.Items.Add do
begin
Caption := STitle;
SubItems.Add(sLink) ;
SubItems.Add(sDesc)
end;
ANode := ANode.NextSibling;
until ANode = nil;
完整的源代码 ( Full Source Code )
I suppose the code is more or less easy to understand:
我认为代码或多或少易于理解:
- Make sure FileName property of the TXMLDocument points to our XML file. 确保TXMLDocument的FileName属性指向我们的XML文件。
- Set Active to True 设置为真
- Find the first ("meat") node 找到第一个(“肉”)节点
- Iterate through all the nodes and grab the information they cary. 遍历所有节点并获取他们关心的信息。
- Add each node's value to ListView 将每个节点的值添加到ListView
Maybe only the next line can be confusing: StartItemNode := XMLDoc.DocumentElement.ChildNodes.First.ChildNodes.FindNode('item') ;
也许只有下一行会造成混淆:StartItemNode:= XMLDoc.DocumentElement.ChildNodes.First.ChildNodes.FindNode('item');
The DocumentElement property of the XMLDoc provides access to the root node of the document. This root node is the element. Next, ChildNodes.First returns the only child node to the element, which is the node. Now, ChildNodes.FindNode('item') finds the first "meat" node. Once we have the first node we simply iterate through all the "meat" nodes in the document. The NextSibling method returns the next child of a node’s parent.
XMLDoc的DocumentElement属性提供对文档根节点的访问。 该根节点是元素。 接下来,ChildNodes.First将唯一的子节点返回到元素,即该节点。 现在,ChildNodes.FindNode('item')找到第一个“肉”节点。 一旦有了第一个节点,我们就可以简单地遍历文档中的所有“肉”节点。 NextSibling方法返回节点父级的下一个子级。
That's it. Make sure you download the full source. And of course, feel free and encouraged to post any comments to this article on our Delphi Programming Forum.
而已。 确保下载完整的源代码。 当然,随时欢迎并鼓励在我们的Delphi编程论坛上发表对本文的任何评论。
翻译自: https://www.thoughtco.com/reading-manipulating-xml-files-rss-feeds-1058140
delphi读取xml文件
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)