mercredi 22 avril 2015

Grab XML tag content and print it

I've had a bit of help with some code. The idea is it reads two xml files, finds the difference, maps the tag name to a category (I.e "content" tag is a description) and then does processing on the data depending on the category.

I'm having a hard time working out how to grab the tag content within the unordered map and print out the value. For example how to I print out the id that's found within the id tag?

This is the specific bit of code I'm referring to:

std::cout << ; // print ID

This is the full code:

#include "pugi/pugixml.hpp"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"

#include <iostream>
#include <string>
#include <map>

using namespace std;

std::string id;
std::string description;
std::string url;


int main()
{

    const std::map<std::string, std::string> tagMap {
        {"id", "id"}, {"description", "content"}, {"url", "web_address"}
    };

    pugi::xml_document doca, docb;
    std::map<std::string, pugi::xml_node> mapa, mapb;

    if (!doca.load_file("a.xml") || !docb.load_file("b.xml")) {
        std::cout << "Can't find input files";
        return 1;
    }

    for (auto& node: doca.child("data").children("item")) {
        const char* id = node.child_value("id");
        mapa[id] = node;
    }

    for (auto& node: docb.child("data").children("item")) {
        const char* idcs = node.child_value("id");
        if (!mapa.erase(idcs)) {
            mapb[idcs] = node;
        }
    }


    std::unordered_map<std::string, void(*)(const std::string&)> tasks = {
        { "id", [](const std::string& val) {
            std::cout << ; // print ID
        }},
        { "description", [](const std::string& val) {
            // Do work on returned value of content tag
                        std::cout << ;
        }},
        { "url", [](const std::string& val) {
            // Do work on returned value of web_address tag
                std::cout << ;
                }}
    };

    for (auto& eb: mapb) {
        for (auto& kv: tagMap) {
            auto it = tasks.find(kv.first);
            if (it != tasks.end()) {
                it->second(eb.second.child_value(kv.second.c_str()));
            }
        }
    }
}

Input

<data>
<item>
<id>1</id>
<content>Test</content>
<web_address>http://ift.tt/1yK4hfs;
<redundant>Not Needed</redundant>
</item>
</data>

Desired Output

1
Test
www.test.com

Aucun commentaire:

Enregistrer un commentaire