用bcdgen转换的头文件。
对xml不大感兴趣。只是尝试用D语言改写,证明它还是能正确处理xml的。
和C程序差不多。真不爽。
[code]
/**
* expat 中的例子改写成D语言
* Edit By Liu Dehong @ 2007/08/10
* version: 1.0
*/
import std.stdio;
import std.stream;
import bcd.expat.xmlparse;
extern(C)
void startElement(void* userData, XML_Char* name, XML_Char** atts)
{
int i;
int *depthPtr = cast(int *) userData;
for (i = 0; i < *depthPtr; i++)
putchar('\t');
printf("%.*s\n", name);
*depthPtr += 1;
}
extern(C)
void endElement(void *userData, XML_Char *name)
{
int *depthPtr = cast(int*) userData;
*depthPtr -= 1;
}
int main()
{
int done;
int depth = 0;
XML_Parser parser;
string fn = "a.xml";
File f = new File(fn);
parser = XML_ParserCreate(null);
XML_SetUserData(parser, &depth);
XML_SetElementHandler(parser, cast(XML_StartElementHandler)
&startElement, cast(XML_EndElementHandler) &endElement);
while (!f.eof())
{
string line = f.readLine();
//writefln(line, line.length);
if (!XML_Parse(parser, cast(char*) line, line.length, done))
{
fprintf(stderr,
"%s at line %d\n",
XML_ErrorString(XML_GetErrorCode(parser)),
XML_GetCurrentLineNumber(parser));
return 1;
}
}
XML_ParserFree(parser);
return 0;
}
[/code]
对xml不大感兴趣。只是尝试用D语言改写,证明它还是能正确处理xml的。
和C程序差不多。真不爽。
[code]
/**
* expat 中的例子改写成D语言
* Edit By Liu Dehong @ 2007/08/10
* version: 1.0
*/
import std.stdio;
import std.stream;
import bcd.expat.xmlparse;
extern(C)
void startElement(void* userData, XML_Char* name, XML_Char** atts)
{
int i;
int *depthPtr = cast(int *) userData;
for (i = 0; i < *depthPtr; i++)
putchar('\t');
printf("%.*s\n", name);
*depthPtr += 1;
}
extern(C)
void endElement(void *userData, XML_Char *name)
{
int *depthPtr = cast(int*) userData;
*depthPtr -= 1;
}
int main()
{
int done;
int depth = 0;
XML_Parser parser;
string fn = "a.xml";
File f = new File(fn);
parser = XML_ParserCreate(null);
XML_SetUserData(parser, &depth);
XML_SetElementHandler(parser, cast(XML_StartElementHandler)
&startElement, cast(XML_EndElementHandler) &endElement);
while (!f.eof())
{
string line = f.readLine();
//writefln(line, line.length);
if (!XML_Parse(parser, cast(char*) line, line.length, done))
{
fprintf(stderr,
"%s at line %d\n",
XML_ErrorString(XML_GetErrorCode(parser)),
XML_GetCurrentLineNumber(parser));
return 1;
}
}
XML_ParserFree(parser);
return 0;
}
[/code]