Delphi XML 遍历

2024-04-08

我是德尔福新手。

我想找到每个主题下的所有作者。

这是我的 XML

<?xml version="1.0"?>
<catalog>
   <subject id="computer">
       <book id="bk101">                                                            
          <author>Gambardella, Matthew</author>                                  
          <title>XML Developer's Guide</title>                                   
          <genre>Computer</genre>                                                
          <price>44.95</price>                                                   
          <publish_date>2000-10-01</publish_date>                                
          <description>An in-depth look at creating applications                 
          with XML.</description>                                                
       </book>                                                                   
       <book id="bk112">                                                         
          <author>Galos, Mike</author>                                           
          <title>Visual Studio 7: A Comprehensive Guide</title>                  
          <genre>Computer</genre>                                                
          <price>49.95</price>                                                   
          <publish_date>2001-04-16</publish_date>                                
          <description>Microsoft Visual Studio 7 is explored in depth,           
          looking at how Visual Basic, Visual C++, C#, and ASP+ are              
          integrated into a comprehensive development                            
          environment.</description>                                             
       </book>                                                                   
   </subject>
   <subject id="literature">
       <book id="bk102">                                                         
          <author>Ralls, Kim</author>                                            
          <title>Midnight Rain</title>                                           
          <genre>Fantasy</genre>                                                 
          <price>5.95</price>                                                    
          <publish_date>2000-12-16</publish_date>                                
          <description>A former architect battles corporate zombies,             
          an evil sorceress, and her own childhood to become queen               
          of the world.</description>                                            
       </book>                                                                   
       <book id="bk103">                                                         
          <author>Corets, Eva</author>                                           
          <title>Maeve Ascendant</title>                                         
          <genre>Fantasy</genre>                                                 
          <price>5.95</price>                                                    
          <publish_date>2000-11-17</publish_date>                                
          <description>After the collapse of a nanotechnology                    
          society in England, the young survivors lay the                        
          foundation for a new society.</description>                            
       </book>                                                                   
       <book id="bk104">                                                         
          <author>Corets, Eva</author>                                           
          <title>Oberon's Legacy</title>                                         
          <genre>Fantasy</genre>                                                 
          <price>5.95</price>                                                    
          <publish_date>2001-03-10</publish_date>                                
          <description>In post-apocalypse England, the mysterious                
          agent known only as Oberon helps to create a new life                  
          for the inhabitants of London. Sequel to Maeve                         
          Ascendant.</description>                                               
       </book>                                                                   
       <book id="bk105">                                                         
          <author>Corets, Eva</author>                                           
          <title>The Sundered Grail</title>                                      
          <genre>Fantasy</genre>                                                 
          <price>5.95</price>                                                    
          <publish_date>2001-09-10</publish_date>                                
          <description>The two daughters of Maeve, half-sisters,                 
          battle one another for control of England. Sequel to                   
          Oberon's Legacy.</description>                                         
       </book>                                                                   
       <book id="bk106">                                                         
          <author>Randall, Cynthia</author>                                      
          <title>Lover Birds</title>                                             
          <genre>Romance</genre>                                                 
          <price>4.95</price>                                                    
          <publish_date>2000-09-02</publish_date>                                
          <description>When Carla meets Paul at an ornithology                   
          conference, tempers fly as feathers get ruffled.</description>         
       </book>                                                                   
       <book id="bk107">                                                         
          <author>Thurman, Paula</author>                                        
          <title>Splish Splash</title>                                           
          <genre>Romance</genre>                                                 
          <price>4.95</price>                                                    
          <publish_date>2000-11-02</publish_date>                                
          <description>A deep sea diver finds true love twenty                   
          thousand leagues beneath the sea.</description>                        
       </book>                                                                   
       <book id="bk108">                                                         
          <author>Knorr, Stefan</author>                                         
          <title>Creepy Crawlies</title>                                         
          <genre>Horror</genre>                                                  
          <price>4.95</price>                                                    
          <publish_date>2000-12-06</publish_date>                                
          <description>An anthology of horror stories about roaches,             
          centipedes, scorpions  and other insects.</description>                
       </book>                                                                   
       <book id="bk109">                                                         
          <author>Kress, Peter</author>                                          
          <title>Paradox Lost</title>                                            
          <genre>Science Fiction</genre>                                         
          <price>6.95</price>                                                    
          <publish_date>2000-11-02</publish_date>                                
          <description>After an inadvertant trip through a Heisenberg            
          Uncertainty Device, James Salway discovers the problems                
          of being quantum.</description>                                        
       </book>                                                                   
       <book id="bk110">                                                         
          <author>O'Brien, Tim</author>                                          
          <title>Microsoft .NET: The Programming Bible</title>                   
          <genre>Computer</genre>                                                
          <price>36.95</price>                                                   
          <publish_date>2000-12-09</publish_date>                                
          <description>Microsoft's .NET initiative is explored in                
          detail in this deep programmer's reference.</description>              
       </book>                                                                   
       <book id="bk111">                                                         
          <author>O'Brien, Tim</author>                                          
          <title>MSXML3: A Comprehensive Guide</title>                           
          <genre>Computer</genre>                                                
          <price>36.95</price>                                                   
          <publish_date>2000-12-01</publish_date>                                
          <description>The Microsoft MSXML3 parser is covered in                 
          detail, with attention to XML DOM interfaces, XSLT processing,         
          SAX and more.</description>                                            
       </book>                                                                   
   </subject>
