Solved

Expat example...

Posted on 2006-07-15
21
2,094 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
  • 9
  • 9
21 Comments
 
LVL 8

Expert Comment

by:manish_regmi
Comment Utility
> 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
Comment Utility
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
Comment Utility
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
 

Author Comment

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

Expert Comment

by:manish_regmi
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
> 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:sudarshantk
Comment Utility
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
Comment Utility
I need to store all the Tags and values into internal variable/Structures
0
 
LVL 8

Expert Comment

by:manish_regmi
Comment Utility
<id>12245<ip>

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

Author Comment

by:sudarshantk
Comment Utility
What about other errors
0
 
LVL 8

Expert Comment

by:manish_regmi
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
> 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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

There are two main kinds of selectors in CSS: One is base selector like h1, h2, body, table or any existing HTML tags.  For instance, the following rule sets all paragraphs (<p> elements) to red: (CODE) CSS also allows us to define our own custom …
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now