zoukankan      html  css  js  c++  java
  • Lazarus Reading XML- with TXMLDocument and TXPathVariable

    也就是使用XPath的方式,具体语法规则查看http://www.w3school.com.cn/xpath/xpath_syntax.asp,说明得相当详细。这里列举例子是说明在Lazarus/FPC下具体应用于实现,以及注意事项。首先可以构建一个“ReadXPath”的函数方便调用。毕竟每次使用EvaluateXPathExpression后还有些任务要处理……。

    1. function ReadXPath(const aNode: TDOMNode; const aPath: string): TDOMNode;   
    2. var  
    3.   rv: TXPathVariable;   
    4.   tl: TFPList;   
    5. begin  
    6.   Result := nil;   
    7.   if Assigned(aNode) then  
    8.   begin  
    9.     rv := EvaluateXPathExpression(aPath, aNode);   
    10.     if Assigned(rv) then  
    11.     begin  
    12.       tl := rv.AsNodeSet;   
    13.       if Assigned(tl) then  
    14.       begin  
    15.         if tl.Count > 0 then  
    16.         begin  
    17.           Result := TDOMNode(tl[0]);   
    18.         end;   
    19.       end;   
    20.     end;   
    21.   end;   
    22. end;   

    具体使用了,要记住返回的其实是“元素”,就算强制约定了“属性”——[@Attrib],所以要读取任何值,都要按“扫描到元素”的方式来处理。

    1. function ReadCFG: boolean;   
    2. var  
    3. .....   
    4.   vConfigXml: string = '';   
    5.   HistoryPath: string = '';   
    6.   TracePath: string = '';   
    7.   vXP: TDOMNode;   
    8. .....   
    9. begin  
    10.   Result := False;   
    11.   ReadXMLFile(xmlCfg, vConfigXml);   
    12.   vXP := ReadXPath(xmlCfg, '/Config/HistoryPath[@value]');   
    13.   if Assigned(vXP) then  
    14.   begin  
    15.     if vXP.HasAttributes then  
    16.       HistoryPath := vXP.Attributes.Item[0].NodeValue;   
    17.   end;   
    18.   vXP := ReadXPath(xmlCfg, '/Config/TracePath[@value]');   
    19.   if Assigned(vXP) then  
    20.   begin  
    21.     if vXP.HasAttributes then  
    22.       TracePath := vXP.Attributes.Item[0].NodeValue;   
    23.   end;   
    24.   
    25.   if (HistoryPath <> ''and (TracePath <> ''then  
    26.       .....   
    27.   
    28. end;       
  • 相关阅读:
    vs2010在进行数据架构比较时报'text lines should not be null'错误
    idea安装2019
    无法获得VMCI驱动程序的版本:句柄无效
    VMware对虚拟机快照进行克隆
    此主机支持Intel VT-x,但Intel VT-x处于禁用状态
    惠普台式机,如何开启虚拟化技术
    eclipse格式化代码快捷键失效
    python之shutil模块
    hadoop中hive常用的交互式操作
    hadoop中hive的属性
  • 原文地址:https://www.cnblogs.com/hieroly/p/5204264.html
Copyright © 2011-2022 走看看