简体   繁体   English

RapidXML解析卡在第一个节点C ++上

[英]RapidXML Parsing stuck on first node C++

When I run this it prints x: 400 Next: y continuously. 当我运行此命令时,它将连续打印x:400 Next:y。 Why doesn't it go to the next sibling when it repeats? 为什么重复时不进入下一个兄弟姐妹? I'm following an example from the RapidXML manual so it should work. 我正在遵循RapidXML手册中的示例,因此它应该可以工作。

data.xml data.xml

<?xml version="1.0"?>
<dimensions>
    <x>400</x>
    <y>400</y>
    <z>1</z>
</dimensions>

map.h 地图

#ifndef MAP_H_
#define MAP_H_
class map{
  public:
    std::vector<int> get_xml();
};
#endif

map.cpp map.cpp

#include <vector>
#include <iostream>  
#include "../lib/rapidxml.hpp"
#include "../lib/rapidxml_print.hpp"
#include "../lib/rapidxml_utils.hpp"

using std::vector;
using std::cout;
using std::endl;
using namespace rapidxml;

vector<int> map::get_xml(){
    file<> xmlFile("res/data.xml");
    cout<<"XML loaded"<<endl;
    xml_document<> doc;
    xml_node<> *dims=doc.first_node("dimensions");
    vector<int> values;
    cout<<"Dimensions loaded"<<endl;

    for(xml_node<> *dim=dims->first_node();dim;dim->next_sibling()){
        cout<<dim->name()<<": "<<dim->value()<<endl
        <<"Next: "<<dim->next_sibling()->name()<<endl;
        values.push_back(atoi(dim->value()));
    }
    cout<<"All values loaded"<<endl;
    return values;
}

main.cpp main.cpp

#include <vector>
#include "map.h"
map Map;
int main(int argc, char** argv){
    std::vector<int> dims=Map.get_xml();
    ...
}

RapidXML Docs say: RapidXML文件说:

function xml_node::next_sibling

Synopsis

xml_node<Ch>* next_sibling(const Ch *name=0, std::size_t name_size=0, bool case_sensitive=true) const;
Description

Gets next sibling node, optionally matching node name. Behaviour is undefined if node has no parent. Use parent() to test if node has a parent.
Parameters

name
Name of sibling to find, or 0 to return next sibling regardless of its name; this string doesn't have to be zero-terminated if name_size is non-zero
name_size
Size of name, in characters, or 0 to have size calculated automatically from string
case_sensitive
Should name comparison be case-sensitive; non case-sensitive comparison works properly only for ASCII characters
Returns

Pointer to found sibling, or 0 if not found.

So dim->next_sibling() does not modify dim itself. 所以dim->next_sibling()不会修改dim本身。 You have to type dim = dim->next_sibling() as the final parameter of the for loop. 您必须输入dim = dim->next_sibling()作为for循环的最终参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM