Mini-XML doesn't retrieve root element properly



I'm using Mini-XML to parse some XML documents. Here's some sample code I wrote:



#include <stdio.h>
#include "mxml/mxml.h"


void test_xml(mxml_node_t *node, int indent)
{
int i;
mxml_node_t *n;
for(i = 0; i < indent; ++i) printf(" ");
printf("Node name: %s, node type: %d\n", node->value.element.name,
node->type);
for(i = 0; i < indent; ++i) printf(" ");
printf("Sibling nodes:\n");
for(n = node->next; n; n = n->next) test_xml(n, indent + 1);
for(i = 0; i < indent; ++i) printf(" ");
printf("End of sibling nodes\nChild nodes:\n");
for(n = node->child; n; n = n->child) test_xml(n, indent + 1);
for(i = 0; i < indent; ++i) printf(" ");
printf("End of child nodes\nEnd of node %s\n", node->value.element.name);
}


int main(int argc, char **argv)
{

//Test
FILE *f = fopen("test.xml", "r");
if(!f)
{
printf("File problem!");
return 1;
}
mxml_node_t *root = mxmlLoadFile(0, f, MXML_TEXT_CALLBACK);
fclose(f);
if(!root)
{
printf("Invalid XML document!");
return 1;
}
test_xml(root, 0);
//End of test

return 0;
}


File test.xml contains a valid XML document with XML declaration and what else. The problem is that it's parsed correctly only if I remove that declaration. Yeah, strange, but if I don't do that, it prints me the following:



Node name: ?xml version="1.0" encoding="UTF-8"?, node type: 0

Sibling nodes:

End of sibling nodes

Child nodes:



and then crashes.


Something I'm doing wrong, maybe? I'd rather avoid removing XML declarations from documents I'm about to parse with my app.


No comments:

Post a Comment