1

我的 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;

        }

    }
}

}

4

2 回答 2

2

最终这对我有用,感谢大家的投入:

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();
        }
    }
}
于 2013-10-21T13:20:27.540 回答
0

我不知道我说得对不对。但是,如果您已经实现了循环遍历文档的 XMLElements 而无需递归,那么循环遍历 XMLAttributes 应该很容易。

假设你有XMLElement* elem你想要从简单地获取属性

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

为了看看 TinyXML2 是否正确,我编写了以下原始递归函数,它打印出所有元素及其属性(没有值):

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 );
    }
}

希望有帮助。干杯。

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

for( myChild = child->FirstChild(); myChild; myChild = child->IterateChildren( child ) )

在你最内心的循环中?(参见TinyXML 参考

于 2013-10-13T13:51:29.087 回答