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;       
  • 相关阅读:
    png图片在ie不透明的解决方案
    ASP如何查询ACCESS数据库中上一周的所有记录
    使用FSO修改文件夹的名称
    两组字符串数据比较合并相同数据
    1272 并查集
    1232 并查集
    What's New in ASP.NET 4.5 and Visual Web Developer 11 Developer Preview
    knockout.js
    .net training
    Unknown server tag 'asp:ListView'. sharepoint
  • 原文地址:https://www.cnblogs.com/hieroly/p/5204264.html
Copyright © 2011-2022 走看看