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;       
  • 相关阅读:
    Filter ,Interceptor,AOP
    React路由官方网站
    React的UI库以及国内镜像
    HBuilder+个推 实现app推动消息
    基于create-react-app再次配置
    使用 ES7 的 async/await 时报错—Uncaught ReferenceError: regeneratorRuntime is not defined
    微信小程序学习网站
    我的github地址
    如何在github搭建自己的项目
    Vue简单封装axios—解决post请求后端接收不到参数问题
  • 原文地址:https://www.cnblogs.com/hieroly/p/5204264.html
Copyright © 2011-2022 走看看