Hi everybody, I'm new with C programming also the expat. So I must realize a project where I should use Expat as a parser XML. The object is parsing this XML file:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<conf>
<network>
<config_interface_loopback>
<option>
<ifname>lo</ifname>
<proto>static</proto>
<ipaddr>127.0.0.1</ipaddr>
<netmask>255.0.0.0</netmask>
</option>
</config_interface_loopback>
<config_interface_lan>
<option>
<ifname>eth0</ifname>
<type>bridge</type>
<proto>static</proto>
<ipaddr>192.168.1.1</ipaddr>
<netmask>255.255.255.0</netmask>
</option>
</config_interface_lan>
<config_interface_wifi>
<option>
<proto>static</proto>
<ipaddr>192.168.2.1</ipaddr>
<netmask>255.255.255.0</netmask>
</option>
</config_interface_wifi>
</network>
<wireless>
<config_wifi-device_radio0>
<option>
<type>atheros</type>
<channel>auto</channel>
<macaddr>00:15:6d:fc:71:ac</macaddr>
<disable>0</disable>
</option>
</config_wifi-device_radio0>
<config_wifi-iface>
<option>
<device>radio0</device>
<network>wifi</network>
<mode>ap</mode>
<ssid>OpenWrt</ssid>
<encryption>none</encryption>
</option>
</config_wifi-iface>
</wireless>
</conf>
</note>
I'd like to parse it to a file who names network, and the contents are:
Code:
config interface loopback
option ifname lo
option proto static
option ipaddr 127.0.0.1
option netmask 255.0.0.0
config interface lan
option ifname eth0
option type bridge
option proto static
option ipaddr 192.168.1.1
option netmask 255.255.255.0
config interface wifi
option proto static
option ipaddr 192.168.2.1
option netmask 255.255.255.0
So, I tried myself to use expat. But with the example given by expat, I can't write a parser which could show me the contents of the elements. That's the source code I changed:
Code:
#include <stdio.h>
#include <expat.h>
#if defined(__amigaos__) && defined(__USE_INLINE__)
#include <proto/expat.h>
#endif
#ifdef XML_LARGE_SIZE
#if defined(XML_USE_MSC_EXTENSIONS) && _MSC_VER < 1400
#define XML_FMT_INT_MOD "I64"
#else
#define XML_FMT_INT_MOD "ll"
#endif
#else
#define XML_FMT_INT_MOD "l"
#endif
#define BUFFSIZE 8192
char Buff[BUFFSIZE];
int Depth;
static void XMLCALL
start(void *data, const char *el, const char **attr)
{
int i;
for (i = 0; i < Depth; i++)
printf(" ", data);
printf("%s", el);
for (i = 0; attr[i]; i += 2) {
printf(" %s='%s'", attr[i], attr[i + 1]);
}
printf("\n");
Depth++;
}
static void XMLCALL
end(void *data, const char *el)
{
Depth--;
}
int
main(int argc, char *argv[])
{
XML_Parser p = XML_ParserCreate(NULL);
if (! p) {
fprintf(stderr, "Couldn't allocate memory for parser\n");
exit(-1);
}
XML_SetElementHandler(p, start, end);
for (;;) {
int done;
int len;
len = (int)fread(Buff, 1, BUFFSIZE, stdin);
if (ferror(stdin)) {
fprintf(stderr, "Read error\n");
exit(-1);
}
done = feof(stdin);
if (XML_Parse(p, Buff, len, done) == XML_STATUS_ERROR) {
fprintf(stderr, "Parse error at line %" XML_FMT_INT_MOD "u:\n%s\n",
XML_GetCurrentLineNumber(p),
XML_ErrorString(XML_GetErrorCode(p)));
exit(-1);
}
if (done)
break;
}
XML_ParserFree(p);
return 0;
}
I'd like to ask the parser to print me all the contents in the file, but it won't work, can somebody help me a little please? Thanks a lot.