Search code examples
c++xmltinyxml

Loading XML attribute with multiple values to different table cells


Say I have an XML file like the following:

<holidays>
    <holiday1 val="New Years, 01/01, First half"/>
    <holiday2 val="Chirstmas, 12/25, Second half"/>
    <holiday3 val="Valentines Day, 02/14, First half"/>
<holidays>

And I have a table with the following headings in which I need to load the data from the XML file to:

Holiday Name        First Half  Second Half

(I realize the table headings aren't really code but i formatted it with a code block so it's easier to read)

So basically what I want to happen is if the element has the attribute "First half" (of the year btw), then load the holiday name under holiday name, and date under first half. if the element has the attribute "second half", then load the holiday name under holiday name still, but load the date under second half. It should end up looking like this:

Holiday Name        First Half  Second Half
New Years           01/01
Chirstmas                       12/25
Valentines Day      02/14

I thought about using an Xpath to first check for first half or second half, and using that info select the correct cells to load the data. However, my biggest problem is I don't know how to select just the date, or just the holiday name.

some background info:
-the format of the xml file cannot be change, it was given to me like that, and unless it is absolutely impossible to achieve what I want to do, then the format cannot be change
-i am using tinyxml as my parser
-i am doing this in C++
-i do know how to load like the value of an element to a cell of a table, so i do have a little background knowledge to this

Thanks in advanced. Should further information be required to help you understand my problem, please leave a comment.


Solution

  • You're going to have to bite the bullet and parse the attribute string manually. You're going to have to search for "First Half" or "Second Half" manually, see if it's there, and if it is, do something based on that. You're going to have to separate each of the 3 comma-separated values within the attribute and work with them.

    I would suggest some form of regular expression parsing for cases this simple. Or maybe even std::string::find will work; it depends on what your needs are.