一个简单方法,
简单来说,读一个
xml文件,然后对某些节点进行修改(本例子没有修改,增加修改也很简单),然后
写入目标
xml文件的例子。 QFile srcFile(srcFileName); if (!srcFile.open(QFile::ReadOnly | QFile::Text)) { QMessageBox::warning(this,tr("Warning/ Convert"),tr("Cannot open soruce file %1:\n%2.") .arg(srcFileName) .arg(srcFile.errorString())); return; } QFile tarFile(tarFileName); if (!tarFile.open(QFile::WriteOnly | QFile::Text | QIODevice::Truncate)) { QMessageBox::warning(this,tr("Cannot open target file %1:\n%2.") .arg(tarFileName) .arg(tarFile.errorString())); srcFile.close(); return; } QXmlStreamReader reader(&srcFile); QXmlStreamWriter writer(&tarFile); writer.setAutoFormatting(true); writer.writeStartDocument(); while (!reader.atEnd()) { reader.readNext(); if(reader.isStartElement()) { QXmlStreamAttributes attr = reader.attributes(); writer.writeStartElement(reader.name().toString()); if(attr.size()>0) { writer.writeAttributes(attr); } } if(reader.isCharacters()) { writer.writeCharacters(reader.text().toString()); } if(reader.isEndElement()) { writer.writeEndElement(); } } writer.writeEndDocument(); srcFile.close(); tarFile.Close();
原文链接:https://www.f2er.com/xml/298729.html