I have an application that creates a left menu based on a component of the Qt tree. To download it, I need to parse the XML file. The XML file looks like this:
<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 really 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;
Between the parsing, I am already collecting the menu. In the end, Iโm already set up to update XML, when the user edits the xml file and reloads in the application, I just delete the tree and create it again. You can see that I also pass some data to some structures, they are basically std :: vector and std :: map. This code above was written with examples from the Qt documentation, which, incidentally, are pretty decent.
It happens that I wrote a simple dialog so that the user avoids editing XML. Well, for me it would be simpler and easier to edit XML, even from the point of view of the user, but possible users would prefer to edit things in a dialog box. Everything is good. I can capture the data passed to them in the application. No problem.
But I need to update the XML. Basically, editing will consist of updating the node by adding a new one or inserting a child element of the node. How to update node? Is there any specific way to achieve this? My experience with XML is short, I usually write, update, parse txt and binaries.
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 ) {
Using Qt Dom seems pretty decent for parsing and creating XML files, but it lacks tools for updates. Any help would be greatly appreciated, even an example.
This other topic here looks useful.
Change QDomElement value?
I browsed the internet for examples that would update an XML file. It seems that if I catch the current node, I can add a child to it until I figured out how to do it.
Thanks for the help and obviously regret my ignorance.