gpt4 book ai didi

c++ - boost read_xml 问题解析字符串?

转载 作者:太空宇宙 更新时间:2023-11-04 13:34:31 25 4
gpt4 key购买 nike

我正在尝试解析此字符串并检索“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;
}

谁能发现我可能做错了什么?

最佳答案

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?

In an XML document or external parsed entity, a CDATA section is a section of element content that is marked for the parser to interpret as only character data, not markup. wikipedia

总结

  • Boost Property Tree 不是 XML 解析库 ( xml parsing using boost )
  • CDATA 段不是 XML(因此即使使用 XML 库,您也不应该期望直接解析它)

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

关于c++ - boost read_xml 问题解析字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30132641/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com