mercredi 22 avril 2015

Extracting information based on XML tags

This code is designed to do the following:

  1. Find different nodes across two XML files based on ID
  2. Take the node that has been added in B.xml
  3. Extract the content from the tag name
  4. Process it and print it depending on what the tag name is mapped to.

I'm having a hard time with point 3 and 4. I've got as far as I think I can get with sudo code but I'm not sure I am taking the right approach. I would really appreciate any help.

#include "pugi/pugixml.hpp"
#include <iostream>
#include <string>
#include <map>


int main()
{

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")) {
    const char* id = node.child_value("id");
    mapa[id] = node;
}

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

    }
}

// Here I map what the content is to what the XML tag name is
const std::map<int, std:string> mapping = {{1, "id"},{2,"content"},{3,"web_address"}}

// Here we find what has been added in b.xml
for (auto& eb:mapb) {
    // Here we look at the tagMap
    for (auto& kv: tagMap) {
        if (kv.first == 1) {
        // Extra ID validation here in the future
                std:cout << eb.second.child_value(kv.second.c_str())); << "- ID"
        }
        if (kv.first == 2) {
        // Extra description validation here in the future
                std:cout << eb.second.child_value(kv.second.c_str())); << "- DESCRIPTION"
        }
        if (kv.first == 3) {
        // Extra description validation here in the future
                std:cout << eb.second.child_value(kv.second.c_str())); << "- URL"
        }
    }
}

Example input XML data

<data>
    <entry>
        <id>1</id>
        <location>New York</location>
        <redundant>Test</redundant>
        <content>Hello World</content>
        <web_address>http://ift.tt/1yK4hfs;
    </entry>
</data>

Required Output

1 - ID
Hello World - DESCRIPTION
www.google.com - URL

Aucun commentaire:

Enregistrer un commentaire