简体   繁体   中英

Reading out XML serves unexpected output in C++

I have a question about reading from a XML file.

My XML File:

<?xml version="1.0" encoding="UTF-8"?>
    <level>
       <terrain name="terrain" mesh="terrain.x" texture="terrain.bmp" position="011"></terrain>
       <skybox name="skybox" mesh="skybox.x" left="left.jpg" right="right.jpg" bottom="bottom.jpg" top="top.jpg" back="back.jpg" front="front.jpg" position="111"></skybox>
       <entity name="tiger" mesh="tiger.x" texture="tiger.bmp" position="000"></entity>
</level>

My code:

rapidxml::xml_document<> doc;
rapidxml::xml_node<>* root;

std::ifstream file("level\\noob.xml");

    std::vector<char> buffer((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
    buffer.push_back('\0');

    doc.parse<0>(&buffer[0]);

    root = doc.first_node("level");

    for (rapidxml::xml_node<>* terrain = root->first_node("terrain"); terrain; terrain = terrain->next_sibling())
    {
        std::cout << terrain->first_attribute("name")->value() << std::endl;
    }

So I only want to print the name of the terrain. But the outcome when running is this:

terrain
skybox
tiger

I check for the node "terrain" but it still prints the nodes "skybox" and "tiger". How?

for循环从节点“ terrain”开始,然后进行到同级兄弟,即skybox和Tiger。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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