PDA

View Full Version : Updating xml attribute



steg90
17th July 2007, 08:40
Hi,

Just wondering what is the best way to update an attribute in xml file, I use QXmlStreamReader class for reading my xml and now want to modify some attributes within it.

The dbcfilename attribute shown below, this can change and I'd like to save the xml file with the new change :



<!DOCTYPE configurationXML>
<configuration>
<dbc>
<config dbcfilename="testx150hs.dbc" loadonstart="1"/>
</dbc>
<read>
<config limits="20000" />
</read>
</configuration>

Any advice is appreciated,
Regards,
Steve

fullmetalcoder
17th July 2007, 09:08
When one wants to have an easy-to-modify document structure that can be traversed and edited in any possible way (adding/removing/modifying elements/attributes...) it is highly recommened to use QDomDocument and related-classes. The stream-oriented classes are meant for fast parsing/writing of xml, not for any kind of editing...

steg90
17th July 2007, 09:17
Thanks,

I am using QDomDocument to create the xml, example :



QDomDocument doc( "configurationXML" );
QDomElement root = doc.createElement( "configuration" );
doc.appendChild( root );
QDomElement dbc = doc.createElement( "dbc" );
root.appendChild( dbc );
QDomElement read = doc.createElement( "read" );
root.appendChild( read );

// etc


and then using QTextStream to write the file.

I can't seem to find methods for actually amending the xml without having to re-create the dom...I know there must be some! Maybe setNodeValue?

Regards,
Steve

guilugi
17th July 2007, 09:26
In QDomDocument, you have methods to look for elements (QDomElement), and this last class has setAttribute : that's what you need :)