• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

cyz88

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

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;
}

posted on 2012-09-25 16:22  cyz88  阅读(1081)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3