Qt平台下,修改xml文件。
#include<QtGui>
#include<QtXml>
#include<iostream>
#include"xmlstream.h"
#include"cscenario.h"
boolXmlStream::modiXml(constQString&fileName)
{
QFilefile(fileName);
if(!file.open(QFile::ReadOnly|QFile::Text))
{
std::cerr<<"Error:Cannotreadfile"<<qPrintable(fileName)
<<":"<<qPrintable(file.errorString())
<<std::endl;
returnfalse;
}
QStringerrorStr;
interrorLine;
interrorColumn;
QDomDocumentdoc;
if(!doc.setContent(&file,false,&errorStr,&errorLine,
&errorColumn))
{
std::cerr<<"Error:Parseerroratline"<<errorLine<<","
<<"column"<<errorColumn<<":"
<<qPrintable(errorStr)<<std::endl;
returnfalse;
}
file.close();
QDomElementroot=doc.documentElement();
QDomNodechild=root.firstChild();
while(!child.isNull())
{
if(child.toElement().tagName()=="name")
{
QDomElementel=child.toElement();
QDomNodeoldnode=el.firstChild();
el.firstChild().setNodeValue("chenXX");
QDomNodenewnode=el.firstChild();
el.replaceChild(newnode,oldnode);
std::cerr<<qPrintable(el.tagName())<<std::endl;
std::cerr<<qPrintable(el.text())<<std::endl;
child=child.nextSibling();
}
elseif(child.toElement().tagName()=="station")
{
QDomElementel=child.toElement();
QDomNodeoldnode=el.firstChild();
el.firstChild().setNodeValue("only one");
QDomNodenewnode=el.firstChild();
el.replaceChild(newnode,oldnode);
std::cerr<<qPrintable(el.tagName())<<std::endl;
std::cerr<<qPrintable(el.text())<<std::endl;
child=child.nextSibling();
}
elseif(child.toElement().tagName()=="qqqqqqqqqqqqqq")
{
QDomElementel=child.toElement();
QDomNodeoldnode=el.firstChild();
el.firstChild().setNodeValue("super man");
QDomNodenewnode=el.firstChild();
el.replaceChild(newnode,oldnode);
std::cerr<<qPrintable(el.tagName())<<std::endl;
std::cerr<<qPrintable(el.text())<<std::endl;
child=child.nextSibling();
}
elseif(child.toElement().tagName()=="a suiper man")
{
QDomElementel=child.toElement();
QDomNodeoldnode=el.firstChild();
el.firstChild().setNodeValue(
xmlScenario->getState("XXXXXXXX"));
QDomNodenewnode=el.firstChild();
el.replaceChild(newnode,oldnode);
std::cerr<<qPrintable(el.tagName())<<std::endl;
std::cerr<<qPrintable(el.text())<<std::endl;
child=child.nextSibling();
}
elseif(child.toElement().tagName()=="rnc_level")
{
QDomElementel=child.toElement();
QDomNodeoldnode=el.firstChild();
el.firstChild().setNodeValue(
xmlScenario->getState("perserres"));
QDomNodenewnode=el.firstChild();
el.replaceChild(newnode,oldnode);
std::cerr<<qPrintable(el.tagName())<<std::endl;
std::cerr<<qPrintable(el.text())<<std::endl;
child=child.nextSibling();
}
elseif(child.toElement().tagName()=="sssssss")
{
QDomElementel=child.toElement();
QDomNodeoldnode=el.firstChild();
el.firstChild().setNodeValue("sssssssssssssssssssssss");
QDomNodenewnode=el.firstChild();
el.replaceChild(newnode,oldnode);
std::cerr<<qPrintable(el.tagName())<<std::endl;
std::cerr<<qPrintable(el.text())<<std::endl;
child=child.nextSibling();
}
elseif(child.toElement().tagName()=="sssssssss")
{
QDomElementel=child.toElement();
QDomNodeoldnode=el.firstChild();
QStringmml="thisisceshideremove_mmlwenjian;";
//el.firstChild().setNodeValue(xmlScenario->getRncRemove());
el.firstChild().setNodeValue(mml);
QDomNodenewnode=el.firstChild();
el.replaceChild(newnode,oldnode);
std::cerr<<qPrintable(el.tagName())<<std::endl;
std::cerr<<qPrintable(el.text())<<std::endl;
child=child.nextSibling();
}
elseif(child.toElement().tagName()=="aaaaaaaaaaaaaaaa")
{
QDomElementel=child.toElement();
QDomNodenodebChild=el.firstChild();
QVector<CFG_ENTITY>::iteratorite;
QVector<CFG_ENTITY>m_cfgEntity;
m_cfgEntity=xmlScenario->getCfgEntity();
while(!nodebChild.isNull())
{
if(nodebChild.toElement().tagName()=="aaaaaaaaaaaaa")
{
for(ite=m_cfgEntity.begin();ite!=m_cfgEntity.end();++ite)
{
if(ite->entityName==nodebChild.toElement().text())
{
break;
}
}
std::cerr<<qPrintable(nodebChild.toElement().text())<<std::endl;
if(ite==m_cfgEntity.end())
{
}
nodebChild=nodebChild.nextSibling();
}
elseif(nodebChild.toElement().tagName()=="ccccccccccccc")
{
QDomElementel=nodebChild.toElement();
QDomNodeoldnode=el.firstChild();
el.firstChild().setNodeValue("ddddddddddd");
QDomNodenewnode=el.firstChild();
el.replaceChild(newnode,oldnode);
std::cerr<<qPrintable(el.tagName())<<std::endl;
std::cerr<<qPrintable(el.text())<<std::endl;
nodebChild=nodebChild.nextSibling();
}
elseif(nodebChild.toElement().tagName()=="ffffffffffffff")
{
QDomElementel=nodebChild.toElement();
QDomNodeoldnode=el.firstChild();
el.firstChild().setNodeValue("ggggggggggggggggg");
QDomNodenewnode=el.firstChild();
el.replaceChild(newnode,oldnode);
std::cerr<<qPrintable(el.tagName())<<std::endl;
std::cerr<<qPrintable(el.text())<<std::endl;
nodebChild=nodebChild.nextSibling();
}
else
{
nodebChild=nodebChild.nextSibling();
}
}
child=child.nextSibling();
}
else
{
child=child.nextSibling();
}
}
QFilef(fileName);
if(!f.open(QFile::WriteOnly|QFile::Text))
{
std::cerr<<"Error:Cannotreadfile"
<<qPrintable(fileName)
<<":"<<qPrintable(f.errorString())
<<std::endl;
returnfalse;
}
QTextStreamout(&f);
root.save(out,4);
returntrue;
}
浙公网安备 33010602011771号