Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7695865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:38:47+00:00 2026-05-31T21:38:47+00:00

I have an application that builds a left menu that is based on a

  • 0

I have an application that builds a left menu that is based on a tree Qt component. In order to load it I need to parse a XML file. The XML file looks like:

<comandos>
        <categoria>
                <nome>Cupom fiscal</nome>
                <comando>
                        <nome>01 - Abrir Cupom Fiscal</nome>
                        <env>3</env>
                        <rec>4</rec>
                        <desc>CNPJ / CPF : </desc>
                        <desc>Nome : </desc>
                        <desc>Endereco: </desc>
                </comando>
        </categoria>
</comandos>

I can actually read this XML using QtDOM.

    QDomDocument doc( "ComandosML" );

    QFile file( "comandos.xml" );

    int r = 0;

    datafields.clear();
    receFields.clear();
    categories.clear();

    if( !file.open( QIODevice::ReadOnly ) )
      return -1;

    if( !doc.setContent( &file ) )
    {
      file.close();
      return -2;
    }

    // Ok we are ready to parse DOM
    QDomElement root = doc.documentElement();
    if( root.tagName() != "comandos" )
      return -3;

    QDomNode n = root.firstChild();
    while( !n.isNull() )
    {
          QDomElement e = n.toElement();
          if( !e.isNull() )
          {
            if( e.tagName() == "categoria" )
            {
                QDomNode cat = n.firstChild();
                while( !cat.isNull() )
                {
                    QDomElement CatName = cat.toElement();

                    if ( CatName.tagName() == "nome")
                    {
                        QString s = CatName.text();

                        if ( s != "")
                        {
                            categories.push_back(s);
                            item = new QStandardItem( (s) );
                            item->setEditable(false);
                        }
                    }

                    if ( CatName.tagName() == "comando")
                    {

                        QDomNode params = cat.firstChild();
                        QString qdCmd;
                        int env = 0;
                        int rec = 0;
                        Categories Desc;

                        while ( !params.isNull())
                        {
                           QDomElement ParamName = params.toElement();

                           if ( ParamName.tagName() == "nome")
                           {
                               qdCmd = ParamName.text();
                               child = new QStandardItem( (qdCmd) );
                               child->setEditable( false );
                               child->setDragEnabled(false);
                               item->appendRow( child );
                           }
                           else
                           if ( ParamName.tagName() == "env")
                           {
                               env = ParamName.text().toInt();
                           }
                           else
                           if ( ParamName.tagName() == "rec")
                           {
                               rec = ParamName.text().toInt();
                           }
                           else
                           if ( ParamName.tagName() == "desc")
                           {
                               Desc.push_back(ParamName.text());
                           }

                           params = params.nextSibling();
                        }

                        datafields.insert(pair<QString,int>(     qdCmd,      env    ));
                        receFields.insert(pair<QString,int>(     qdCmd,      rec    ));
                        descriptions.insert(pair<QString, Categories>( qdCmd, Desc) );
                    }
                    cat= cat.nextSibling();
                }
                model->setItem(r++,item);
            }
          }
          n = n.nextSibling();
    }

    file.close();

    return 0;

In between parsing I already assembly the menu. After all, I already have all set for updating the XML when the user edits the xml file and reloads at the application, I simply erase the tree and recreate it again. You can see that I also pass some data onto some structures, they are basically std::vector and std::map. This code above was written with examples from the Qt documentation, which are quite decent by the way.

It happens that I wrote a simple dialog to make the user avoiding editing the XML. Ok, for me it might be easier and simpler to edit the XML even from the user perspective, but the possible users will prefer to edit things on the dialog. This is all OK. I can grab the data pass it to the application. No trouble at all.

But I need to update the XML. Basically an edit will consist into updating the node by either adding a new one or inserting a child node into under . How do I update a node ? Is there any specific way to accomplish that ? My experiences with XML are short, I usually write, update, parse txt and binary files.

I want to do something like:

   if( root.tagName() != "comandos" )
      return -3;

    QDomNode n = root.firstChild();
    while( !n.isNull() )
    {
          QDomElement e = n.toElement();
          if( !e.isNull() )
          {
            if( e.tagName() == "categoria" )
            {
                QDomNode cat = n.firstChild();
                while( !cat.isNull() )
                {

                    QDomElement CatName = cat.toElement();

                    if ( CatName.tagName() == "nome")
                    {
                        QString s = CatName.text();

                        if ( s != qsCategory )
                        {
                            // we have not found the category
                            // add it here

                        }
                        else
                        {
                           // the category exists simply update
                        }



                    }

                    cat= cat.nextSibling();
                }
            }
          }
          n = n.nextSibling();
    }

It seems that using Qt Dom is quite decent for parsing and creating XML files, but it lacks the tools for updates. Any help would be much appreciated, even an example.

This other thread here, looks useful

Edit Value of a QDomElement?

I have looked over the internet for examples that would update a XML file. It seems that if I catch the current node I can add a child to it, so far I did not figure out how to do so.

Thanks for the help and obviously sorry for my ignorance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T21:38:48+00:00Added an answer on May 31, 2026 at 9:38 pm

    QDomElement newCategoriaTag = doc.createElement(QString("categoria")); 
    QDomElement newNomeTag = doc.createElement(QString("nome")); 
    QDomText newNomeText = doc.createTextNode(QString("Cupom fiscal 2"));
    newNomeTag.appendChild(newNomeText);
    newCategoriaTag.appendChild(newNomeTag);
    root.appendChild(newCategoriaTag);
    

    This will result in:

    <comandos>
            <categoria>
                    <nome>Cupom fiscal</nome>
                    <comando>
                            <nome>01 - Abrir Cupom Fiscal</nome>
                            <env>3</env>
                            <rec>4</rec>
                            <desc>CNPJ / CPF : </desc>
                            <desc>Nome : </desc>
                            <desc>Endereco: </desc>
                    </comando>
            </categoria>
            <categoria>
                    <nome>Cupom fiscal 2</nome>
            </categoria>
    </comandos>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a factory that builds the objects with longest lifetime in my application.
I have a fairly small solution that includes a WPF windows application. It builds
I have a web application that was build in c# and uses queries in
I have an application that I'm trying to build with at least a nominally
I have an application that sends messages to an external web service. I build
I have a web application that allows users to build a 'client list.' We
I have to build a web application that's similar to a branch/store locater. A
I have been asked to build a web application that will be used to
I have made a build system for my web application that's rewriting all resource
I have a application that like firefox, can be enhanced from plugins available from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.