Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed SelectNode, added XMLLoadFromFile overload #15

Merged
merged 1 commit into from
Dec 15, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 40 additions & 16 deletions OmniXMLUtils.pas
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
}
Expand Down Expand Up @@ -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 }
Expand Down Expand Up @@ -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 }

Expand Down