0
我有以下格式无法访问使用pugixml
<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文件,我想用一个循环由物体通过以下方式来访问我的文件对象使用循环XML节点:
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"))
{
}
尽管前两种方法没有打印预期结果(只有第一次迭代有效),但编译器说第三种方法是语法错误!
任何帮助!
感谢您的回答。它正在工作。 – Ibrahim
@Ibrahim如果zeuxcg的答案是正确的,请将其标记为给他信用。如果你一直给予答案的信贷,人们将更有可能回答你未来的问题。 –