lundi 20 avril 2015

Standardize XML file tag naming and then do work on the values

I'm working with a lot of XML files which each have their own naming convention:

Filea.xml

<entry>
<id>1</id>
<information>Testing</information>
<url>http://ift.tt/OgFyc6;
</entry>
...

Fileb.xml

<entry>
<id>1</id>
<content>Testing</content>
<website>http://ift.tt/1aK5Sqg;
</entry>
...

Filec.xml

<entry>
<id>1</id>
<data>Testing</data>
<url>http://ift.tt/OgFyc6;
</entry>
...

In my code I use a map to define what tag contains what information, for example, for Fileb.xml:

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

Then when I grab an XML node from my file, I loop through my tag map, which looks for the specified tag names and assignes them to the type of content they are.

for (auto& kv : tagMap) {
        // This aims to grab the content of the file tag and names it correctly based on it's content
        kv.first=eb.second.child_value(kv.second.c_str());
}

Depending on the tag that I have found, i.e "description", I want to do work on it. What is the right approach to this, should I have an if command for each tag I want to do work on, something like this?

if (kv.first="id") {
//Do something on the ID, i.e check if it is unique 
}

if (kv.first="description") {
// DO something on the description, i.e trim, validate
}

I'm a little concerned I am taking the wrong approach with this as:

  1. I seem to define tags when when they match my naming convention {"id","id"}

  2. Since in the real implementation I will be grabbing the content from about 10 XML tags, it seems wrong to do a loop for each tag I want to grab.

Aucun commentaire:

Enregistrer un commentaire