</catalog>

您可以使用XPath http://www.w3.org/TR/xpath/选择所有作者,使用这样的表达式//catalog/subject/book/author,从 delphi 中,您有很多选项来管理 XML 文件,但在这种情况下,最easy是使用Microsoft XML DOM implementation http://msdn.microsoft.com/en-us/library/ms760218%28v=VS.85%29.aspx

检查这个示例应用程序

{$APPTYPE CONSOLE}

uses
  ActiveX,
  Variants,
  ComObj,
  SysUtils;

procedure ReadXMLFile(const FileName:TFileName);
const
  Msxml2_DOMDocument='Msxml2.DOMDocument.6.0';
var
  XmlDoc         : OleVariant;
  Nodes          : OleVariant;
  lNodes         : Integer;
  i              : Integer;
begin
  //create an instance to the XML DOM 
  XmlDoc       := CreateOleObject(Msxml2_DOMDocument);
  try
    XmlDoc.Async := False;
    //load the file
    XmlDoc.Load(FileName);
    //set the xpath mode
    XmlDoc.SetProperty('SelectionLanguage','XPath');
    //check for errors in the xml file
      if (XmlDoc.parseError.errorCode <> 0) then
       raise Exception.CreateFmt('Error in Xml Data %s',[XmlDoc.parseError]);

    //select the nodes with match with the expression
    Nodes := XmlDoc.selectNodes('//catalog/subject/book/author');
    //get the number of nodes selected
    lNodes:= Nodes.Length;
    Writeln(Format('%d Authors found',[lNodes]));
    //traverse the nodes
     for i:=0 to Nodes.Length - 1 do
      Writeln(Format('Author Name %s',[Nodes.Item(i).Text]));

  finally
   XmlDoc :=Unassigned;
  end;
end;


begin
 try
    CoInitialize(nil);
    try
      ReadXMLFile(ExtractFilePath(ParamStr(0))+'test.xml');
    finally
      CoUninitialize;
    end;
 except
    on E:Exception do
        Writeln(E.Classname, ':', E.Message);
 end;
 Writeln('Press Enter to exit');
 Readln;      
end.

最后应用程序将返回这个

