1

I write an xml document like this:

<doc>
  <node1>
    <value1>aa bb</value1>
    <value2>cc dd</value2>
  </node1>
  <node2>
    <value1>aaa bbb</value1>
    <value2>ccc ddd</value2>
  </node2>
</doc>

I try to parse this document with minixml library :

    mxml_node_t                 *b = tree, *c; //tree is global variable

    while (b) {
        if (b && b->type == MXML_ELEMENT) {
            if(strcmp(b->value.element.name, "value1") == 0)
            {
                c = mxmlWalkNext(b, b, MXML_DESCEND);
                if (c && c->type == MXML_TEXT)
                {
                    if(c->value.text.string != NULL)
                    {
                        printf("value1=%s"c->value.text.string);
                    }
                }
            }
        }
        if (b && b->type == MXML_ELEMENT) {
            if(strcmp(b->value.element.name, "value2") == 0)
            {
                c = mxmlWalkNext(b, b, MXML_DESCEND);
                if (c && c->type == MXML_TEXT)
                {
                    if(c->value.text.string != NULL)
                    {
                        printf("value2=%s"c->value.text.string);
                    }
                }
            }
        }
        b = mxmlWalkNext(b, tree, MXML_DESCEND);
    }

When I parse the file I found this result:

value1=aa //the value is not right it must be "aa bb"
value2=cc
...

Have you an idea, how to solve my problem?

4

1 回答 1

0

I used the following solution:

mxml_node_t                 *b = tree; //tree is global variable
char                        *tmp,*value1=NULL,*value2=NULL;

while (b) {
    if (b && b->type == MXML_TEXT &&
        b->value.text.string &&
        b->parent->type == MXML_ELEMENT &&
        !strcmp(b->parent->value.element.name, "value1"))
        {
            if(value1 != NULL)
            {
                value1 = strdup(b->value.text.string);
            }
            else
            {
                tmp = value1;
                if (asprintf(&value1,"%s %s",tmp, b->value.text.string) == -1)
                {
                    return -1;
                }
                free(tmp);
                tmp=NULL;
            }
        }
        else
        {
            if (value1!=NULL)
            {
                printf("value1=%s",value1);
                free(value1);
                value1=NULL;
            }
        }
    if (b && b->type == MXML_TEXT &&
        b->value.text.string &&
        b->parent->type == MXML_ELEMENT &&
        !strcmp(b->parent->value.element.name, "value2"))
        {
            if(value2 != NULL)
            {
                value2 = strdup(b->value.text.string);
            }
            else
            {
                tmp = value2;
                if (asprintf(&value2,"%s %s",tmp, b->value.text.string) == -1)
                {
                    return -1;
                }
                free(tmp);
                tmp=NULL;
            }
        }
        else
        {
            if (value2!=NULL)
            {
                printf("value2=%s",value2);
                free(value2);
                value2=NULL;
            }
        }
    b = mxmlWalkNext(b, tree, MXML_DESCEND);
}
于 2013-07-15T09:13:43.957 回答