如何解析 xml 响应?

How can I parse an xml response?

本文关键字:响应 xml 何解析      更新时间:2023-10-16

我想解析变量字符串中的xml响应。

我正在使用C++ RestSDK。

pplx::task<void> Azure::GetTranslateText(utility::string_t ocrText, utility::string_t &transText)
{
auto client = http_client{ U("https://api.microsofttranslator.com/V2/Http.svc") };
uri_builder query;
query.set_path(U("/Translate"));
query.append_query(U("appid"), appid);
query.append_query(U("text"), ocrText);
query.append_query(U("from"), U("en"));
query.append_query(U("to"), U("ko"));
auto path_query_fragment = query.to_string();
return client.request(methods::GET, path_query_fragment).then([&](http_response response)
{
auto bodyStream = response.body();
concurrency::streams::stringstreambuf sbuffer;
auto& target = sbuffer.collection();
bodyStream.read_to_end(sbuffer).get();
transText = utility::conversions::to_string_t(target);
});
}

XML 响应采用 transText 格式。

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/"/>translated text</string>

我找到了如何在没有xml文档的情况下解析。 使用 tinyxml2。

谢谢大家:D

tinyxml2::XMLDocument doc;
doc.Parse(target.c_str());
tinyxml2::XMLElement *elem = doc.FirstChildElement("string");
transText = utility::conversions::to_string_t(elem->GetText());