Important: Please read the Qt Code of Conduct -

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

  • the xml is:

    @<patient id="1">
    <clinical_department>A' Xeirourgiki Kliniki</clinical_department>
    <icd9_explanation>Malignant neoplasm of liver, primary</icd9_explanation>
    <description>meriki ektomi hpatos</description>
    <description>neoplasm of liver</description>

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

    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?

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

  • 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.

  • 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

  • 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.

  • [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...

  • 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...

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

  • @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

Log in to reply