I want to make request to the web service, get the XML content, and parse it to get specific values returned by the service.
The code is to be written in native C++11 (MS Visual Studio 2013). The Cassablanca PPL library was chosen. For XML parsing, the XmlLite was chosen.
I am used to C++ programming; however, the async-task programming from the PPL library--the approach--is new to me. I know what the asynchronous programming is, and I know the principles of parallel programming. However, I am not used to using the continuations (.then(...)
), and I am only slowly wrapping my head around the concept.
So far, I have modified the examples to get the XML result and write it into the text file:
// Open a stream to the file to write the HTTP response body into.
auto fileBuffer = std::make_shared<concurrency::streams::streambuf<uint8_t>>();
file_buffer<uint8_t>::open(L"test.xml", std::ios::out)
.then([=](concurrency::streams::streambuf<uint8_t> outFile) -> pplx::task < http_response >
{
*fileBuffer = outFile;
// Create an HTTP request.
// Encode the URI query since it could contain special characters like spaces.
// Create http_client to send the request.
http_client client(L"http://api4.mapy.cz/");
// Build request URI and start the request.
uri_builder builder(L"/geocode");
builder.append_query(L"query", address);
return client.request(methods::GET, builder.to_string());
})
// Write the response body into the file buffer.
.then([=](http_response response) -> pplx::task<size_t>
{
printf("Response status code %u returned.\n", response.status_code());
return response.body().read_to_end(*fileBuffer);
})
// Close the file buffer.
.then([=](size_t)
{
return fileBuffer->close();
})
// Wait for the entire response body to be written into the file.
.wait();
Now, I need to understand how to modify the code to get the result that could be consumed XmlLite (Microsoft implementation that comes as xmllite.h
, xmllite.lib
, and xmllite.dll
. I know what pull parsers are. But again, I am very new to the library. I am still a bit lost in PPL related streams and other classes. I do not know how to use them correctly. Any explanation is higly welcome.
The cassablanca people say they use the XmlLite with the Cassablanca to process the results, but I did not find any example. Can you point me to some? Thanks.
Update (4th June 2014): The above code is actually wrapped as a function like that (wxString
comes from wxWidgets, but one can easily replace it by std::string
or std::wstring
):
std::pair<double, double> getGeoCoordinatesFor(const wxString & address)
{
...the above code...
...here should be the XML parsing code...
return {longitude, latitude};
}
The goal actually is instead of writing the stream to the test.xml
file to feed the XmlLite parser. The XML is rather small and it contains one or more (if the address is ambiguous) item elements with the x and y attributes that I want to extract -- like this:
<?xml version="1.0" encoding="utf-8"?>
<result>
<point query="Vítězství 27, Olomouc">
<item
x="17.334045"
y="49.619723"
id="9025034"
source="addr"
title="Vítězství 293/27, Olomouc, okres Olomouc, Česká republika"
/>
<item
x="17.333067"
y="49.61618"
id="9024797"
source="addr"
title="Vítězství 27/1, Olomouc, okres Olomouc, Česká republika"
/>
</point>
</result>
I do not need that test.xml
file. How to get the stream and how to redirect it to the XmlLite parser?