0

I'm trying to load an XML file from an URL using pugixml parser. The problem is that if you want to load a file from URL you have to load it from memory which it's very confusing for me. This is what I've tried and it doesn't work:

char* source = "http://some_url.xml";
int size = 256;
char* buffer = new char[size];

memcpy(buffer, source, size);

pugi::xml_document doc;
doc.load_buffer_inplace(buffer, size);
...
delete[] buffer;

Here is their documentation: https://pugixml.org/docs/manual.html#loading.memory

Can someone who is more familiar with that stuff please give me an example of how to do it.

theg
  • 469
  • 2
  • 6
  • 21
  • 1
    This is not the way. It won't go fetch and download the file. What you are telling it is your xml file is 256 bytes and located in your buffer (where you put URL instead of the contents). – drescherjm Jun 19 '18 at 18:24
  • 1
    ***you have to load it from memory*** You can do this after you downloaded it using whatever library you are using to download from the web or if your library downloaded and saved the file to your filesystem you can use the file option. – drescherjm Jun 19 '18 at 18:27
  • 1
    @drescherjm It seems from the docs you are right. So @Rok try using `libcurl` for example to download the file to memory at first, and when the download complete, operate on it, the code you submitted is just parsing the URL as if it was XML – user9335240 Jun 19 '18 at 18:32
  • 1
    I just used that library yesterday. However I am using Qt for the communications and a client / server network. I had to use `pugixml` because the XML parsing builtin to `Qt` could no longer handle my 500+ MB XML data sets. It just returned empty text for an element that was over 500 MB. With `pugixml` I got the entire value (which was a sql database dump). – drescherjm Jun 19 '18 at 18:35
  • 2
    If you want to use `libcurl`, refer to this very simple [example](https://curl.haxx.se/libcurl/c/https.html) – user9335240 Jun 19 '18 at 18:37
  • Thanks guys! I've set up libcurl and tried this example from fvu https://stackoverflow.com/questions/1636333/download-file-using-libcurl-in-c-c but I still can't get it to work.. any ideas? – theg Jun 19 '18 at 19:44

1 Answers1

0

If anyone is interested.. this is how I solved it:

const char* f = "new_file.xml";
if (curl){
    const char* c_url = "some_url";

    FILE* ofile = fopen(f, "wb");
    if (!ofile) { fprintf(stderr, "Failed to open file: %s\n", strerror(errno)); }

    if (ofile){
        curl_easy_setopt(curl, CURLOPT_URL, c_url);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, ofile);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeData);
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);

        curl_easy_perform(curl);

        fclose(ofile);
    }
}
pugi::xml_document doc;
doc.load_file(f);

Thanks for all the help guys!

theg
  • 469
  • 2
  • 6
  • 21