【问题标题】:Got tag name, but no values while parsing with miniXML获得标签名称,但在使用 miniXML 解析时没有值
【发布时间】:2014-03-18 09:50:33
【问题描述】:

为什么这段代码只检索标签,而不检索值?

int fromString(char* str)
{
    mxml_node_t *root, *node;   
    root  = mxmlLoadString(NULL,str, MXML_TEXT_CALLBACK);
    node = root;
    const char *textValue;
    char  bbb [256];
    const char *name;
    int aaa;
    mxmlGetElement(node);

    if (!strcmp(mxmlGetElement(node),"Root" ))
        {
        node = mxmlGetFirstChild(node);

        while (name= mxmlGetElement(node))
            {
            if (!strcmp(name,"AAA" ))
                {
                    cout<<"getting AAA"<<endl; 
                    aaa= mxmlGetInteger(node);
                    cout<<aaa<<std::endl;
                } else
            if (!strcmp(name,"BBB" ))
                {
                    cout<<"getting BBB"<<endl;
                    textValue=mxmlGetText(node,0);
                    if (textValue!=NULL) 
                        {
                        strcpy(bbb,textValue);
                        cout<<textValue<<std::endl;
                        }
                }
            node = mxmlGetNextSibling(node);
            }
        return 0;
        } else return -1;
}


int _tmain(int argc, _TCHAR* argv[])
{
    fromString("<Root><AAA>12</AAA><BBB>txt</BBB></Root>");

    int i ;

    cin>>i;
}

输出:

getting AAA
0
getting BBB

【问题讨论】:

    标签: c++ xml xml-parsing


    【解决方案1】:

    在你显示的 XML sn-p 中:

    <Root>
     <AAA>12</AAA>
     <BBB>txt</BBB>
    </Root>
    

    我很确定12 是一个单独的整数节点,它是AAA 节点的子节点。因此,您必须遍历该节点的子节点,才能找到包含的整数。

    请参阅文档中的initial node structure example

    【讨论】:

    • 我试图从 AAA 节点中再提取一个节点,但这没有帮助:cnode = mxmlGetFirstChild(node); cout
    猜你喜欢
    • 1970-01-01
    • 2015-10-06
    • 2014-03-27
    • 1970-01-01
    • 1970-01-01
    • 2021-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多