That is, the use of XPath, the specific syntax rules to see http://www.w3school.com.cn/xpath/xpath_syntax.asp, the explanation is quite detailed. Examples are given here to illustrate the specific application of the LAZARUS/FPC, as well as considerations. First, you can construct a "readxpath" function for easy invocation. After all, there are some tasks to be dealt with after each use of 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 ;
Specifically used, to remember that the return is actually "element", even if the mandatory Convention "property"--[@Attrib], so to read any value, you have to press "scan to Element" way to handle.
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 <> c7> ") Then
- .....
- End ;
Lazarus Reading xml-with txmldocument and Txpathvariable