Lazarus Reading XML- with TXMLDocument and TXPathVariable
2016-02-20 23:33
525 查看
也就是使用XPath的方式,具体语法规则查看http://www.w3school.com.cn/xpath/xpath_syntax.asp,说明得相当详细。这里列举例子是说明在Lazarus/FPC下具体应用于实现,以及注意事项。首先可以构建一个“ReadXPath”的函数方便调用。毕竟每次使用EvaluateXPathExpression后还有些任务要处理……。
view plaincopy to clipboardprint
function ReadXPath(const aNode: TDOMNode; const aPath: string): TDOMNode;
var
rv: TXPathVariable;
tl: TFPList;
begin
Result := nil;
if Assigned(aNode) then
begin
rv := EvaluateXPathExpression(aPath, aNode);
if Assigned(rv) then
begin
tl := rv.AsNodeSet;
if Assigned(tl) then
begin
if tl.Count > 0 then
begin
Result := TDOMNode(tl[0]);
end;
end;
end;
end;
end;
具体使用了,要记住返回的其实是“元素”,就算强制约定了“属性”——[@Attrib],所以要读取任何值,都要按“扫描到元素”的方式来处理。
view plaincopy to clipboardprint
function ReadCFG: boolean;
var
.....
vConfigXml: string = '';
HistoryPath: string = '';
TracePath: string = '';
vXP: TDOMNode;
.....
begin
Result := False;
ReadXMLFile(xmlCfg, vConfigXml);
vXP := ReadXPath(xmlCfg, '/Config/HistoryPath[@value]');
if Assigned(vXP) then
begin
if vXP.HasAttributes then
HistoryPath := vXP.Attributes.Item[0].NodeValue;
end;
vXP := ReadXPath(xmlCfg, '/Config/TracePath[@value]');
if Assigned(vXP) then
begin
if vXP.HasAttributes then
TracePath := vXP.Attributes.Item[0].NodeValue;
end;
if (HistoryPath <> '') and (TracePath <> '') then
.....
end;
view plaincopy to clipboardprint
function ReadXPath(const aNode: TDOMNode; const aPath: string): TDOMNode;
var
rv: TXPathVariable;
tl: TFPList;
begin
Result := nil;
if Assigned(aNode) then
begin
rv := EvaluateXPathExpression(aPath, aNode);
if Assigned(rv) then
begin
tl := rv.AsNodeSet;
if Assigned(tl) then
begin
if tl.Count > 0 then
begin
Result := TDOMNode(tl[0]);
end;
end;
end;
end;
end;
具体使用了,要记住返回的其实是“元素”,就算强制约定了“属性”——[@Attrib],所以要读取任何值,都要按“扫描到元素”的方式来处理。
view plaincopy to clipboardprint
function ReadCFG: boolean;
var
.....
vConfigXml: string = '';
HistoryPath: string = '';
TracePath: string = '';
vXP: TDOMNode;
.....
begin
Result := False;
ReadXMLFile(xmlCfg, vConfigXml);
vXP := ReadXPath(xmlCfg, '/Config/HistoryPath[@value]');
if Assigned(vXP) then
begin
if vXP.HasAttributes then
HistoryPath := vXP.Attributes.Item[0].NodeValue;
end;
vXP := ReadXPath(xmlCfg, '/Config/TracePath[@value]');
if Assigned(vXP) then
begin
if vXP.HasAttributes then
TracePath := vXP.Attributes.Item[0].NodeValue;
end;
if (HistoryPath <> '') and (TracePath <> '') then
.....
end;
相关文章推荐
- 用邻接表实现无向图
- 大话设计模式策略模式_多种方法实现商场促销
- oracle数据库中日期函数的用法
- java动态代理学习
- Spring多数据源的配置和使用
- Command rejected: FastEthernet0/1 is a dynamic port
- neuq oj 1049: 谭浩强C语言(第三版)习题6.5 C语言
- 四中方式实现单例模式
- OS X_常用快捷键
- java中continue配合标签进行继续循环
- 安装wordpress常见问题
- myeclipse部署多个TOMCAT
- 学无涯,海无涯,学海无涯
- 涉及基本类型的重载
- ubuntu终端怎么打开
- 《从0到1》
- 读书笔记 -《Python 黑帽子》 ( 五 )
- UPX-windows版本的编译
- TCP-IP学习笔记-- 浅析TCP(1)
- Spark的性能调优