Permalink
Browse files

fixed SelectNode, added XMLLoadFromFile overload

! Fixed SelectNode when names of parent and child node are the same.
+ Added XMLLoadFromFile overload returning error message.
1 parent 0963ecf commit b03d689022b60ac9470cd29417c4f1e341b1154c @gabr42 gabr42 committed Dec 14, 2016
Showing with 40 additions and 16 deletions.
  1. +40 −16 OmniXMLUtils.pas
View
@@ -2,18 +2,19 @@
different types, manipulate nodes, load/save XML documents.
@author Primoz Gabrijelcic
@desc <pre>
- (c) 2011 Primoz Gabrijelcic
+ (c) 2016 Primoz Gabrijelcic
Free for personal and commercial use. No rights reserved.
Author : Primoz Gabrijelcic
Creation date : 2001-10-25
- Last modification : 2013-12-12
- Version : 1.31
+ Last modification : 2016-12-14
+ Version : 1.31a
</pre>*)(*
History:
+ 1.31a: 2016-12-14
+ - Fixed SelectNode when names of parent and child node are the same.
1.31: 2013-12-13
- - Can be compiled without VCL (/dNOVCL). Font persistency would then
- be disabled.
+ - Can be compiled without VCL (/dNOVCL). That also disables font persistency.
- Added procedure SetNodeAttrs.
1.30: 2011-03-01
- Convert EFOpenError exception in XMLLoadFromFile to function result.
@@ -525,7 +526,12 @@ EOmniXMLUtils = class(Exception);
{:Load XML document from a file.
}
function XMLLoadFromFile(xmlDocument: IXMLDocument;
- const xmlFileName: string): boolean;
+ const xmlFileName: string): boolean; overload;
+
+ {:Load XML document from a file, returning error message on error.
+ }
+ function XMLLoadFromFile(xmlDocument: IXMLDocument; const xmlFileName: string;
+ out errorMsg: string): boolean; overload;
{:Save XML document to a file.
}
@@ -2421,21 +2427,21 @@ function FindProcessingInstruction(
}
function SelectNode(parentNode: IXMLNode; const nodeTag: string): IXMLNode;
begin
- if IsDocument(parentNode) and (assigned(DocumentElement(parentNode))) then
- Result := DocumentElement(parentNode)
- else
- Result := parentNode;
- if (nodeTag <> '') and assigned(Result) and (Result.NodeName <> nodeTag) then
- Result := Result.SelectSingleNode(nodeTag);
+ SelectNode(parentNode, nodeTag, Result);
end; { SelectNode }
function SelectNode(parentNode: IXMLNode; const nodeTag: string; var childNode: IXMLNode): boolean;
begin
- if IsDocument(parentNode) and (assigned(DocumentElement(parentNode))) then
- childNode := DocumentElement(parentNode)
+ if IsDocument(parentNode) and (assigned(DocumentElement(parentNode))) then begin
+ childNode := DocumentElement(parentNode);
+ if (nodeTag <> '') and assigned(childNode) and (childNode.NodeName = nodeTag) then begin
+ Result := true;
+ Exit;
+ end;
+ end
else
childNode := parentNode;
- if (nodeTag <> '') and assigned(childNode) and (childNode.NodeName <> nodeTag) then
+ if (nodeTag <> '') and assigned(childNode) then
childNode := childNode.SelectSingleNode(nodeTag);
Result := assigned(childNode);
end; { SelectNode }
@@ -2609,12 +2615,30 @@ procedure XMLSaveToStream(xmlDocument: IXMLDocument;
@since 2001-10-24
}
function XMLLoadFromFile(xmlDocument: IXMLDocument; const xmlFileName: string): boolean;
+var
+ errorMsg: string;
+begin
+ Result := XMLLoadFromFile(xmlDocument, xmlFileName, errorMsg);
+end; { XMLLoadFromFile }
+
+{:@param xmlDocument XML document.
+ @param xmlFileName Name of the file containing XML document.
+ @param errorMsg Empty if XML was loaded without a problem, error message instead.
+ @returns True if contents of file were successfully parsed and loaded into the
+ xmlDocument.
+ @since 2014-11-19
+}
+function XMLLoadFromFile(xmlDocument: IXMLDocument; const xmlFileName: string;
+ out errorMsg: string): boolean;
begin
+ errorMsg := '';
try
Result := xmlDocument.Load(xmlFileName);
except
- on E: EFOpenError do
+ on E: EFOpenError do begin
+ errorMsg := E.Message;
Result := false;
+ end;
end;
end; { XMLLoadFromFile }

0 comments on commit b03d689

Please sign in to comment.