【发布时间】:2014-09-16 23:38:24
【问题描述】:
我有一个如下的 XML 文件:
<transaction>
<day>20</day>
<month>2</month>
<year>2014</year>
<product>
<barcode>123456789012</barcode>
<type>Food</type>
<price>12</price>
<currency>gbp</currency>
<name>Oreo</name>
<quantity>10</quantity>
</product>
<product>
<barcode>123456789012</barcode>
<type>Food</type>
<price>12</price>
<currency>gbp</currency>
<name>Oreo</name>
<quantity>10</quantity>
</product>
现在我想用 TinyXML2 解析它并编写了以下代码:
int count = 0;
int product_count = 0;
std::string prod_id("product");
//Get first node inside the root node then start iterations from there
XMLNode* node = doc.FirstChild()->FirstChild();
for(node; node; node=node->NextSibling()){
std::cout << node->Value() << std::endl;
count++;
std::string tag( node->Value());
if(tag.compare(prod_id)){
std::cout << "Product found!" << std::endl;
product_count++;
}
}
std::cout << "There are " << count << " tags in total" << std::endl;
std::cout << "There are " << product_count << " products in total" << std::endl;
但是我得到的输出如下:
day
Product found!
month
Product found!
year
Product found!
product
product
There are 5 tags in total
There are 3 products in total
从本质上讲,代码出于某种原因表示 day == product。我在这里错过了什么?
【问题讨论】:
标签: c++ xml xml-parsing