我使用RapidXML来解析一串xml。 有我的字符串:
std::string str("<?xml version=\"1.0\" encoding=\"UTF-8\"
standalone=\"yes\"?><protocol version=\"1.5\"><srvResponse>
<dateTime>2016-10-18T08:51:50.657+01:00</dateTime><responseFrom ag=\"1\"
/><idMessage>0</idMessage><rejectionCode>0</rejectionCode>
</srvResponse></protocol>");
以下是我尝试解析的方法:
XML::xml_document<> doc;
doc.parse<0>((char*) str.c_str());
XML::xml_node<>* firstNode = doc.first_node();
然而,我得到的是它被解析为只有一个节点:protocol
,即兄弟姐妹,协议的子节点是空的,只是它没有。
我想我错过了一件主要的事情。
你能找到并告诉我吗?
谢谢。
答案 0 :(得分:1)
您可以将XML数据保存到文件,例如&#34; 1.xml&#34;,然后您可以执行以下操作:
<?xml version="1.0" encoding="utf-8"?>
<protocol version="1.5">
<srvResponse>
<dateTime>2016-10-18T08:51:50.657+01:00</dateTime>
<responseFrom ag="1"/>
<idMessage>0</idMessage>
<rejectionCode>0</rejectionCode>
</srvResponse>
</protocol>
void ParseWithAtrribute(std::string strFilePath /*= "1.xml"*/)
{
rapidxml::file<> docFile(strFilePath.c_str());
rapidxml::xml_document<> doc;
doc.parse<0>(docFile.data());
rapidxml::xml_node<> *pRootNode = doc.first_node();
if (pRootNode != NULL)
{
std::cout << pRootNode->name() << " " << pRootNode->value()<< std::endl; // protocol
rapidxml::xml_attribute<> *pAttr = pRootNode->first_attribute();
if (pAttr != NULL)
{
std::cout << pAttr->name() << " " << pAttr->value() << std::endl; // version
}
rapidxml::xml_node<> *pChildNode = pRootNode->first_node();
if (pChildNode != NULL)
{
std::cout << pChildNode->name() << " " << pChildNode->value() << std::endl;
rapidxml::xml_node<> * pSonNode = pChildNode->first_node();
for(; pSonNode != NULL; pSonNode = pSonNode->next_sibling())
{
std::cout << pSonNode->name() << " " << pSonNode->value() << std::endl;
rapidxml::xml_attribute<> *pSonAttr = pSonNode->first_attribute();
if (pSonAttr != NULL)
{
std::cout << " " << pSonAttr->name() << ":" << pSonAttr->value() << std::endl;
}
}
}
}
}
现在我们不需要将xml数据保存到文件:
void ParseWithAtrribute()
{
std::string str("<?xml version=\"1.0\" encoding=\"utf-8\"?> <protocol version=\"1.5\"> <srvResponse> <dateTime>2016-10-18T08:51:50.657+01:00</dateTime> <responseFrom ag=\"1\"/> <idMessage>0</idMessage> <rejectionCode>0</rejectionCode> </srvResponse> </protocol>");
rapidxml::xml_document<> doc;
doc.parse<0>((char *)(str.c_str()));
rapidxml::xml_node<> *pRootNode = doc.first_node();
if (pRootNode != NULL)
{
std::cout << pRootNode->name() << " " << pRootNode->value() << std::endl; // protocol
rapidxml::xml_attribute<> *pAttr = pRootNode->first_attribute();
if (pAttr != NULL)
{
std::cout << pAttr->name() << " " << pAttr->value() << std::endl; // version
}
rapidxml::xml_node<> *pChildNode = pRootNode->first_node();
if (pChildNode != NULL)
{
std::cout << pChildNode->name() << " " << pChildNode->value() << std::endl;
rapidxml::xml_node<> * pSonNode = pChildNode->first_node();
for (; pSonNode != NULL; pSonNode = pSonNode->next_sibling())
{
std::cout << pSonNode->name() << " " << pSonNode->value() << std::endl;
rapidxml::xml_attribute<> *pSonAttr = pSonNode->first_attribute();
if (pSonAttr != NULL)
{
std::cout << " " << pSonAttr->name() << ":" << pSonAttr->value() << std::endl;
}
}
}
}
}