簡體   English   中英

RapidXml:選擇first_attribute值

[英]RapidXml: Selecting a first_attribute value

我是RapidXml的入門者。

<catalog>
   <book id="bk101" value="ABCDEFGHIJKLMNOPQRSTUVWXYZ">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>
   <book id="bk102" value="ABCDEFGHIJKLMNOPQRSTUVWXYZ">
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-12-16</publish_date>
      <description>A former architect battles corporate zombies, 
      an evil sorceress, a

nd her own childhood to become queen 
      of the world.</description>
   </book>
</catalog>

對於此XML,我如何僅在id="bk102"時選擇,然后解析value="ABCDEFGHIJKLMNOPQRSTUVWXYZ"

提前謝謝大家!

解決方案非常簡單:

#include <cstddef>
#include <cstring>
#include <iostream>

#include "rapidxml.hpp"
#include "rapidxml_utils.hpp"

…

rapidxml::file<> xml_file("/path/to/xml/file");
rapidxml::xml_document<> xml_doc;
xml_doc.parse<0>(xml_file.data());

const rapidxml::xml_node<> *catalog_node = xml_doc.first_node("catalog");
if (catalog_node == NULL) {
    std::cout << "No \"catalog\" node!" << std::endl;
    return;
}

for (const rapidxml::xml_node<> *book_node = catalog_node->first_node("book");
    book_node != NULL;
    book_node = book_node->next_sibling()) {

    const rapidxml::xml_attribute<> *id_attribute = book_node->first_attribute("id");
    if (id_attribute == NULL || strcmp(id_attribute->value(), "bk102") != 0) {
        continue;
    }

    const rapidxml::xml_attribute<> *value_attribute = book_node->first_attribute("value");
    if (value_attribute == NULL) {
        continue;
    }

    std::cout << "Found \"value\" attribute with value: " << value_attribute->value() << std::endl;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM