0

I am currently writing code that shall interprete xml and extract relevant data for later processing. For this task I've included the pugixml module. As my code will grow with time surely requiring further data I have no idea of yet I consider my stump xml structure as pretty preliminary and likely to evolve. For that reason I would like my code to be flexible enough so I can easily manage changes to my xml file. To make it short: my xml file is not fixed and I may like to change node names and node structure later. I am not yet fiddling with flexibilizing node structure (though I have some ideas) but I concentrate on the more simple task to flexibilize node names that are accepted by my code.

To give an example to highlight this with code: I've created a class that shall contain the node "shape" which is child of a node "case_definition" which is child of the document root node "case". I later may introduce a child of case_definition that will have the child "shape" so that "shape" descended one step in the hierarchy of nodes. I also might change the name of the shape node to something else that may proove to be more striking than "shape".

So here's the method to take the node "shape" using pugixml:

//returns the node shape with a given shape_id number
pugi::xml_node nXml::tXmlShape::GetShapeNode(const int shape_id, const pugi::xml_node& root_node)
{   
    std::string tmp = std::to_string(shape_id);
    char const* char_shape_id = tmp.c_str();

    nXml::sXmlNodeNames sNodeNames;
    nXml::sXmlShapeTypes sShapeTypes;
    return root_node.child(sNodeNames.case_def_name.c_str()).find_child_by_attribute(sNodeNames.shape_name.c_str(),"item_id", char_shape_id);
}
;

In the return value you can see the structure of my xml file and this coding makes my structure currently pretty rigid. I have some ideas to flexibilize this my using recursive functions but I am not sure how to implement this at this moment.

But my core question right now is how more elegantly flexibilize the node names. At the moment I use a structure that contains all possible node names as strings so I can simply put different names later on.

const struct sXmlNodeNames
    {
        std::string root_name = "case";
        std::string case_def_name = "case_definition";
        std::string shape_name = "shape";
        std::string shape_name = "somethingelse";
    }
    ;

However, for some reason I'd prefer enum but I cannot figure out how I then could pass it to the pugixml method that wants c_strings. I would like to have less hard-coded node extraction methods where I simply could pass an enum instance that has been initialized with the proper enum option so it's either a shape or something else and then passes the correct string to the pugixml method. So in short I'd like to marry enum with the above struct functionality but I have not the slightest clue how to approach this.

  • 1
    Does this answer your question? https://stackoverflow.com/questions/28828957/enum-to-string-in-modern-c11-c14-c17-and-future-c20 – 463035818_is_not_an_ai Mar 05 '21 at 11:29
  • 2
    You can use an enum to index into an array, so you could put your nodenames in an array and index into that with you enum to get the right string. – super Mar 05 '21 at 11:46
  • @largest_prime_is: Thanks. That is an awesome link and I think it could answer my question although I was hoping for a more standardized way (maybe we get it with C++20 as far as I can understand). – ManyQuestions Mar 05 '21 at 11:53

0 Answers0