简体   繁体   中英

Parsing a XML file in C using MiniXML

I'm using the MiniXML library to parse a XML file in C, however when i try to read the node's value it returns NULL.

Here's the code:

FILE *fp;
mxml_node_t *tree;

fp = fopen("test.xml", "r");
tree = mxmlLoadFile(NULL, fp, MXML_TEXT_CALLBACK);
fclose(fp);

mxml_node_t *node;
for(node = mxmlFindElement(tree, tree,"node",NULL, NULL,MXML_DESCEND);
     node != NULL;
     node = mxmlFindElement(node, tree,"node",NULL, NULL, MXML_DESCEND)) {
  printf("Text: %s\n", node->value.text.string);
}

The problem is that node->value.text.string is NULL. I've been reading the documentation and I don't know what im doing wrong. Has anybody run into this problem before?

Try changing your for-loop to specify:

node->child->value.text.string

instead of:

node->value.text.string

Does that work? It's just a guess, but I'm thinking it might be necessary to get the data for the "node" elements.

If that does not work, look at C++: Trouble loading long string from XML file using Mini-XML . This person says they needed to use MXML_DESCEND_FIRST instead of MXML_DESCEND to fix their problem. I'm not sure if it would help in your case.

If neither of these work, you might post your input XML as well so that we can try to recreate your problem.

I got the same error. Thanks for the link you posted. I can fixed the error by changing node->child->value.text.string in node->child->value.opaque. nothing to change when the file is opened because the type_cb function select the right case.

Minixml bug 502 - minixml mxmlLoad*() functions fail to load text with MXML_TEXT_CALLBACK. To work around, define and use your own text callback

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM