PDA

View Full Version : QXmlStreamReader help?



nthung
4th October 2011, 03:47
This is a XML file. I want to use QXmlStreamReader to read the value of href, but I don't how to know to get the its value

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xbel>
<xbel version="1.0">
<folder folded="no">
<FullName>Perter</FullName>
<Age>38</Age>
</folder>
<bookmark href="http://qt.nokia.com/"/>
</xbel>

The XML file is produced by this code after modifing the code
http://www.java2s.com/Code/Cpp/Qt/UsingQXmlStreamWriter.htm
thanks very much
-------------------------
Once I have found the sulotion for that
http://www.qtcentre.org/threads/25077-Need-help-with-QXmlStreamReader
But my current problem is update the value of href ="http://qt.nokia.com/" to href="http://www.qtcentre.org/"
Thanks

ChrisW67
4th October 2011, 06:23
Load the document into a QDomDocument, find and modify any elements you want to, then save() the QDomDocument to a QTextStream (file).

nthung
4th October 2011, 09:03
THis is code for updating

QFile inFile("C:\\test.xml" );
inFile.open( QIODevice::ReadWrite | QIODevice::Text );
QDomDocument document;
document.setContent( &inFile ) ;


QDomElement documentElement = document.documentElement();
QDomNodeList elements = documentElement.elementsByTagName( "TextWatermark" );
QDomElement bar = elements.at(0).toElement();

if(bar.hasAttribute("FontName"))
bar.setAttribute("FontName","Thanh Hung");

QFile outFile( "C:\\test.xml" );
if( !outFile.open( QIODevice::WriteOnly | QIODevice::Text ) )
{
qDebug( "Failed to open file for writing." );
//return 0;
}

QTextStream stream( &outFile );
stream << document.toString();

outFile.close();