Menu

[r3166]: / trunk / Src / Web.UXMLRequestor.pas  Maximize  Restore  History

Download this file

148 lines (126 with data), 3.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
{
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/
*
* Copyright (C) 2010-2012, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Implements a class that fetches XML documents from the internet via HTTP
* requests, parses the XML and provides a reference to the DOM.
}
unit Web.UXMLRequestor;
interface
uses
// Project
UExceptions, UXMLDocumentEx, Web.UHTTPEx;
type
{
TXMLRequestor:
Class that fetches XML documents from the internet via HTTP requests, parses
the XML and provides a reference to the DOM.
}
TXMLRequestor = class(TObject)
strict private
fHTTP: THTTPEx; // Object used to perform HTTP requests
function DownloadDocument(const URI: string): string;
{Downloads XML document from internet.
@param URI [in] URI of required XML document.
@return Content of XML document.
@except EXMLRequestor raised if any web error encountered.
}
function ParseXML(const XML: string): IXMLDocumentEx;
{Parses an XML document and provides reference to DOM.
@param XML [in] Document's XML as text.
@return Reference to XML document's DOM.
@except EXMLRequestor raised if error parsing XML.
}
public
constructor Create;
{Object constructor. Sets up object.
}
destructor Destroy; override;
{Object destructor. Tears down object.
}
function GetDocument(const URI: string): IXMLDocumentEx;
{Fetches XML document from internet, parses it and provides reference to
its DOM.
@param URI [in] URI of required XML document.
@return Reference to XML document's DOM.
@except EXMLRequestor raised if error in downloading or parsing XML.
}
end;
{
EXMLRequestor:
Class of exceptions raised by TXMLRequestor.
}
EXMLRequestor = class(ECodeSnip);
implementation
uses
// Delphi
XMLIntf,
// Project
Web.UExceptions;
{ TXMLRequestor }
constructor TXMLRequestor.Create;
{Object constructor. Sets up object.
}
begin
inherited Create;
fHTTP := THTTPEx.Create;
fHTTP.MediaType := 'application/xml';
fHTTP.ContentType := 'application/xml';
end;
destructor TXMLRequestor.Destroy;
{Object destructor. Tears down object.
}
begin
fHTTP.Free;
inherited;
end;
function TXMLRequestor.DownloadDocument(const URI: string): string;
{Downloads XML document from internet.
@param URI [in] URI of required XML document.
@return Content of XML document.
@except EXMLRequestor raised if any web error encountered.
}
begin
try
Result := fHTTP.GetText(URI);
except
on E: EWebError do // convert any web error to own exception type
raise EXMLRequestor.Create(E);
end;
end;
function TXMLRequestor.GetDocument(const URI: string): IXMLDocumentEx;
{Fetches XML document from internet, parses it and provides reference to its
DOM.
@param URI [in] URI of required XML document.
@return Reference to XML document's DOM.
@except EXMLRequestor raised if error in downloading or parsing XML.
}
var
XML: string; // content of downloaded XML document
begin
XML := DownloadDocument(URI);
Result := ParseXML(XML);
end;
function TXMLRequestor.ParseXML(const XML: string): IXMLDocumentEx;
{Parses an XML document and provides reference to DOM.
@param XML [in] Document's XML as text.
@return Reference to XML document's DOM.
@except EXMLRequestor raised if error parsing XML.
}
begin
try
Result := TXMLDocumentEx.Create(nil);
Result.LoadFromXML(XML);
Result.Active := True;
except
on E: EXMLDocError do // convert any XML parsing error to own exception type
raise EXMLRequestor.Create(E);
end;
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.