Unit: | SDL_streams | Class: | none |
Declaration: | function ReadNextTagInTextFile (const AFile: TextFile; TagList: array of string; var Attrib, Contents: string): integer; {Pascal}
int __fastcall ReadNextTagInTextFile(const TextFile &AFile, const AnsiString * TagList, const int TagList_Size, AnsiString &Attrib, AnsiString &Contents); {C++} |
The function ReadNextTagInTextFile scans the open text file AFile for the next XML tag,
starting at or after the current file position. The read tag
is parsed, the tag attributes are returned in the variable parameter Attrib,
the contents of the tag are returned in the parameter Contents. The function
returns the index of the tag in the TagList. If the tag is not contained in the list, the
index of the last tag of the tag list is returned. It is therefore recommended
to include a dummy tag which indicates an invalid tag. The file position after
performing ReadNextTagInTextFile points to the first character after the read tag.
Hint 1: |
The returned tag index is base zero, i.e. it can be used as the ordinal of a corresponding enumerated type declaration (see example below). |
Hint 2: |
This function cannot be used for HTML tags containing the same HTML tag with different attributes (e.g. <FONT color=#323233>some text <FONT size=3>more text</FONT> additional text</FONT>). In this case the closing </FONT> tag will be associated with the wrong opening tag. |
Hint 3: |
The function ReadNextTagInTextFile is considerably faster (up to a factor 5) than the related function ReadNetTagInStream. |
Hint 4: |
The declaration of ReadNextTagInTextFile in C++ slightly differs from the Pascal declaration (note the extra parameter TagList_Size which specifies the highest index of the TagList array):
int __fastcall ReadNextTagInTextFile(const TextFile &AFile, const AnsiString * TagList, const int TagList_Size, AnsiString &Attrib, AnsiString &Contents); |
Example: |
The following sample code shows how to use the function ReadNextTagInTextFile. The valid XML tags are defined in the constant string array xmlTagIds. Note that the last tag is a dummy tag to account for invalid tags.
type {valid xml tags}
TXmlTag = (xmlSizeX, xmlSizeY, xmlNrInsens, xmlMaxNeighb,
xmlMaxSteps, xmlMaxAlpha, xmlInvalid);
const {xml tag names}
xmlTagIds : array[xmlSizeX..xmlInValid] of string =
('sizex', 'sizey', 'nrinsens', 'maxneighb',
'maxsteps', 'maxalpha', 'invalidtag');
var
attrib : string;
contents : string;
AFile : TextFile;
xmlTag : TXmlTag;
...
xmltag := TXmlTag (ReadNextTagInTextFile (AFile, xmlTagIds, attrib, contents));
...
|
|