• 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;       
  • 相关阅读:
    B-Tree(B树)原理及C++代码实现
    Select(快速选择顺序统计量)原理及C++代码实现
    BucketSort(桶排序)原理及C++代码实现
    RadixSort(基数排序)原理及C++代码实现
    CountingSort(计数排序)原理及C++代码实现
    面向对象之封装
    今日算法题
    面向对象之抽象类和接口
    面向对象之多态
    今日算法题
  • 原文地址:https://www.cnblogs.com/hieroly/p/5204264.html
Copyright © 2020-2023  润新知