我试图解析这个字符串并检索" sid"和"类型"。我有以下代码。它在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;
}
有人能发现我可能做错了吗?
答案 0 :(得分:0)
Session
属性cd=""\"o=rrs,o=ces,maxtime=""\"64""\"
解码为
cd="o=rrs,o=ces,maxtime="64"
会留下无效的XML。
无论如何,所有""\"
都可以被\"
除此之外,一个简单的测试显示Boost Property Tree不喜欢<![CDATA[]]>
部分,这是正确的:What does <![CDATA[]]> in XML mean?
在XML文档或外部解析的实体中,CDATA部分是元素内容的一部分,标记为解析器仅解释为字符数据,而不是标记。 wikipedia
<强>概要强>
<强> Live On Coliru 强>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <iostream>
int main()
{
std::string const strXMLText = R"(<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<NotificationSet vers="1.0" svcid="session" notid="42">
<Notification><SessionNotification vers="1.0" notid="42">
<Session sid="sdfkljdsfkjjsf" stype="user" cid="uid=1,u=People,dc=r,dc=c" cd="o=rrs,o=ces,maxtime=64" maxidle="24">
<Property name="CharSet" value="UTF-8"></Property>
<Property name="ed" value="xxx"></Property>
<Property name="Sdle" value="sdl:asdadsad"></Property>
</Session>
<Type>5</Type>
<Time>324242</Time>
</SessionNotification></Notification>
</NotificationSet>
)";
//// Parse the HTTP header Status line.
std::stringstream ss(strXMLText);
boost::property_tree::ptree xmlResponse;
std::istringstream isResponse (strXMLText);
boost::property_tree::read_xml(isResponse, xmlResponse);
if (auto SessionNotification = xmlResponse.get_child_optional("NotificationSet.Notification.SessionNotification"))
{
struct SessionElement {
std::string id;
unsigned int uiTypeOfNotification;
};
if (auto Session = SessionNotification->get_child_optional("Session")) {
SessionElement elem {
Session->get("<xmlattr>.sid", ""),
SessionNotification->get("Type", 0u)
};
std::cout << "id: " << elem.id << ", type: " << elem.uiTypeOfNotification << "\n";
}
}
}
打印
id: sdfkljdsfkjjsf, type: 5