Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2134
  • Last Modified:

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_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

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(parser, 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_GetErrorCode(parser)),
              XML_GetCurrentLineNumber(parser));
      return 1;
    }
  } while (!done);
  printf("7\n");
  XML_ParserFree(parser);
  printf("8\n");
  return 0;
}
0
sudarshantk
Asked:
sudarshantk
  • 9
  • 9
1 Solution
 
manish_regmiCommented:
> size_t len = fread(buf, 1, sizeof(buf), stdin);
>    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
0
 
manish_regmiCommented:
It seems it is not stdin but something else you need to fread from.

can you point to original source.

regards
Manish Regmi
0
 
sudarshantkAuthor Commented:
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>


~
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
sudarshantkAuthor Commented:
I also have another example....
http://www.xml.com/1999/09/expat/src/line.c
0
 
manish_regmiCommented:
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
0
 
sudarshantkAuthor Commented:
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
0
 
manish_regmiCommented:
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

0
 
sudarshantkAuthor Commented:
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,
0
 
manish_regmiCommented:
> 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




0
 
sudarshantkAuthor Commented:
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</present>


  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
0
 
sudarshantkAuthor Commented:
I need to store all the Tags and values into internal variable/Structures
0
 
manish_regmiCommented:
<id>12245<ip>

it should be
<id>12245</id>
0
 
sudarshantkAuthor Commented:
What about other errors
0
 
manish_regmiCommented:
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
0
 
sudarshantkAuthor Commented:
Waht about this->

Start tag ip - <ip>STRING=
ÿ¿®ç


and what about this->

Parse error at line 8:
mismatched tag

Please let me know.
0
 
manish_regmiCommented:
It seems depth is not handled correctly.

try this one.
http://www.xml.com/1999/09/expat/src/outline.c

regards
Manish Regmi

0
 
sudarshantkAuthor Commented:
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?
0
 
cwwkieCommented:
> 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>
0
 
manish_regmiCommented:
ok, i found the error
1) you cannot directly write <ip=
should be <ip val="192.168.1.2"

2) the data must be enclosed in "". period=60
should be period="60"



//1
<?xml version="1.0" ?>
<reader>
<ip>12245</ip>
<aa>
<ip>192.20.1.1</ip>
<present>true</present>
</aa>
</reader>

//2
<?xml version="1.0" ?>
<reader>
<id>
12445
</id>
      <ip val="192.168.1.1" present="true"  loseheartbeats="no">
                  <tags>
                       <periodic period="60" toggle="true">
                        111111111111111111111
                       </periodic>
                       <ondemand >
                       0000000000000000000001
                       0000000000000000000002
                       0000000000000000000003
                       </ondemand>
                  </tags>
           </ip>
          <ip val="192.168.1.2" present="false"  ></ip>
          <ip val="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>

regards
Manish Regmi

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 9
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now