如何在TinyXML中盲目解析

时间:2013-10-11 14:13:58

标签: xml tinyxml

我的XML知识已经足够了。

我想要做的是盲解析XML字符串并从根开始获取所有元素和属性。我不能像文档中的示例那样使用递归,我似乎无法掌握如何使用循环来完成它。我知道足以从XML字符串中获取根,并且可以遍历字符串,但似乎无法获取属性或文本值。

我不知道任何标签的名称,需要在解析时弄清楚它们。任何人都知道我是如何开始的还是一个例子?

由于

这是我到目前为止对于这样的XML字符串的代码,除非它的嵌套元素如位置和国家/地区有效:

    string strData = "<MyStuff mystring1=""111"" mystring2=""223"">\
                      <MYTAG1>0</MYTAG1>\
                      <MYTAG2>0</MYTAG3>\
                      <MYTAG4>0</MYTAG4>\
                      <location><country>GreatBritain</country></location>\
                      </MyStuff>";

void parseXmlString2( TiXmlNode* root )
{
TiXmlNode*         child;
TiXmlElement*      elem = (TiXmlElement*)root;
TiXmlAttribute*  pAttrib = elem->FirstAttribute();

//This gets the root and its attributes.
while(pAttrib)
{
    cout << "Value: " << pAttrib->Name() << ":" << pAttrib->Value() << endl;
    pAttrib=pAttrib->Next();
}

//Now I want to parse up the rest.
//Does not work if nested such as <location><country>GreatBritain</country></location>
for( child = root->FirstChild(); child; child = child->NextSibling() )
{

    //Test For child.
    if(child->FirstChild())
    {
        //My helper fuct to tell type
        //getType(child->FirstChild()->Type());
        TiXmlNode*         myChild;
        for( myChild = child->FirstChild(); myChild; myChild = child->IterateChildren(child->FirstChild()))
        {
            cout << " " << myChild->Value() << endl;

        }

    }
}

}

2 个答案:

答案 0 :(得分:1)

最终这对我有用,感谢所有人的投入:

for( child = root->FirstChild(); child; child = child->NextSibling() )
{

    if(child->Type() == TiXmlNode::TINYXML_ELEMENT)
    {

        thevalue = child->Value();
    }

    //If this Node has children traverse them.
    //and keep going for all.
    if(child->FirstChild())
    {
        TiXmlNode*  myChild  = child->FirstChild();
        while(myChild)
        {


            if(myChild->Type() == TiXmlNode::TINYXML_ELEMENT)
            {

                thevalue = child->Value();
            }

            if(myChild->Type() == TiXmlNode::TINYXML_TEXT)
            {

                thevalue= myChild->Value();
            }
            myChild  = myChild->FirstChild();
        }
    }
}

答案 1 :(得分:0)

我不知道我是否帮助你。但是,如果您已经在没有递归的情况下实现了文档的XMLElements的循环,那么循环遍历XMLAttributes应该很容易。

假设您想要从{<1}}获取属性,只需执行

XMLElement* elem

只是看看TinyXML2是否做得对,我写了下面的原语,递归函数打印出所有元素及其属性(没有值):

XMLAttribute* attrib;
for( attrib = elem->FirstAttribute(); attrib != NULL; attrib = attrib->Next() ) {
    // do something
    std::cout << attrib->Name() << " " << attrib->Value();
}

希望有所帮助。欢呼声。

编辑:如果没有应用程序的实际输出,很难猜出错误所在。但不应该是

void printAllChildren( XMLElement* parent, int recursionlvl ) {
    std::stringstream indent("");
    for( int i = 0; i <= recursionlvl; i++ ) {
        indent << " ";
    }

    std::cout << "Element Name: " << indent.str() << parent->Name() << " ";
    const XMLAttribute* attrib;
    for( attrib = parent->FirstAttribute(); attrib != NULL; attrib = attrib->Next() ) {
        std::cout << attrib->Name() << " ";
    }
    std::cout << std::endl;

    XMLElement* childEl;
    for( childEl = parent->FirstChildElement(); childEl != NULL; childEl = childEl->NextSiblingElement() ) {
        printAllChildren( childEl, recursionlvl + 1 );
    }
}

在你最内心的循环中? (见a TinyXML Reference