12 Authors found
Author Name Gambardella, Matthew
Author Name Galos, Mike
Author Name Ralls, Kim
Author Name Corets, Eva
Author Name Corets, Eva
Author Name Corets, Eva
Author Name Randall, Cynthia
Author Name Thurman, Paula
Author Name Knorr, Stefan
Author Name Kress, Peter
Author Name O'Brien, Tim
Author Name O'Brien, Tim
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Delphi XML 遍历 的相关文章

  • VBA XML V6.0 如何让它等待页面加载?

    我一直在努力寻找答案 但似乎找不到任何有用的东西 基本上 我是从一个网站上拉取的 当您在该页面上时 该网站会加载更多项目 我希望我的代码在加载完成后提取最终数据 但不知道如何让 XML httprequest 等待 Edited Sub p
  • 解组转义 XML

    在 Go 中 我将如何解码此 XML 响应 我尝试过建立一个自定义UnMarshal方法在我的Answerstruct 但我运气不太好
  • 在线性布局内的 ScrollView 内并排对齐 TextView

    我有一个带有滚动视图的线性布局 我想保留它的当前格式 但只需将 textView2a 和 textView3a 并排放置 而不会破坏我当前的布局格式 我已经包含了我最近的尝试 但它们似乎不正确 提前致谢 Java菜鸟 当前有效的 XML
  • XSLT - 追加同级

    我正在尝试使用 XSLT 来附加context param作为最后一个兄弟姐妹 没有共同的父元素 因此任务有点困难 我想附加以下元素
  • XML 创建 - 错误:带有替代方案的重载方法构造函数 UnprefixedAttribute

    scala gt val count 7 count Int 7 将其放入 XML 属性中会出现错误 scala gt val x
  • XPath 中的 ., 有什么用?

    为什么在某些 XPath 表达式中句号后面使用逗号 这是一个例子 Set nlist doc selectNodes book author first name starts with M 我试图用谷歌搜索这个 但字面意思 运算符似乎不喜
  • 如何用不同的颜色绘制选定的列表框项目?

    是否可以更改 TListBox 中的项目选择焦点颜色和文本颜色 当项目中未启用主题或列表框样式设置为所有者绘制时 项目周围的选择将被涂成蓝色 我相信这是由系统的外观设置全局定义的 我想将所选项目的颜色更改为自定义颜色 举个例子 结果会是这样
  • Jackson XML ArrayList 输出具有两个包装器元素

    我在 Jackson 生成的 XML 输出中得到了两个包装器元素 我只想拥有一个 我有一个 Java bean Entity Table name CITIES JacksonXmlRootElement localName City pu
  • 删除Android所有语言中的字符串

    我有一个包含多个翻译的应用程序 我想删除一些字符串 我怎样才能重构并删除它们一次 例如在默认情况下strings xml文件并自动将删除传播到其他翻译的其他 strings xml 文件 您可以通过 Android Studio 中的 翻译
  • 从 Delphi VCL 样式获取特定字形

    我想从 VCL 样式获取特定的位图 并将其设置为按钮上的图像 它实际上是帮助问号 在位图样式编辑器中是来自表单的 btnHelp 图像 要从 VCL 样式获取视觉元素 字形 您必须使用GetElementDetails和TCustomSty
  • 将图像加载到 TImageList 并读取它们?

    我试图通过将 jpg 转换为 bmp 然后将其保存到 imagelist1 来将 jpg 加载到图像列表中 从上到下的代码片段 Selectdir 有效 fileexists 部分有效 这用于加载文件夹中的所有图像 所有图像都以 0 jpg
  • 如何从该 JAVA 文件中提取 Delphi 类以与 Android 一起使用?

    我的Delphi XE7项目需要与FTDI FT311 Android 配件芯片 http www ftdichip com Products ICs FT311D html 他们帮助提供了一个 Android 演示 其中包括他们的 JAV
  • 如何在Delphi中将对象方法作为参数传递,然后调用它?

    我担心这可能是一个有点愚蠢的问题 但这让我很难过 我正在寻找将对象的方法传递到过程中的最简单的方法 以便过程可以调用对象的方法 例如 超时后 或者可能在不同的线程中 所以基本上我想 捕获对对象方法的引用 将该引用传递给过程 使用该引用 从过
  • REXX 或 JCL 中的 XML 处理

    在 PS 文件中 我有如下 XML 数据 For ex
  • 如何使用C#在asp.net中下载xml文件

    我正在使用网络应用程序ASP NET 与 MVC3 我是 mvc3 的新手 我的网页上有一个下载按钮 当我要单击下载按钮时 我希望能够打开该 XML 文件 我尝试过一些代码更改动作结果但我没有打开文件 通过使用下面提到的代码 我收到一个下载
  • XSL字符串多重替换功能

    如何让这个函数进行多重替换 经验 替换aaa with 111并替换bbb with 222 etc
  • 将 XSD 文件转换为 C# 可序列化类

    我尝试遵循以下答案这个问题 https stackoverflow com questions 87621 how do i map xml to c objects 但无法让 xsd exe 愉快地获取 XSD 文件并将它们转换为类 此处
  • CharInSet 不适用于非英文字母?

    我已经将应用程序从 Delphi 2007 更新到 Delphi 2010 一切都很顺利 除了一条编译正常但不起作用的语句 If Edit1 Text 1 in S then ShowMessage Found else ShowMessa
  • 如何使用XPath选择非空段落?

    我想要抓取的网页具有类似的结构 每个都有一个段落是一个问题 一个段落是一个答案 我想抓取每个问题和答案并将它们存储在两个项目中 问题是 在某些页面上 问题和答案分别是 xxx p 1 and xxx p 2 但在其他页面上 xxx p 1
  • 从其可执行文件的路径获取服务名称

    我有一个可执行文件的路径 它是一个正在运行的服务应用程序 例如 C Program Files x86 Someapp somesvc exe 我想停止并启动它 为此我想我需要获取服务的名称 如下所示 this https stackove

随机推荐