tinyXML 元素访问的运行时错误

2024-01-02

昨天是我的第一次尝试。我试图在以下“new.xml”文件中捕获变量“time”

<?xml version="1.0" standalone=no>
<main>
 <ToDo time="1">
  <Item priority="1"> Go to the <bold>Toy store!</bold></Item>
  <Item priority="2"> Do bills</Item>
 </ToDo>
 <ToDo time="2">
  <Item priority="1"> Go to the Second<bold>Toy store!</bold></Item>
 </ToDo>
</main>

这是我的代码

TiXmlDocument doc("new.xml");
TiXmlNode * element=doc.FirstChild("main");
element=element->FirstChild("ToDo");
string temp=static_cast<TiXmlElement *>(element)->Attribute("time");

但我从第三行和第四行收到运行时错误。有人可以阐明这个问题吗?


在我看来,您忘记加载该文件。通常我会按照以下方式做一些事情:

TiXmlDocument doc("document.xml");
bool loadOkay = doc.LoadFile(); // Error checking in case file is missing
if(loadOkay)
{
    TiXmlElement *pRoot = doc.RootElement();
    TiXmlElement *element = pRoot->FirstChildElement();
    while(element)
    {
        string value = firstChild->Value(); // In your example xml file this gives you ToDo
        string attribute = firstChild->Attribute("time"); //Gets you the time variable
        element = element->NextSiblingElement();
    }
}
else
{
    //Error conditions
} 

希望这可以帮助

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

tinyXML 元素访问的运行时错误 的相关文章