Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How can i save data (with the same tag) from an xml to a qmap?
Forum Updated to NodeBB v4.3 + New Features

How can i save data (with the same tag) from an xml to a qmap?

Scheduled Pinned Locked Moved General and Desktop
9 Posts 5 Posters 5.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    annatz
    wrote on last edited by
    #1

    the xml is:

    @<patient id="1">
    <name>George</name>
    <surname>Papadakis</surname>
    <birthdate><d>2</d><m>5</m><y>1954</y></birthdate>
    <admission_date><d>17</d><m>4</m><y>2010</y></admission_date>
    <clinical_department>A' Xeirourgiki Kliniki</clinical_department>
    <ward>302</ward>
    <bed>3</bed>
    <primary_diagnosis>
    <icd9_code>155.0</icd9_code>
    <icd9_explanation>Malignant neoplasm of liver, primary</icd9_explanation>
    </primary_diagnosis>
    <surgical_operations>
    <operation>
    <oper_date><d>20</d><m>4</m><y>2010</y></oper_date>
    <description>meriki ektomi hpatos</description>
    </operation>
    <operation>
    <oper_date><d>27</d><m>4</m><y>2010</y></oper_date>
    <description>neoplasm of liver</description>
    </operation>
    </surgical_operations>
    </patient>@

    @
    void QXSRExample::parseSurgicalOperations(QXmlStreamReader& xml,QMap <QString,QString>& map) {
    while(!(xml.tokenType() == QXmlStreamReader::EndElement
    && xml.name() == "operations")) {
    if (xml.name()=="oper_date"){
    oper_date=parseDate(xml);
    map.insert(xml.name().toString(), oper_date);
    }
    if(xml.name() == "description"){
    descr=xml.readElementText();
    map.insert(xml.name().toString(), descr);
    }
    xml.readNext();
    }
    }
    @

    I have created the above function parseSurgicalOperations, in which I read the above xml file and I save the elements in a qmap. The xml has two operations and may have more. I want to read the operations and save them in the map, but each operation has the same tags (oper_date and description). How can I save them in the map?

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      I would say that you need a different data structure than a map...

      1 Reply Last reply
      0
      • JohanSoloJ Offline
        JohanSoloJ Offline
        JohanSolo
        wrote on last edited by
        #3

        Andre is right, a map is not what you want in this case.
        You may want to try something like :
        @
        struct OperationData
        {
        string oper_date;
        string descr;
        };

        vector< OperationData > operations;
        @

        and then use the operations container instead of your map. Of course this is a crude example, one can imagine to use a class instead of a struct for OperationData.

        `They did not know it was impossible, so they did it.'
        -- Mark Twain

        1 Reply Last reply
        0
        • A Offline
          A Offline
          annatz
          wrote on last edited by
          #4

          My whole program uses a qlist with qmap which contains all the patients with their data. When i search to find a specific patient how could i get the operation data? Is there another way to save the operation data in the qlist qmap? I think the struct doesnt help

          1 Reply Last reply
          0
          • JohanSoloJ Offline
            JohanSoloJ Offline
            JohanSolo
            wrote on last edited by
            #5

            OK, then you may use the patient's name as the key of your QMap, and then a QMultimap for the operations data.. like this :

            @
            QMap< QString, QMultiMap< QString, QString > > container;
            @
            with such a structure, I think you have what you need : you can easily look for a patient, and then have access to the (possibly multiple) operation data.
            But since in your XML snippet it seems that you have oper_date and description element nodes with a operation parent, you may want to add a class Operation, with data and description fields, so that your container can read :

            @
            class Operation;

            QMap< QString, Operation > container;
            @

            I see a problem if you blindly us a QMap, for you won't be able to link the date and description of the operation.

            `They did not know it was impossible, so they did it.'
            -- Mark Twain

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #6

              [quote author="annatz" date="1302847166"]My whole program uses a qlist with qmap which contains all the patients with their data.[/quote]

              This sounds like a serious design flaw. I strongly recommend you re-think this!

              Or do you really want to use constructs like

              @QMap< QString, QMultiMap< QString, QString > > container;@

              ?

              Where nothing is typed and no one ever knows what's where (think of yourself and your fellow prorammers - will eventually have to change the program in a couple of years!)

              Make distinct classes for your distinct entity types. That's what C++ is made for, not for creating arbitrary nested stacks of lists and maps...

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #7

                To make it real "fun", considder using a QMap< QString, QVariant > as a container. You can put anything you want in that QVariant, including new QMaps and QLists! How is that for fun! :-)

                /me runs and hides...

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  annatz
                  wrote on last edited by
                  #8

                  thanks. I solved the problem. I used a QMultiMap.

                  Z 1 Reply Last reply
                  0
                  • A annatz

                    thanks. I solved the problem. I used a QMultiMap.

                    Z Offline
                    Z Offline
                    zabdielfer
                    wrote on last edited by
                    #9

                    @annatz Hi:

                    how did you use qmultimap for resolve your problem? at this moment , I am in that issue, i don't know how I will resolve that writing qmap into xmlFile. thanks

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved