【发布时间】:2015-05-08 20:50:07
【问题描述】:
我正在尝试解析此字符串并检索“sid”和“Type”。我有以下代码。它在get_child 行崩溃,我不完全确定为什么......
const boost::property_tree::ptree& empty_ptree(){
static boost::property_tree::ptree t;
return t;
}
int _tmain(int argc, _TCHAR* argv[])
{
struct SXMLElements
{
std::string strSessionId;
unsigned int uiTypeOfNotification;
};
std::string strXMLText = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\r\n"
"<NotificationSet vers=\"1.0\" svcid=\"session\" notid=\"42\">\r\n" "<Notification><![CDATA[<SessionNotification vers=\"1.0\" notid=\"42\">\r\n"
"<Session sid=\"sdfkljdsfkjjsf\">\r\n" "<Property name=\"CharSet\" value=\"UTF-8\"></Property>\r\n"
"</Session>\r\n" "<Type>5</Type>\r\n"
"<Time>324242</Time>\r\n"
"</SessionNotification>]]></Notification>\r\n"
"</NotificationSet>";
//// Parse the HTTP header Status line.
std::stringstream ss( strXMLText );
boost::property_tree::ptree xmlResponse;
//if (strXMLText.size() > 0)
//{
std::istringstream isResponse (strXMLText);
boost::property_tree::read_xml(isResponse, xmlResponse);
SXMLElements sXmlElem;
//const boost::property_tree::ptree & formats = xmlResponse.get_child("NotificationSet.Notification.Session", empty_ptree());
BOOST_FOREACH( boost::property_tree::ptree::value_type const& v, xmlResponse.get_child("NotificationSet.Notification.SessionNotification.Session") )
{
sXmlElem.strSessionId = xmlResponse.get<std::string>("<xmlattr>.sid", "");
sXmlElem.uiTypeOfNotification = xmlResponse.get<unsigned int>("Type", 0);
// }
}
//}
return 0;
}
谁能发现我做错了什么?
【问题讨论】:
-
呵呵。对于编辑,我还提供了答案:)
标签: c++ xml boost boost-propertytree