sudarshantk
asked on
Expat example...
There is some problem in this expat example...it seems to be going in infinite loop since it is not comming out of while(done) this is elements.c example provided in the expat distribution....
Anyone know about this?
/* This is simple demonstration of how to use expat. This program
reads an XML document from standard input and writes a line with
the name of each element to standard output indenting child
elements by one tab stop more than their parent element.
It must be used with Expat compiled for UTF-8 output.
*/
#include <stdio.h>
#include "expat.h"
#ifdef XML_LARGE_SIZE
#if defined(XML_USE_MSC_EXTENS IONS) && _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
static void XMLCALL
startElement(void *userData, const char *name, const char **atts)
{
int i;
int *depthPtr = (int *)userData;
for (i = 0; i < *depthPtr; i++)
putchar('\t');
puts(name);
*depthPtr += 1;
}
static void XMLCALL
endElement(void *userData, const char *name)
{
int *depthPtr = (int *)userData;
*depthPtr -= 1;
}
#ifdef AMIGA_SHARED_LIB
#include <proto/expat.h>
int
amiga_main(int argc, char *argv[])
#else
int
main(int argc, char *argv[])
#endif
{
char buf[BUFSIZ];
printf("1\n");
XML_Parser parser = XML_ParserCreate(NULL);
printf("2\n");
int done;
int depth = 0;
printf("3\n");
XML_SetUserData(parser, &depth);
printf("4\n");
XML_SetElementHandler(pars er, startElement, endElement);
printf("5\n");
do {
size_t len = fread(buf, 1, sizeof(buf), stdin);
done = len < sizeof(buf);
printf("6\n");
if (XML_Parse(parser, buf, len, done) == XML_STATUS_ERROR) {
fprintf(stderr,
"%s at line %" XML_FMT_INT_MOD "u\n",
XML_ErrorString(XML_GetErr orCode(par ser)),
XML_GetCurrentLineNumber(p arser));
return 1;
}
} while (!done);
printf("7\n");
XML_ParserFree(parser);
printf("8\n");
return 0;
}
Anyone know about this?
/* This is simple demonstration of how to use expat. This program
reads an XML document from standard input and writes a line with
the name of each element to standard output indenting child
elements by one tab stop more than their parent element.
It must be used with Expat compiled for UTF-8 output.
*/
#include <stdio.h>
#include "expat.h"
#ifdef XML_LARGE_SIZE
#if defined(XML_USE_MSC_EXTENS
#define XML_FMT_INT_MOD "I64"
#else
#define XML_FMT_INT_MOD "ll"
#endif
#else
#define XML_FMT_INT_MOD "l"
#endif
static void XMLCALL
startElement(void *userData, const char *name, const char **atts)
{
int i;
int *depthPtr = (int *)userData;
for (i = 0; i < *depthPtr; i++)
putchar('\t');
puts(name);
*depthPtr += 1;
}
static void XMLCALL
endElement(void *userData, const char *name)
{
int *depthPtr = (int *)userData;
*depthPtr -= 1;
}
#ifdef AMIGA_SHARED_LIB
#include <proto/expat.h>
int
amiga_main(int argc, char *argv[])
#else
int
main(int argc, char *argv[])
#endif
{
char buf[BUFSIZ];
printf("1\n");
XML_Parser parser = XML_ParserCreate(NULL);
printf("2\n");
int done;
int depth = 0;
printf("3\n");
XML_SetUserData(parser, &depth);
printf("4\n");
XML_SetElementHandler(pars
printf("5\n");
do {
size_t len = fread(buf, 1, sizeof(buf), stdin);
done = len < sizeof(buf);
printf("6\n");
if (XML_Parse(parser, buf, len, done) == XML_STATUS_ERROR) {
fprintf(stderr,
"%s at line %" XML_FMT_INT_MOD "u\n",
XML_ErrorString(XML_GetErr
XML_GetCurrentLineNumber(p
return 1;
}
} while (!done);
printf("7\n");
XML_ParserFree(parser);
printf("8\n");
return 0;
}
It seems it is not stdin but something else you need to fread from.
can you point to original source.
regards
Manish Regmi
can you point to original source.
regards
Manish Regmi
ASKER
One more question...
This is my sample XML file I need to parse it and print the tags and Data using expat Do you have a example...
<?xml version="1.0" ?>
<reader>
<id>
12445
</id>
<ip="192.168.1.1" present="true" loseheartbeats="no">
<tags>
<periodic period=60 toggle="true">
111111111111111111111
</periodic>
<ondemand >
0000000000000000000001
0000000000000000000002
0000000000000000000003
</ondemand>
</tags>
</ip>
<ip="192.168.1.2" present="false" ></ip>
<ip="192.168.1.3" present="true" loaseheartbeats="yes" loseheartbeatsperiod="200" >
<tags>
<periodic period=60>
311111111111111111111
</periodic>
<ondemand toggle="false">
3000000000000000000001
3000000000000000000002
3000000000000000000003
</ondemand>
</tags>
</ip>
</reader>
~
This is my sample XML file I need to parse it and print the tags and Data using expat Do you have a example...
<?xml version="1.0" ?>
<reader>
<id>
12445
</id>
<ip="192.168.1.1" present="true" loseheartbeats="no">
<tags>
<periodic period=60 toggle="true">
111111111111111111111
</periodic>
<ondemand >
0000000000000000000001
0000000000000000000002
0000000000000000000003
</ondemand>
</tags>
</ip>
<ip="192.168.1.2" present="false" ></ip>
<ip="192.168.1.3" present="true" loaseheartbeats="yes" loseheartbeatsperiod="200"
<tags>
<periodic period=60>
311111111111111111111
</periodic>
<ondemand toggle="false">
3000000000000000000001
3000000000000000000002
3000000000000000000003
</ondemand>
</tags>
</ip>
</reader>
~
ASKER
I also have another example....
http://www.xml.com/1999/09/expat/src/line.c
http://www.xml.com/1999/09/expat/src/line.c
OK, The program is expecting the data from the stdin.
It means you need to use pipes (UNIX)
eg,
cat my.xml | yourprog
Alternatively, You may try to read from the xml file.
FILE *fl;
fl = fopen("xyz.xml", "r");
then replace stdin with fl.
regards
Manish Regmi
It means you need to use pipes (UNIX)
eg,
cat my.xml | yourprog
Alternatively, You may try to read from the xml file.
FILE *fl;
fl = fopen("xyz.xml", "r");
then replace stdin with fl.
regards
Manish Regmi
ASKER
Yes i tried that it works to some extent but for the above xml file that has attributes like "period etc" it fails...Is there any standard example in expat that can completely parse the above XML file
where does the parse fail. i mean what error it returns.
This means that your XML is not valid and the parse engine is unable to parse the xml file.
regards
Manish Regmi
This means that your XML is not valid and the parse engine is unable to parse the xml file.
regards
Manish Regmi
ASKER
Hi Manish,
could you please suggest some methods to parse the above XML file. Is it possible to modify the above XML file to work with one of the examples mentioned above.
Rgds,
could you please suggest some methods to parse the above XML file. Is it possible to modify the above XML file to work with one of the examples mentioned above.
Rgds,
> could you please suggest some methods to parse the above XML file.
Your program does the work of parsing. You need a parser engine to parse it. Expat is a good one. see its project page
http://www.libexpat.org/
>> Is it possible to modify the above XML file to work with one of the examples mentioned above.
Of course.
Let me see the error it returns. You are printing the error with fprintf().
regards
Manish Regmi
Your program does the work of parsing. You need a parser engine to parse it. Expat is a good one. see its project page
http://www.libexpat.org/
>> Is it possible to modify the above XML file to work with one of the examples mentioned above.
Of course.
Let me see the error it returns. You are printing the error with fprintf().
regards
Manish Regmi
ASKER
0: Start tag reader - <reader>STRING=
`‡è
1: Text -
2: Start tag id - <id>STRING=12245<ip>
3: Text - 12245
4: Start tag ip - <ip>STRING=
ÿ¿®ç
5: Text -
6: Start tag aa - <aa>STRING=
ÿ¿®ç
7: Text -
8: Start tag ip - <ip>STRING=192.20.1.1</ip>
9: Text - 192.20.1.1
10: End tag ip -
11: Text -
12: Start tag present - <present>STRING=true</pres ent>
13: Text - true
14: End tag present -
15: Text -
16: End tag aa -
17: Text -
Parse error at line 8:
mismatched tag
XML File...
---------------------
<?xml version="1.0" ?>
<reader>
<id>12245<ip>
<aa>
<ip>192.20.1.1</ip>
<present>true</present>
</aa>
</reader>
Please let me know how to fix it
`‡è
1: Text -
2: Start tag id - <id>STRING=12245<ip>
3: Text - 12245
4: Start tag ip - <ip>STRING=
ÿ¿®ç
5: Text -
6: Start tag aa - <aa>STRING=
ÿ¿®ç
7: Text -
8: Start tag ip - <ip>STRING=192.20.1.1</ip>
9: Text - 192.20.1.1
10: End tag ip -
11: Text -
12: Start tag present - <present>STRING=true</pres
13: Text - true
14: End tag present -
15: Text -
16: End tag aa -
17: Text -
Parse error at line 8:
mismatched tag
XML File...
---------------------
<?xml version="1.0" ?>
<reader>
<id>12245<ip>
<aa>
<ip>192.20.1.1</ip>
<present>true</present>
</aa>
</reader>
Please let me know how to fix it
ASKER
I need to store all the Tags and values into internal variable/Structures
<id>12245<ip>
it should be
<id>12245</id>
it should be
<id>12245</id>
ASKER
What about other errors
there are no other errors in your Xml. It should parse fine.
have you read this tutorial.
http://www.xml.com/pub/a/1999/09/expat/index.html
regards
Manish Regmi
have you read this tutorial.
http://www.xml.com/pub/a/1999/09/expat/index.html
regards
Manish Regmi
ASKER
Waht about this->
Start tag ip - <ip>STRING=
ÿ¿®ç
and what about this->
Parse error at line 8:
mismatched tag
Please let me know.
Start tag ip - <ip>STRING=
ÿ¿®ç
and what about this->
Parse error at line 8:
mismatched tag
Please let me know.
It seems depth is not handled correctly.
try this one.
http://www.xml.com/1999/09/expat/src/outline.c
regards
Manish Regmi
try this one.
http://www.xml.com/1999/09/expat/src/outline.c
regards
Manish Regmi
ASKER
Hi Manish,
Here is program output
------------------------
cat foo.xml | ./outline1
reader
id
id
aa
ip
present
Parse error at line 8:
mismatched tag
--------------------------
Where is the data?
Here is program output
------------------------
cat foo.xml | ./outline1
reader
id
id
aa
ip
present
Parse error at line 8:
mismatched tag
--------------------------
Where is the data?
> reader
> id
> id
> aa
> ip
> present
> Parse error at line 8:
> mismatched tag
It looks as if your input file is still not correct.
I think the second line is now: <id>12245<id>
but it should be: <id>12245</id>
> id
> id
> aa
> ip
> present
> Parse error at line 8:
> mismatched tag
It looks as if your input file is still not correct.
I think the second line is now: <id>12245<id>
but it should be: <id>12245</id>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
> done = len < sizeof(buf);
fread returns the no of items read. sizebuf(buf) reurns the size of buf. If you are at the end you get shorter value than you passed or 0.
But you are reading from stdin? why?
It will block until you put sizeof(buf) characters from your standard input device.
regards
Manish Regmi