Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to delete and add data to a QDomDocument file



  • I have a xml file that I need to change according some variables.
    This is my xml:

    <?xml version='1.0' encoding='UTF-8'?>
    <fiducial_circle>
     <threshold>
      <value0 value="30"/>
      <value1 value="125"/>
      <value2 value="120"/>
      <value3 value="255"/>
     </threshold>
     <dilation_erosion>
      <value0 value="25"/>
      <value1 value="25"/>
     </dilation_erosion>
     <select_shape id="0">
      <value0 value="circularity"/>
      <value1 value="0.1"/>
      <value2 value="1"/>
      <value3 value="area"/>
      <value4 value="50"/>
      <value5 value="150"/>
      <value6 value="bulkiness"/>
      <value7 value="0.9"/>
      <value8 value="1.1"/>
     </select_shape>
     <select_shape id="1">
      <value0 value="roundness"/>
      <value1 value="0"/>
      <value2 value="0.5"/>
      <value3 value="width"/>
      <value4 value="167"/>
      <value5 value="1234"/>
      <value6 value="ratio"/>
      <value7 value="0.9"/>
      <value8 value="0.95"/>
     </select_shape>
     <metrology>
      <value0 value="7"/>
      <value1 value="25"/>
     </metrology>
    </fiducial_circle>
    

    I have a QVector<QVector<QString>> named list_select_shape whit the new variables: list_select_shape[0] = area, 0, 20, ratio, 0, 0.8 and list_select_shape[1] = area, 200, 220, roundness, 0, 0.7, dist_mean, 1, 3, holes, 0, 14.
    At this point, I need to eliminate every info from select_shape id="0" and select_shape id="1", add the new ones and save it. It is supposed to became like that:

    <?xml version='1.0' encoding='UTF-8'?>
    <fiducial_circle>
     <threshold>
      <value0 value="30"/>
      <value1 value="125"/>
      <value2 value="120"/>
      <value3 value="255"/>
     </threshold>
     <dilation_erosion>
      <value0 value="25"/>
      <value1 value="25"/>
     </dilation_erosion>
     <select_shape id="0">
      <value0 value="area"/>
      <value1 value="0"/>
      <value2 value="20"/>
      <value3 value="ratio"/>
      <value4 value="0"/>
      <value5 value="0.8"/>
     </select_shape>
     <select_shape id="1">
      <value0 value="area"/>
      <value1 value="200"/>
      <value2 value="220"/>
      <value3 value="roundness"/>
      <value4 value="0"/>
      <value5 value="0.7"/>
      <value6 value="dist_mean"/>
      <value7 value="1"/>
      <value8 value="3"/>
      <value9 value="holes"/>
      <value10 value="0"/>
      <value11 value="14"/>
     </select_shape>
     <metrology>
      <value0 value="7"/>
      <value1 value="25"/>
     </metrology>
    </fiducial_circle>
    

    I have this so far, but can delete all and when I can, I can't add more child

        QFile file(file_name_data + "_data.xml");
        QFile data(file_name_data + "_data.xml");
        QDomDocument xml_file;
        QDomElement xml_element, xml_function, xml_child;
        QString value;
        QVector<QString> list_select;
    
        if (!file.open(QIODevice::ReadOnly)) 
        {
            std::cout << "File not found!" << std::endl;
        }
        else
        {
            xml_file.setContent(&file); 
            file.close();
            xml_element = xml_file.documentElement();
            xml_function = xml_element.firstChildElement();
    
            while(!xml_function.isNull())
            {
                int index = 0;
                xml_child = xml_function.firstChildElement();
                while(!xml_child.isNull())
                {
                    if (xml_function.tagName().toStdString() == "threshold")
                    {
                        xml_child.setAttribute("value", list_threshold[index]);
                        index += 1;
                    }
                    else if (xml_function.tagName().toStdString() == "dilation_erosion")
                    {
                        xml_child.setAttribute("value", list_dilation_erosion [index]);
                        index += 1;
                    }
                    else if (xml_function.tagName().toStdString() == "select_shape")
                    {
                        if (xml_function.attribute("id", "No id found") == QString::number(count_ss-1))
                        {
                           //change tha xml data here
                        }
                    }
                    else if (xml_function.tagName().toStdString() == "metrology")
                    {
                        xml_child.setAttribute("value", list_metrology [index]);
                        index += 1; 
                    }
                    else
                    {
                        xml_function = xml_function.nextSiblingElement();
                    }
                    xml_child = xml_child.nextSiblingElement();
                }
                xml_function = xml_function.nextSiblingElement();
                if (list_select.size() != 0)
                {
                    list_select_shape << list_select;
                    list_select.clear();
                }
            }
        }
    
        data.open(QIODevice::ReadWrite);
        QTextStream stream(&data);
        stream << xml_file.toString();
        data.close();
    

    Can some one help me with my problem? Thank you


  • Lifetime Qt Champion

    Hi,

    You can take a look at the DOM bookmarks example.

    On a side note, there's no need for all these conversions to std::string.


Log in to reply