Solved

Expat example...

Posted on 2006-07-15
21
2,113 Views
Last Modified: 2013-11-19
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
Comment
Question by:sudarshantk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 9
21 Comments
 
LVL 8

Expert Comment

by:manish_regmi
ID: 17116451
> 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
 
LVL 8

Expert Comment

by:manish_regmi
ID: 17116698
It seems it is not stdin but something else you need to fread from.

can you point to original source.

regards
Manish Regmi
0
 

Author Comment

by:sudarshantk
ID: 17116775
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
Independent Software Vendors: 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!

 

Author Comment

by:sudarshantk
ID: 17116795
I also have another example....
http://www.xml.com/1999/09/expat/src/line.c
0
 
LVL 8

Expert Comment

by:manish_regmi
ID: 17116839
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
 

Author Comment

by:sudarshantk
ID: 17116844
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
 
LVL 8

Expert Comment

by:manish_regmi
ID: 17116852
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
 

Author Comment

by:sudarshantk
ID: 17116860
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
 
LVL 8

Expert Comment

by:manish_regmi
ID: 17116865
> 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
 

Author Comment

by:sudarshantk
ID: 17116870
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
 

Author Comment

by:sudarshantk
ID: 17116882
I need to store all the Tags and values into internal variable/Structures
0
 
LVL 8

Expert Comment

by:manish_regmi
ID: 17116886
<id>12245<ip>

it should be
<id>12245</id>
0
 

Author Comment

by:sudarshantk
ID: 17116892
What about other errors
0
 
LVL 8

Expert Comment

by:manish_regmi
ID: 17116899
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
 

Author Comment

by:sudarshantk
ID: 17116904
Waht about this->

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


and what about this->

Parse error at line 8:
mismatched tag

Please let me know.
0
 
LVL 8

Expert Comment

by:manish_regmi
ID: 17116924
It seems depth is not handled correctly.

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

regards
Manish Regmi

0
 

Author Comment

by:sudarshantk
ID: 17116937
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
 
LVL 14

Expert Comment

by:cwwkie
ID: 17116950
> 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
 
LVL 8

Accepted Solution

by:
manish_regmi earned 125 total points
ID: 17116951
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

Independent Software Vendors: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
The Best Website Creation Tools 14 122
Correct and Effective way to generate pdf files online via JavaScript or ASP.Net? 6 150
asp Google Map 2 93
How to build a web site 17 105
I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question