[Expat-bugs] using '<' and '>' in the attributes

Giulio Rossato giulio.rossato at infocamere.it
Fri Jul 21 13:52:39 CEST 2006


Hi all,
I have a problem using expat. When i use the characters '<' and '>' 
inside an attribute value, i get the error 'not well-formed (invalid 
token)'.
I get the error whether with version 2.0.0 or with version 1.95.8 of expat.
The tests are run on solaris operating system.
Here is my test code:

fpx10:[sirius]>/home/sirius/development/src_test/sqlexecTest $ cat 
expattest.c
/*
*  Test of expat lib.
*/
#include <stdio.h>
#include <string.h>
#include <expat.h>
#define XML_INPUT_FILE "expattest.xml"
static void XMLCALL startElement(void *userData, const char *name, const 
char **atts) {
       int i;
       printf("start tag: <%s>\n", name);
       for (i=0; atts[i]!=NULL; i+=2)
               printf("\tattribute %s=%s\n", atts[i], atts[i+1]);
}
static void XMLCALL endElement(void *userData, const char *name) {
       printf("end tag: <%s>\n", name);
}
static int parseInputXml(FILE *fpIn) {
       XML_Parser parser;
       char buf[BUFSIZ];
       int done;
       parser = XML_ParserCreate(NULL);
       if (parser == NULL) {
               printf("error creating parser\n");
               return 1;
       }
       XML_SetUserData(parser, NULL);
       XML_SetElementHandler(parser, startElement, endElement);
       do {
               int len = fread(buf, 1, sizeof(buf), fpIn);
               done = feof(fpIn);
               if (XML_Parse(parser, buf, len, done) == XML_STATUS_ERROR) {
                       if (XML_GetErrorCode(parser) != XML_ERROR_ABORTED)
                               printf("parse error at line %d: %s\n",
                               XML_GetCurrentLineNumber(parser),
                               XML_ErrorString(XML_GetErrorCode(parser)));
                       break;
               }
       } while (!done);
       XML_ParserFree(parser);
       return 0;
}
static void testExpat() {
       FILE *fpIn;
       int ret;
   fpIn = fopen(XML_INPUT_FILE, "r");
   if (fpIn == NULL) {
       printf("error opening file %s\n", XML_INPUT_FILE);
       return;
   }
       ret = parseInputXml(fpIn);
       fclose(fpIn);
}
int main() {
       testExpat();
}

If I run this program with various input, the outputs that I get are:

fpx10:[sirius]>/home/sirius/development/src_test/sqlexecTest $ cat 
expattest.xml; ./expattest
<?xml version="1.0"?>
<maintag name="test 1">
</maintag>
start tag: <maintag>
       attribute name=test 1
end tag: <maintag>
fpx10:[sirius]>/home/sirius/development/src_test/sqlexecTest $ cat 
expattest.xml; ./expattest
<?xml version="1.0"?>
<maintag name="test <> 1">
</maintag>
parse error at line 2: not well-formed (invalid token)
fpx10:[sirius]>/home/sirius/development/src_test/sqlexecTest $ cat 
expattest.xml; ./expattest
<?xml version="1.0"?>
<maintag name="test &lt;&gt; 1">
</maintag>
start tag: <maintag>
       attribute name=test <> 1
end tag: <maintag>

In the first prove the attribute 'name' doesn't contain the characters 
'<' and '>' and the test program goes well.
In the second prove the attribute 'name' contains the characters '<' and 
'>' and the test program gets an error.
In the third prove the characters '<' and '>' are been substituted 
rispectively with &lt; and &gt; and the test program runs correctly.

Is there an error on my code? can the characters < and > be used in the 
attributes?
Thanks for your feedback.




More information about the Expat-bugs mailing list