2

I would like to update a single attribute value of the following xml file:

<table>
    <obj ID="101103" name="name_a" type="nametype" cat="txt"/>
    <obj ID="101104" name="name_b" type="nametype" cat="txt"/>
    <obj ID="101105" name="name_c" type="nametype" cat="txt"/>
    <obj ID="101106" name="name_d" type="nametype" cat="txt"/>
    [...]
</table>

the code identifies the attribute to update by the "ID"-value. The ID comes first, the attribute (e.g. "name") follows.

static void xmlActions::writeXMLValue(QString XMLID, QString attrName, QString attrVal, QFile *XMLFile, bool newID)
{
    if(XMLFile->open(QIODevice::ReadOnly))
    {  
        //comes true if the ID is found:
        bool hold = false;

        if (XMLFile->open(QIODevice::ReadWrite))
        {
            QXmlStreamReader reader(XMLFile->readAll());

            while(!reader.atEnd())
            {
                reader.readNext();
                foreach(const QXmlStreamAttribute &attr, reader.attributes())
                {
                    if (attr.value().toString() == XMLID)
                    {
                        // the ID has been found, flag is set
                        hold = true;
                    }
                    if (attr.name().toString() == attrName)
                    {
                        // now we are searching for the attribute which value is to change. If it 'belongs' to the found ID (hold == true) we change it's value:
                        if (hold == true)
                        {
                                //do changes to the attribute value, when it's found.
                                //e.g. change "name_a" to "name_x"
                        }
                    }
                }
                //reset the flag.
                hold = false;   
            }         
        }
        XMLFile->close();
    }
}

My question is, how can I update that single value within this element.

leatherwolf
  • 99
  • 1
  • 6
  • Use QDomDocument. It is slower but allows modification. You could also combine QStreamWriter with your code but is much more difficult. – Silicomancer Sep 27 '14 at 13:27

0 Answers0