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;
39 property NodeName : string read FNodeName write FNodeName;
40 property Attributes[ const Name : string ] : string read GetAttribute;
41 property Node[ Index : Integer ] : IXMLNode read GetNode; default;
42 property ChildNodes : IXMLNode read FChildNodes write FChildNodes;
43 property Count : Integer read FCount write FCount;
44 procedure Add( node : IXMLNode );
45 procedure AddAttribute( const Name : string; const Value : string );
48 IXMLDocument = class( IXMLNode )
50 function GetDocumentElement() : IXMLNode;
52 property DocumentElement : IXMLNode read GetDocumentElement;
55 function XMLCloseCheck(
60 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
61 ) : boolean; // ch
\82ð
\82±
\82Ì
\83\8b\81[
\83`
\83\93\82ª
\8f\88\97\9d\82µ
\82½
\82È
\82ç true
66 ) : string; // node
\88È
\8aO
\82Ì
\83m
\81[
\83h
\82ª
\95Â
\82¶
\82ç
\82ê
\82½
\8fê
\8d\87\82Ì
\83m
\81[
\83h
\96¼
68 function LoadXMLDocument(
69 const fileName : string
72 //==================================================
74 //==================================================
75 kXMLWhite : TSysCharSet = [#0..#$20];
76 kXMLDQuote : TSysCharSet = ['"'];
77 kXMLTagStart : TSysCharSet = ['<'];
78 kXMLTagEnd : TSysCharSet = ['>'];
79 kXMLKanji : TSysCharSet = [#$80..#$A0, #$E0..#$ff];
81 //==================================================
83 //==================================================
86 constructor IXMLNode.Create();
93 function IXMLNode.GetAttribute( const Name : string ) : string;
99 while i < FAttributeCount do
101 if Name = FAttributes[ i ].Name then
103 Result := FAttributes[ i ].Value;
112 function IXMLNode.GetNode( Index : Integer ) : IXMLNode;
115 Result := FNodes[ Index ];
119 procedure IXMLNode.Add( node : IXMLNode );
123 SetLength( FNodes, FCount );
124 FNodes[ FCount - 1 ] := node;
128 procedure IXMLNode.AddAttribute(
136 index := FAttributeCount;
137 Inc( FAttributeCount );
138 SetLength( FAttributes, FAttributeCount );
139 FAttributes[ index ].Name := Name;
140 FAttributes[ index ].Value := Value;
144 function IXMLDocument.GetDocumentElement() : IXMLNode;
147 Result := FChildNodes[ 0 ];
151 // untilSet
\82É
\82È
\82é
\82Ü
\82Å
\94ò
\82Î
\82·
152 procedure FileThruUntil(
154 const untilSet : TSysCharSet
160 while f.Position < f.Size do
162 f.ReadBuffer( ch, 1 );
163 if ch in untilSet then
165 f.Seek( -1, soFromCurrent );
167 end else if ch in kXMLKanji then
168 f.Seek( 1, soFromCurrent );
173 // whileSet
\82Ì
\8aÔ
\94ò
\82Î
\82·
174 procedure FileThruWhile(
176 const whileSet : TSysCharSet
182 while f.Position < f.Size do
184 f.ReadBuffer( ch, 1 );
185 if ch in whileSet then
187 if ch in kXMLKanji then
188 f.ReadBuffer( ch, 1 );
190 f.Seek( -1, soFromCurrent );
197 function XMLCloseCheck(
203 ) : boolean; // ch
\82ð
\82±
\82Ì
\83\8b\81[
\83`
\83\93\82ª
\8f\88\97\9d\82µ
\82½
\82È
\82ç true
215 //
\8aJ
\8en
\83^
\83O
\82Ì
\8dÅ
\8cã
\82Ü
\82Å
\93Ç
\82ñ
\82¾
217 end else if ch = '?' then
219 // <?xml?>
\82Ý
\82½
\82¢
\82È
\82â
\82Â
\81B
\82æ
\82Á
\82Ä
\96³
\8e\8b
220 FileThruUntil( f, kXMLTagEnd );
221 FileThruUntil( f, kXMLTagStart );
222 f.Seek( 1, soFromCurrent );
223 FileThruWhile( f, kXMLWhite );
226 end else if ch = '/' then
228 //
\83^
\83O
\96¼
\82ð
\93Ç
\82Ý
\8d\9e\82ñ
\82Å
\95Ô
\82·
230 FileThruUntil( f, kXMLTagEnd );
231 tagLen := f.Position - last;
232 SetLength( tag, tagLen );
234 f.Seek( last, soFromBeginning );
235 f.ReadBuffer( PChar( tag )^, tagLen );
237 f.Seek( f.Position + 1, soFromBeginning ); // '>'
\94ò
\82Î
\82µ
244 function XMLReadNode(
247 ) : string; // node
\88È
\8aO
\82Ì
\83m
\81[
\83h
\82ª
\95Â
\82¶
\82ç
\82ê
\82½
\8fê
\8d\87\82Ì
\83m
\81[
\83h
\96¼
257 attributeName : string;
258 attributeValue : string;
265 // node
\82Ì
\93Ç
\82Ý
\8d\9e\82Ý(1
\83\8b\81[
\83v
\82É
\82Â
\82« 1
\83m
\81[
\83h)
266 node.ChildNodes := IXMLNode.Create;
268 while f.Position < f.Size do
270 // NodeName
\93Ç
\82Ý
\8d\9e\82Ý
271 FileThruWhile( f, kXMLWhite );
273 while f.Position < f.Size do
275 f.ReadBuffer( ch, 1 );
277 if XMLCloseCheck( f, node, ch, tag, isClosed ) then
286 end else if ch = '<' then
288 //
\90V
\8bK
\83m
\81[
\83h
289 child := IXMLNode.Create;
290 tag := XMLReadNode( f, child );
291 node.ChildNodes.Add( child );
293 //
\83^
\83O
\82ª
\95Â
\82¶
\82ç
\82ê
\82½
294 if Length( tag ) > 0 then
296 //
\8e©
\95ª
\82Ì
\82à
\82Ì
\82©
\83`
\83F
\83b
\83N
\82µ
\82Ä
\81A
\88á
\82¦
\82Î
\90e
\82É
\95Ô
\82·
297 if tag <> node.NodeName then
303 end else if ch in kXMLWhite then
308 node.NodeName := node.NodeName + ch;
310 if ch in kXMLKanji then
312 f.ReadBuffer( ch, 1 );
313 node.NodeName := node.NodeName + ch;
318 // Attribute
\82Ì
\93Ç
\82Ý
\8d\9e\82Ý
319 while f.Position < f.Size do
321 // Attribute
\82Ì
\96¼
\91O
\82ð
\93Ç
\82Ý
\8d\9e\82Ý
323 attributeValue := '';
325 FileThruWhile( f, kXMLWhite );
327 while f.Position < f.Size do
329 f.ReadBuffer( ch, 1 );
331 if XMLCloseCheck( f, node, ch, tag, isClosed ) then
335 //
\83^
\83O
\82ª
\95Â
\82¶
\82ç
\82ê
\82½
\82Ì
\82Å
\83\8a\83^
\81[
\83\93
336 //
\81¦NodeName
\82ð
\92Ê
\89ß
\82µ
\82Ä
\82é
\82Ì
\82Å
\93r
\92\86\82Å
\95Â
\82¶
\82Ä
\82é
\82±
\82Æ
\82É
\82È
\82é
\81B
337 //
\82æ
\82Á
\82Ä
\93Æ
\97§
\83m
\81[
\83h
\81B
341 //
\8e\9f\82Ì
\83m
\81[
\83h
\82Ö
343 end else if ch = '=' then
345 //
\82±
\82±
\82©
\82ç
\82Í
\92l
\82ª
\8en
\82Ü
\82é
\82Ì
\82Å
\96¼
\91O
\82Í
\8fI
\97¹
347 end else if ch in kXMLWhite then
349 // Value
\82ª
\91¶
\8dÝ
\82µ
\82È
\82¢(
\8bK
\8ai
\8aO)
\82Ì
\82Å
\8e\9f\82Ì
\83m
\81[
\83h
\82Ö
352 attributeName := attributeName + ch;
354 if ch in kXMLKanji then
356 f.ReadBuffer( ch, 1 );
357 attributeName := attributeName + ch;
362 // Attribute
\82Ì
\92l
\82ð
\93Ç
\82Ý
\8d\9e\82Ý
363 FileThruWhile( f, kXMLWhite );
365 while f.Position < f.Size do
367 f.ReadBuffer( ch, 1 );
369 if XMLCloseCheck( f, node, ch, tag, isClosed ) then
373 if Length( attributeName ) > 0 then
374 //
\8bK
\8ai
\8aO
\82¾
\82¯
\82Ç
\82Ë
375 node.AddAttribute( attributeName, attributeValue );
377 //
\83^
\83O
\82ª
\95Â
\82¶
\82ç
\82ê
\82½
\82Ì
\82Å
\83\8a\83^
\81[
\83\93
378 //
\81¦NodeName
\82ð
\92Ê
\89ß
\82µ
\82Ä
\82é
\82Ì
\82Å
\93r
\92\86\82Å
\95Â
\82¶
\82Ä
\82é
\82±
\82Æ
\82É
\82È
\82é
\81B
379 //
\82æ
\82Á
\82Ä
\93Æ
\97§
\83m
\81[
\83h
\81B
383 //
\8e\9f\82Ì
\83m
\81[
\83h
\82Ö
385 end else if ch = '"' then
387 //
\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Ç)
388 //
\92l
\82ð
\88ê
\8a\87\93Ç
\82Ý
\8d\9e\82Ý
390 FileThruUntil( f, kXMLDQuote );
391 tagLen := f.Position - last;
392 SetLength( attributeValue, tagLen );
394 f.Seek( last, soFromBeginning );
395 f.ReadBuffer( PChar( attributeValue )^, tagLen );
397 node.AddAttribute( attributeName, HtmlDecode( attributeValue ) );
399 //
\92l
\82ð
\93Ç
\82Ý
\8fI
\82í
\82Á
\82½
\82Ì
\82Å
\8fI
\97¹
400 f.Seek( f.Position + 1, soFromBeginning ); // '"'
\94ò
\82Î
\82µ
402 end else if ch in kXMLWhite then
404 //
\8bK
\8ai
\8aO
\82¾
\82¯
\82Ç
\82Ë
405 node.AddAttribute( attributeName, HtmlDecode( attributeValue ) );
409 //
\8bK
\8ai
\8aO
\82¾
\82¯
\82Ç
\88ê
\89\9e\8eæ
\82Á
\82Ä
\82¨
\82
410 attributeValue := attributeValue + ch;
412 if ch in kXMLKanji then
414 f.ReadBuffer( ch, 1 );
415 attributeValue := attributeValue + ch;
419 end; // Attribute
\82Ì
\93Ç
\82Ý
\8d\9e\82Ý
422 end; // // node
\82Ì
\93Ç
\82Ý
\8d\9e\82Ý(1
\83\8b\81[
\83v
\82É
\82Â
\82« 1
\83m
\81[
\83h)
427 function LoadXMLDocument(
428 const fileName : string
431 xmlMode = ( xmlHoge );
433 xmlFile : TFileStream;
437 doc := IXMLDocument.Create;
439 xmlFile := TFileStream.Create( fileName, fmOpenRead );
440 XMLReadNode( xmlFile, IXMLNode( doc ) );