mercredi 22 avril 2015

XML parser - Process and spit out values

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 << VALUE << 'id';

This is the full code:

int main()
{

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

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("entry")) {
std::string id = node.child_value("id");
mapa[id] = node;
}

for (auto& node: docb.child("data").children("entry")) {
std::string idcs = node.child_value("id");

    if (!mapa.erase(idcs)) {
    mapb[idcs] = node;
    }
}

// Use this to find mapped nodes
std::map<std::string, std::string>::const_iterator found;

 for (auto& eb: mapb) {

         // try to find the node in the map
         found = tagMap.find(eb.second.child_value());

         if(found == tagMap.end()) // - FAIL
             continue; // unknown tag - ignore

         // test the corresponding (mapped) tag name

        if (found->second == "id") {
             std::cout << VALUE << 'id';
         }
         if (found->second == "description") {
                         if (found->second == "id") {
             std::cout << VALUE << 'description';
         }
         }
         if (found->second == "url") {
                         if (found->second == "id") {
             std::cout << VALUE << 'url';
         }
         }
         if (found->second == "location") {
                         if (found->second == "id") {
             std::cout << VALUE << 'location';
         }
         }
//         }
     }
}

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