{++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// cleanup_oxml
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
Procedure cleanup_oxml;
begin
FreeAndNil(FXPathResults);
end;
{++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// prepare_oxml
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
Procedure prepare_oxml(xml_file:
string);
begin
FXMLDocument := CreateXMLDoc;
FXMLDocument.WriterSettings.IndentType := itIndent;
if not FXMLDocument.LoadFromFile(xml_file)
then
raise Exception.Create('
Source xml document is not valid');
FXPathResults := TStringList.Create;
end;
{++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// xpath_oxml (uses OXmlPDOM, OXmlUtils)
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
Procedure xpath_oxml(xml_file,expression:
string;amemo:Tmemo;mode:integer);
Var
expressionList:
string;
iNode : integer;
nodeList : IXMLNodeList;
p : integer;
queryNode : PXMLNode;
validResult : boolean;
Attribute : PXMLNode;
ForceNode : PXMLNode;
ForceTarget : PXMLNode;
begin
try
prepare_oxml(xml_file);
queryNode := FXMLDocument.DocumentElement;
expressionList := expression;
amemo.Clear;
repeat
p := Pos('
+', expressionList);
if p > 0
then
begin
expression := TrimRight(Copy(expressionList, 1, p-1));
Delete(expressionList, 1, p);
expressionList := TrimLeft(expressionList);
end
else
expression := expressionList;
nodeList := queryNode.SelectNodes(expression);
ForceNode := queryNode.SelectNode(expression);
if nodeList.Count > 0
then
queryNode := nodeList[0]
else
queryNode :=
nil;
until p = 0;
if mode = 2
then
begin
//iterate through all attributes -> you MUST set the node to nil
Attribute :=
nil;
//showmessage(ForceNode.NodeName);
if ForceNode.HasChildNodes
then
begin
showmessage(ForceNode.NodeName +'
has ChildNodes');
ForceTarget := ForceNode.NextSibling;
Showmessage(ForceTarget.NodeName);
end;
while ForceNode.GetNextAttribute(Attribute)
do
amemo.Lines.Add(ForceNode.NodeName+'
['+
Attribute.NodeName+'
] = '+
Attribute.NodeValue);
end;
for iNode := 0
to nodeList.Count-1
do
begin
if mode = 0
then amemo.Lines.Add(nodeList[iNode].XML);
if mode = 1
then amemo.Lines.Add(nodeList[iNode].NodeName);
end;
finally
cleanup_oxml;
end;
end;