我有一个格式如下的 XML 文件
<Objects>
<Object>
<Id>1</Id>
<Name>Head</Name>
</object>
<Object>
<Id>2</Id>
<Name>Right Hand</Name>
</object>
<Object>
<Id>3</Id>
<Name>Left Hand</Name>
</object>
<Object>
<Id>4</Id>
<Name>Right hand</Name>
</object>
<Object>
<Id>5</Id>
<Name>Cup</Name>
</object>
<Object>
<Id>6</Id>
<Name>Book</Name>
</object>
</Objects>
我想使用循环通过以下方式逐个对象访问我的文件对象:
xml_node nodeObjects = doc.child("Objects").child("Object");
for(xml_node_iterator it = nodeObjects.begin(); it != nodeObjects.end(); ++it)
{
cout<<"Name = "<<it->child_value("Name")<<endl;
}
并且:
xml_node nodeObjects = doc.child("Objects");
for(xml_node nodeObject = nodeObjects.first_child(); nodeObject; nodeObject = nodeObject.next_sibling())
{
numOfObjects += 1;
const char *id = nodeObject.child_value("Id");
const char *name = nodeObject.child_value("Name");
//cout<<"ID = "<<id<<" Name = "<<name<<endl;
//cout<<nodeObjects.next_sibling("Object").child_value("Id")<<endl;;
}
最后:
xml_node nodeObjects = doc.child("Objects");
for(xml_node nodeObjects: nodeObjects.children("Object"))
{
}
虽然前两种方法没有打印出预期的结果(只有第一次迭代有效),但编译器说第三种方法是语法错误!
任何帮助!