4 XMLIntf, XMLDoc
\82 \82½
\82è
\82Ì
\83N
\83\8d\81[
\83\93
9 //==================================================
11 //==================================================
16 //==================================================
18 //==================================================
20 //
\82í
\82¯
\82í
\82©
\82ç
\82¸
\8dì
\82Á
\82Ä
\82é
\82©
\82ç
\83o
\83O
\82¾
\82ç
\82¯
\82©
\82à
21 XMLDictionary = Record
30 FAttributeCount : Integer;
31 FChildNodes : IXMLNode;
32 FNodes : array of IXMLNode;
33 FAttributes : array of XMLDictionary;
34 function GetAttribute( const Name : string ) : string;
35 function GetNode( Index : Integer ) : IXMLNode;
38 destructor Destroy; override;
40 property NodeName : string read FNodeName write FNodeName;
41 property Attributes[ const Name : string ] : string read GetAttribute;
42 property Node[ Index : Integer ] : IXMLNode read GetNode; default;
43 property ChildNodes : IXMLNode read FChildNodes write FChildNodes;
44 property Count : Integer read FCount write FCount;
45 procedure Add( node : IXMLNode );
46 procedure AddAttribute( const Name : string; const Value : string );
49 IXMLDocument = class( IXMLNode )
51 function GetDocumentElement() : IXMLNode;
53 property DocumentElement : IXMLNode read GetDocumentElement;
56 function XMLCloseCheck(
61 out closed : boolean //
\8cÄ
\82Ñ
\8fo
\82µ
\82½
\83\8b\81[
\83`
\83\93\82ª node
\82ð
\95Â
\82¶
\82é
\82×
\82«
\82È
\82ç true
62 ) : boolean; // ch
\82ð
\82±
\82Ì
\83\8b\81[
\83`
\83\93\82ª
\8f\88\97\9d\82µ
\82½
\82È
\82ç true
67 ) : string; // node
\88È
\8aO
\82Ì
\83m
\81[
\83h
\82ª
\95Â
\82¶
\82ç
\82ê
\82½
\8fê
\8d\87\82Ì
\83m
\81[
\83h
\96¼
69 procedure LoadXMLDocument(
70 const fileName : string;
71 var doc : IXMLDocument
74 //==================================================
76 //==================================================
77 kXMLWhite : TSysCharSet = [#0..#$20];
78 kXMLDQuote : TSysCharSet = ['"'];
79 kXMLTagStart : TSysCharSet = ['<'];
80 kXMLTagEnd : TSysCharSet = ['>'];
81 kXMLKanji : TSysCharSet = [#$81..#$9f, #$E0..#$fc];
83 //==================================================
85 //==================================================
88 constructor IXMLNode.Create;
98 destructor IXMLNode.Destroy;
103 for i := FCount - 1 downto 0 do
111 function IXMLNode.GetAttribute( const Name : string ) : string;
117 while i < FAttributeCount do
119 if Name = FAttributes[ i ].Name then
121 Result := FAttributes[ i ].Value;
130 function IXMLNode.GetNode( Index : Integer ) : IXMLNode;
133 Result := FNodes[ Index ];
137 procedure IXMLNode.Add( node : IXMLNode );
141 SetLength( FNodes, FCount );
142 FNodes[ FCount - 1 ] := node;
146 procedure IXMLNode.AddAttribute(
154 index := FAttributeCount;
155 Inc( FAttributeCount );
156 SetLength( FAttributes, FAttributeCount );
157 FAttributes[ index ].Name := Name;
158 FAttributes[ index ].Value := Value;
162 function IXMLDocument.GetDocumentElement() : IXMLNode;
165 Result := FChildNodes[ 0 ];
169 // untilSet
\82É
\82È
\82é
\82Ü
\82Å
\94ò
\82Î
\82·
170 procedure FileThruUntil(
172 const untilSet : TSysCharSet
178 while f.Position < f.Size do
180 f.ReadBuffer( ch, 1 );
181 if ch in untilSet then
183 f.Seek( -1, soFromCurrent );
185 end else if ch in kXMLKanji then
186 f.Seek( 1, soFromCurrent );
191 // whileSet
\82Ì
\8aÔ
\94ò
\82Î
\82·
192 procedure FileThruWhile(
194 const whileSet : TSysCharSet
200 while f.Position < f.Size do
202 f.ReadBuffer( ch, 1 );
203 if ch in whileSet then
205 if ch in kXMLKanji then
206 f.ReadBuffer( ch, 1 );
208 f.Seek( -1, soFromCurrent );
215 function XMLCloseCheck(
221 ) : boolean; // ch
\82ð
\82±
\82Ì
\83\8b\81[
\83`
\83\93\82ª
\8f\88\97\9d\82µ
\82½
\82È
\82ç true
233 //
\8aJ
\8en
\83^
\83O
\82Ì
\8dÅ
\8cã
\82Ü
\82Å
\93Ç
\82ñ
\82¾
235 end else if ch = '?' then
237 // <?xml?>
\82Ý
\82½
\82¢
\82È
\82â
\82Â
\81B
\82æ
\82Á
\82Ä
\96³
\8e\8b
238 FileThruUntil( f, kXMLTagEnd );
239 FileThruUntil( f, kXMLTagStart );
240 f.Seek( 1, soFromCurrent );
241 FileThruWhile( f, kXMLWhite );
244 end else if ch = '/' then
246 //
\83^
\83O
\96¼
\82ð
\93Ç
\82Ý
\8d\9e\82ñ
\82Å
\95Ô
\82·
248 FileThruUntil( f, kXMLTagEnd );
249 tagLen := f.Position - last;
250 SetLength( tag, tagLen );
252 f.Seek( last, soFromBeginning );
253 f.ReadBuffer( PChar( tag )^, tagLen );
255 f.Seek( f.Position + 1, soFromBeginning ); // '>'
\94ò
\82Î
\82µ
262 function XMLReadNode(
265 ) : string; // node
\88È
\8aO
\82Ì
\83m
\81[
\83h
\82ª
\95Â
\82¶
\82ç
\82ê
\82½
\8fê
\8d\87\82Ì
\83m
\81[
\83h
\96¼
275 attributeName : string;
276 attributeValue : string;
283 // node
\82Ì
\93Ç
\82Ý
\8d\9e\82Ý(1
\83\8b\81[
\83v
\82É
\82Â
\82« 1
\83m
\81[
\83h)
284 node.ChildNodes := IXMLNode.Create;
286 while f.Position < f.Size do
288 // NodeName
\93Ç
\82Ý
\8d\9e\82Ý
289 FileThruWhile( f, kXMLWhite );
291 while f.Position < f.Size do
293 f.ReadBuffer( ch, 1 );
295 if XMLCloseCheck( f, node, ch, tag, isClosed ) then
304 end else if ch = '<' then
306 //
\90V
\8bK
\83m
\81[
\83h
307 child := IXMLNode.Create;
308 tag := XMLReadNode( f, child );
309 node.ChildNodes.Add( child );
311 //
\83^
\83O
\82ª
\95Â
\82¶
\82ç
\82ê
\82½
312 if Length( tag ) > 0 then
314 //
\8e©
\95ª
\82Ì
\82à
\82Ì
\82©
\83`
\83F
\83b
\83N
\82µ
\82Ä
\81A
\88á
\82¦
\82Î
\90e
\82É
\95Ô
\82·
315 if tag <> node.NodeName then
321 end else if ch in kXMLWhite then
326 node.NodeName := node.NodeName + ch;
328 if ch in kXMLKanji then
330 f.ReadBuffer( ch, 1 );
331 node.NodeName := node.NodeName + ch;
336 // Attribute
\82Ì
\93Ç
\82Ý
\8d\9e\82Ý
337 while f.Position < f.Size do
339 // Attribute
\82Ì
\96¼
\91O
\82ð
\93Ç
\82Ý
\8d\9e\82Ý
341 attributeValue := '';
343 FileThruWhile( f, kXMLWhite );
345 while f.Position < f.Size do
347 f.ReadBuffer( ch, 1 );
349 if XMLCloseCheck( f, node, ch, tag, isClosed ) then
353 //
\83^
\83O
\82ª
\95Â
\82¶
\82ç
\82ê
\82½
\82Ì
\82Å
\83\8a\83^
\81[
\83\93
354 //
\81¦NodeName
\82ð
\92Ê
\89ß
\82µ
\82Ä
\82é
\82Ì
\82Å
\93r
\92\86\82Å
\95Â
\82¶
\82Ä
\82é
\82±
\82Æ
\82É
\82È
\82é
\81B
355 //
\82æ
\82Á
\82Ä
\93Æ
\97§
\83m
\81[
\83h
\81B
359 //
\8e\9f\82Ì
\83m
\81[
\83h
\82Ö
361 end else if ch = '=' then
363 //
\82±
\82±
\82©
\82ç
\82Í
\92l
\82ª
\8en
\82Ü
\82é
\82Ì
\82Å
\96¼
\91O
\82Í
\8fI
\97¹
365 end else if ch in kXMLWhite then
367 // Value
\82ª
\91¶
\8dÝ
\82µ
\82È
\82¢(
\8bK
\8ai
\8aO)
\82Ì
\82Å
\8e\9f\82Ì
\83m
\81[
\83h
\82Ö
370 attributeName := attributeName + ch;
372 if ch in kXMLKanji then
374 f.ReadBuffer( ch, 1 );
375 attributeName := attributeName + ch;
380 // Attribute
\82Ì
\92l
\82ð
\93Ç
\82Ý
\8d\9e\82Ý
381 FileThruWhile( f, kXMLWhite );
383 while f.Position < f.Size do
385 f.ReadBuffer( ch, 1 );
387 if XMLCloseCheck( f, node, ch, tag, isClosed ) then
391 if Length( attributeName ) > 0 then
392 //
\8bK
\8ai
\8aO
\82¾
\82¯
\82Ç
\82Ë
393 node.AddAttribute( attributeName, attributeValue );
395 //
\83^
\83O
\82ª
\95Â
\82¶
\82ç
\82ê
\82½
\82Ì
\82Å
\83\8a\83^
\81[
\83\93
396 //
\81¦NodeName
\82ð
\92Ê
\89ß
\82µ
\82Ä
\82é
\82Ì
\82Å
\93r
\92\86\82Å
\95Â
\82¶
\82Ä
\82é
\82±
\82Æ
\82É
\82È
\82é
\81B
397 //
\82æ
\82Á
\82Ä
\93Æ
\97§
\83m
\81[
\83h
\81B
401 //
\8e\9f\82Ì
\83m
\81[
\83h
\82Ö
403 end else if ch = '"' then
405 //
\92l
\82ª ""
\82Å
\8a\87\82ç
\82ê
\82Ä
\82é
\82Ì
\82Å(
\82Ä
\82¢
\82¤
\82©
\8a\87\82ç
\82ê
\82Ä
\82È
\82«
\82á
\82¢
\82¯
\82È
\82¢
\82ñ
\82¾
\82¯
\82Ç)
406 //
\92l
\82ð
\88ê
\8a\87\93Ç
\82Ý
\8d\9e\82Ý
408 FileThruUntil( f, kXMLDQuote );
409 tagLen := f.Position - last;
410 SetLength( attributeValue, tagLen );
412 f.Seek( last, soFromBeginning );
413 f.ReadBuffer( PChar( attributeValue )^, tagLen );
415 node.AddAttribute( attributeName, HtmlDecode( attributeValue ) );
417 //
\92l
\82ð
\93Ç
\82Ý
\8fI
\82í
\82Á
\82½
\82Ì
\82Å
\8fI
\97¹
418 f.Seek( f.Position + 1, soFromBeginning ); // '"'
\94ò
\82Î
\82µ
420 end else if ch in kXMLWhite then
422 //
\8bK
\8ai
\8aO
\82¾
\82¯
\82Ç
\82Ë
423 node.AddAttribute( attributeName, HtmlDecode( attributeValue ) );
427 //
\8bK
\8ai
\8aO
\82¾
\82¯
\82Ç
\88ê
\89\9e\8eæ
\82Á
\82Ä
\82¨
\82
428 attributeValue := attributeValue + ch;
430 if ch in kXMLKanji then
432 f.ReadBuffer( ch, 1 );
433 attributeValue := attributeValue + ch;
437 end; // Attribute
\82Ì
\93Ç
\82Ý
\8d\9e\82Ý
440 end; // // node
\82Ì
\93Ç
\82Ý
\8d\9e\82Ý(1
\83\8b\81[
\83v
\82É
\82Â
\82« 1
\83m
\81[
\83h)
445 procedure LoadXMLDocument(
446 const fileName : string;
447 var doc : IXMLDocument
450 xmlMode = ( xmlHoge );
452 xmlFile : TFileStream;
454 //Result := IXMLDocument.Create;
455 //doc := IXMLDocument.Create;
457 xmlFile := TFileStream.Create( fileName, fmOpenRead );
460 XMLReadNode( xmlFile, IXMLNode( doc ) );
461 //XMLReadNode( xmlFile, IXMLNode( Result ) );