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 function LoadXMLDocument(
70 const fileName : string
73 //==================================================
75 //==================================================
76 kXMLWhite : TSysCharSet = [#0..#$20];
77 kXMLDQuote : TSysCharSet = ['"'];
78 kXMLTagStart : TSysCharSet = ['<'];
79 kXMLTagEnd : TSysCharSet = ['>'];
80 kXMLKanji : TSysCharSet = [#$80..#$A0, #$E0..#$ff];
82 //==================================================
84 //==================================================
87 constructor IXMLNode.Create;
97 destructor IXMLNode.Destroy;
102 for i := FCount - 1 downto 0 do
110 function IXMLNode.GetAttribute( const Name : string ) : string;
116 while i < FAttributeCount do
118 if Name = FAttributes[ i ].Name then
120 Result := FAttributes[ i ].Value;
129 function IXMLNode.GetNode( Index : Integer ) : IXMLNode;
132 Result := FNodes[ Index ];
136 procedure IXMLNode.Add( node : IXMLNode );
140 SetLength( FNodes, FCount );
141 FNodes[ FCount - 1 ] := node;
145 procedure IXMLNode.AddAttribute(
153 index := FAttributeCount;
154 Inc( FAttributeCount );
155 SetLength( FAttributes, FAttributeCount );
156 FAttributes[ index ].Name := Name;
157 FAttributes[ index ].Value := Value;
161 function IXMLDocument.GetDocumentElement() : IXMLNode;
164 Result := FChildNodes[ 0 ];
168 // untilSet
\82É
\82È
\82é
\82Ü
\82Å
\94ò
\82Î
\82·
169 procedure FileThruUntil(
171 const untilSet : TSysCharSet
177 while f.Position < f.Size do
179 f.ReadBuffer( ch, 1 );
180 if ch in untilSet then
182 f.Seek( -1, soFromCurrent );
184 end else if ch in kXMLKanji then
185 f.Seek( 1, soFromCurrent );
190 // whileSet
\82Ì
\8aÔ
\94ò
\82Î
\82·
191 procedure FileThruWhile(
193 const whileSet : TSysCharSet
199 while f.Position < f.Size do
201 f.ReadBuffer( ch, 1 );
202 if ch in whileSet then
204 if ch in kXMLKanji then
205 f.ReadBuffer( ch, 1 );
207 f.Seek( -1, soFromCurrent );
214 function XMLCloseCheck(
220 ) : boolean; // ch
\82ð
\82±
\82Ì
\83\8b\81[
\83`
\83\93\82ª
\8f\88\97\9d\82µ
\82½
\82È
\82ç true
232 //
\8aJ
\8en
\83^
\83O
\82Ì
\8dÅ
\8cã
\82Ü
\82Å
\93Ç
\82ñ
\82¾
234 end else if ch = '?' then
236 // <?xml?>
\82Ý
\82½
\82¢
\82È
\82â
\82Â
\81B
\82æ
\82Á
\82Ä
\96³
\8e\8b
237 FileThruUntil( f, kXMLTagEnd );
238 FileThruUntil( f, kXMLTagStart );
239 f.Seek( 1, soFromCurrent );
240 FileThruWhile( f, kXMLWhite );
243 end else if ch = '/' then
245 //
\83^
\83O
\96¼
\82ð
\93Ç
\82Ý
\8d\9e\82ñ
\82Å
\95Ô
\82·
247 FileThruUntil( f, kXMLTagEnd );
248 tagLen := f.Position - last;
249 SetLength( tag, tagLen );
251 f.Seek( last, soFromBeginning );
252 f.ReadBuffer( PChar( tag )^, tagLen );
254 f.Seek( f.Position + 1, soFromBeginning ); // '>'
\94ò
\82Î
\82µ
261 function XMLReadNode(
264 ) : string; // node
\88È
\8aO
\82Ì
\83m
\81[
\83h
\82ª
\95Â
\82¶
\82ç
\82ê
\82½
\8fê
\8d\87\82Ì
\83m
\81[
\83h
\96¼
274 attributeName : string;
275 attributeValue : string;
282 // node
\82Ì
\93Ç
\82Ý
\8d\9e\82Ý(1
\83\8b\81[
\83v
\82É
\82Â
\82« 1
\83m
\81[
\83h)
283 node.ChildNodes := IXMLNode.Create;
285 while f.Position < f.Size do
287 // NodeName
\93Ç
\82Ý
\8d\9e\82Ý
288 FileThruWhile( f, kXMLWhite );
290 while f.Position < f.Size do
292 f.ReadBuffer( ch, 1 );
294 if XMLCloseCheck( f, node, ch, tag, isClosed ) then
303 end else if ch = '<' then
305 //
\90V
\8bK
\83m
\81[
\83h
306 child := IXMLNode.Create;
307 tag := XMLReadNode( f, child );
308 node.ChildNodes.Add( child );
310 //
\83^
\83O
\82ª
\95Â
\82¶
\82ç
\82ê
\82½
311 if Length( tag ) > 0 then
313 //
\8e©
\95ª
\82Ì
\82à
\82Ì
\82©
\83`
\83F
\83b
\83N
\82µ
\82Ä
\81A
\88á
\82¦
\82Î
\90e
\82É
\95Ô
\82·
314 if tag <> node.NodeName then
320 end else if ch in kXMLWhite then
325 node.NodeName := node.NodeName + ch;
327 if ch in kXMLKanji then
329 f.ReadBuffer( ch, 1 );
330 node.NodeName := node.NodeName + ch;
335 // Attribute
\82Ì
\93Ç
\82Ý
\8d\9e\82Ý
336 while f.Position < f.Size do
338 // Attribute
\82Ì
\96¼
\91O
\82ð
\93Ç
\82Ý
\8d\9e\82Ý
340 attributeValue := '';
342 FileThruWhile( f, kXMLWhite );
344 while f.Position < f.Size do
346 f.ReadBuffer( ch, 1 );
348 if XMLCloseCheck( f, node, ch, tag, isClosed ) then
352 //
\83^
\83O
\82ª
\95Â
\82¶
\82ç
\82ê
\82½
\82Ì
\82Å
\83\8a\83^
\81[
\83\93
353 //
\81¦NodeName
\82ð
\92Ê
\89ß
\82µ
\82Ä
\82é
\82Ì
\82Å
\93r
\92\86\82Å
\95Â
\82¶
\82Ä
\82é
\82±
\82Æ
\82É
\82È
\82é
\81B
354 //
\82æ
\82Á
\82Ä
\93Æ
\97§
\83m
\81[
\83h
\81B
358 //
\8e\9f\82Ì
\83m
\81[
\83h
\82Ö
360 end else if ch = '=' then
362 //
\82±
\82±
\82©
\82ç
\82Í
\92l
\82ª
\8en
\82Ü
\82é
\82Ì
\82Å
\96¼
\91O
\82Í
\8fI
\97¹
364 end else if ch in kXMLWhite then
366 // Value
\82ª
\91¶
\8dÝ
\82µ
\82È
\82¢(
\8bK
\8ai
\8aO)
\82Ì
\82Å
\8e\9f\82Ì
\83m
\81[
\83h
\82Ö
369 attributeName := attributeName + ch;
371 if ch in kXMLKanji then
373 f.ReadBuffer( ch, 1 );
374 attributeName := attributeName + ch;
379 // Attribute
\82Ì
\92l
\82ð
\93Ç
\82Ý
\8d\9e\82Ý
380 FileThruWhile( f, kXMLWhite );
382 while f.Position < f.Size do
384 f.ReadBuffer( ch, 1 );
386 if XMLCloseCheck( f, node, ch, tag, isClosed ) then
390 if Length( attributeName ) > 0 then
391 //
\8bK
\8ai
\8aO
\82¾
\82¯
\82Ç
\82Ë
392 node.AddAttribute( attributeName, attributeValue );
394 //
\83^
\83O
\82ª
\95Â
\82¶
\82ç
\82ê
\82½
\82Ì
\82Å
\83\8a\83^
\81[
\83\93
395 //
\81¦NodeName
\82ð
\92Ê
\89ß
\82µ
\82Ä
\82é
\82Ì
\82Å
\93r
\92\86\82Å
\95Â
\82¶
\82Ä
\82é
\82±
\82Æ
\82É
\82È
\82é
\81B
396 //
\82æ
\82Á
\82Ä
\93Æ
\97§
\83m
\81[
\83h
\81B
400 //
\8e\9f\82Ì
\83m
\81[
\83h
\82Ö
402 end else if ch = '"' then
404 //
\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Ç)
405 //
\92l
\82ð
\88ê
\8a\87\93Ç
\82Ý
\8d\9e\82Ý
407 FileThruUntil( f, kXMLDQuote );
408 tagLen := f.Position - last;
409 SetLength( attributeValue, tagLen );
411 f.Seek( last, soFromBeginning );
412 f.ReadBuffer( PChar( attributeValue )^, tagLen );
414 node.AddAttribute( attributeName, HtmlDecode( attributeValue ) );
416 //
\92l
\82ð
\93Ç
\82Ý
\8fI
\82í
\82Á
\82½
\82Ì
\82Å
\8fI
\97¹
417 f.Seek( f.Position + 1, soFromBeginning ); // '"'
\94ò
\82Î
\82µ
419 end else if ch in kXMLWhite then
421 //
\8bK
\8ai
\8aO
\82¾
\82¯
\82Ç
\82Ë
422 node.AddAttribute( attributeName, HtmlDecode( attributeValue ) );
426 //
\8bK
\8ai
\8aO
\82¾
\82¯
\82Ç
\88ê
\89\9e\8eæ
\82Á
\82Ä
\82¨
\82
427 attributeValue := attributeValue + ch;
429 if ch in kXMLKanji then
431 f.ReadBuffer( ch, 1 );
432 attributeValue := attributeValue + ch;
436 end; // Attribute
\82Ì
\93Ç
\82Ý
\8d\9e\82Ý
439 end; // // node
\82Ì
\93Ç
\82Ý
\8d\9e\82Ý(1
\83\8b\81[
\83v
\82É
\82Â
\82« 1
\83m
\81[
\83h)
444 function LoadXMLDocument(
445 const fileName : string
448 xmlMode = ( xmlHoge );
450 xmlFile : TFileStream;
454 doc := IXMLDocument.Create;
456 xmlFile := TFileStream.Create( fileName, fmOpenRead );
457 XMLReadNode( xmlFile, IXMLNode( doc ) );