使用TinyXML进行xml文件解析,生成xml文件


xml文件解析:


void Load()
{
std::string filePath = "project.xml";
TiXmlDocument* xmlDoc = new TiXmlDocument(filePath.c_str());
xmlDoc->LoadFile();//加载xml文件


TiXmlElement* firstNodeEle = xmlDoc->RootElement();//获取根节点


TiXmlElement* secondNodeEle;
secondNodeEle = firstNodeEle->FirstChildElement();//根节点的第一个子节点


TiXmlElement* thirdNodeEle; 
TiXmlElement* fourthNodeEle;
TiXmlAttribute* attr;


while(secondNodeEle)//采用循环遍历
{
thirdNodeEle = secondNodeEle->FirstChildElement();
while(thirdNodeEle)
{
string str = secondNodeEle->Value();
if(str == "Points")
{
attr = thirdNodeEle->FirstAttribute();


PointHolder* pHolder = new PointHolder();


while(attr)
{
string pStr = attr->Name();
if(pStr == "Name")
{
pStr = attr->Value();
pStr = CGb2312String(pStr.c_str());
pHolder->name = pStr;
}
else if(pStr == "ImageName")
{
pStr = attr->Value();
pStr = CGb2312String(pStr.c_str());
pHolder->imageName = pStr;
}
else if(pStr == "Text")
{
pStr = attr->Value(); 
pStr = CGb2312String(pStr.c_str());
pHolder->text = pStr;


}
else if(pStr == "IsVisible")
{
pStr = attr->Value();
pHolder->isVisible = atoi(pStr.c_str());
}
else if(pStr == "X")
{
pStr = attr->Value();
pHolder->point.x() = atof(pStr.c_str());
}
else if(pStr == "Y")
{
pStr = attr->Value();
pHolder->point.y() = atof(pStr.c_str());
}
else if(pStr == "Z")
{
pStr = attr->Value();
pHolder->point.z() = atof(pStr.c_str());
}
attr = attr->Next();
}


QString qName = QString::fromLocal8Bit(pHolder->name.c_str());
QString qImageName = QString::fromLocal8Bit(pHolder->imageName.c_str());;
m_DOMng->UpdateIcon(qName, qImageName, pHolder->point);

delete pHolder;
}
else if(str == "Lines")
{
attr = thirdNodeEle->FirstAttribute();


LineHolder* lHolder = new LineHolder();


while(attr)
{
string lStr = attr->Name();
if(lStr == "Name")
{
lStr = attr->Value();
lStr = CGb2312String(lStr.c_str());//对中文进行转码
lHolder->name = lStr;
}
else if(lStr == "Color")
{
lStr = attr->Value();
lHolder->color = lStr;/
}
else if(lStr == "Width")
{
lStr = attr->Value(); 
lHolder->width = atof(lStr.c_str());


}
else if(lStr == "LineStyle")
{
lStr = attr->Value();
lHolder->lineStyle = lStr;
}
else if(lStr == "IsClampTerrain")
{
lStr = attr->Value();
lHolder->isClampTerrain = atoi(lStr.c_str());//
}
else if(lStr == "IsVisible")
{
lStr = attr->Value();
lHolder->isVisible = atoi(lStr.c_str());///
}
attr = attr->Next();
}


fourthNodeEle = thirdNodeEle->FirstChildElement();
while(fourthNodeEle)
{
attr = fourthNodeEle->FirstAttribute();
while(attr)
{
string lpStr = attr->Name();
osg::Vec3d lp;
if(lpStr == "X")
{
lpStr = attr->Value();
lp.x() = atof(lpStr.c_str());
}
else if(lpStr == "Y")
{
lpStr = attr->Value();
lp.y() = atof(lpStr.c_str());
}
else if(lpStr == "Z")
{
lpStr = attr->Value();
lp.z() = atof(lpStr.c_str());
}
lHolder->line.push_back(lp);
attr = attr->Next();
}


fourthNodeEle = fourthNodeEle->NextSiblingElement();
}


//m_DOMng->addline(lHolder);


delete lHolder;
}
else if(str == "Polygons")
{
attr = thirdNodeEle->FirstAttribute();


PolygonHolder* pgHolder = new PolygonHolder();


while(attr)
{
string pgStr = attr->Name();
if(pgStr == "Name")
{
pgStr = attr->Value();
pgStr = CGb2312String(pgStr.c_str());
pgHolder->name = pgStr;
}
else if(pgStr == "LineColor")
{
pgStr = attr->Value();
pgHolder->lineColor = pgStr;/
}
else if(pgStr == "FillColor")
{
pgStr = attr->Value();
pgHolder->fillColor = pgStr;/
}
else if(pgStr == "LineWidth")
{
pgStr = attr->Value(); 
pgHolder->lineWidth = atof(pgStr.c_str());


}
else if(pgStr == "LineStyle")
{
pgStr = attr->Value();
pgHolder->lineStyle = pgStr;///
}
else if(pgStr == "IsClampTerrain")
{
pgStr = attr->Value();
pgHolder->isClampTerrain = atoi(pgStr.c_str());///
}
else if(pgStr == "IsVisible")
{
pgStr = attr->Value();
pgHolder->isVisible = atoi(pgStr.c_str());
}
attr = attr->Next();
}


fourthNodeEle = thirdNodeEle->FirstChildElement();


while(fourthNodeEle)
{
attr = fourthNodeEle->FirstAttribute();
while(attr)
{
string pgpStr = attr->Name();
osg::Vec3d pgp;
if(pgpStr == "X")
{
pgpStr = attr->Value();
pgp.x() = atof(pgpStr.c_str());
}
else if(pgpStr == "Y")
{
pgpStr = attr->Value();
pgp.y() = atof(pgpStr.c_str());
}
else if(pgpStr == "Z")
{
pgpStr = attr->Value();
pgp.z() = atof(pgpStr.c_str());
}
pgHolder->polygon.push_back(pgp);
attr = attr->Next();
}


fourthNodeEle = fourthNodeEle->NextSiblingElement();
}


//m_DOMng->addpolgygon(pgHolder);


delete pgHolder;
}
else if(str == "Models")
{
attr = thirdNodeEle->FirstAttribute();


ModelHolder* mHolder = new ModelHolder();
while(attr)
{
string mStr = attr->Name();
if(mStr == "Name")
{
mStr = attr->Value();
mStr = CGb2312String(mStr.c_str());
mHolder->name = mStr;
}
else if(mStr == "ModelName")
{
mStr = attr->Value();
mStr = CGb2312String(mStr.c_str());
mHolder->modelName = mStr;
}
else if(mStr == "Scale")
{
mStr = attr->Value();
mHolder->scale= atof(mStr.c_str());
}
else if(mStr == "IsVisible")
{
mStr = attr->Value();
mHolder->isVisible = atoi(mStr.c_str());//
}
attr = attr->Next();
}


fourthNodeEle = thirdNodeEle->FirstChildElement();
while(fourthNodeEle)
{
string mStr = fourthNodeEle->Value();
if(mStr == "Point")
{
attr = fourthNodeEle->FirstAttribute();
while(attr)
{
string pgpStr = attr->Name();
if(pgpStr == "X")
{
pgpStr = attr->Value();
mHolder->point.x() = atof(pgpStr.c_str());
}
else if(pgpStr == "Y")
{
pgpStr = attr->Value();
mHolder->point.y() = atof(pgpStr.c_str());
}
else if(pgpStr == "Z")
{
pgpStr = attr->Value();
mHolder->point.z() = atof(pgpStr.c_str());
}
attr = attr->Next();
}
}
else if(mStr == "Attribute")
{
attr = fourthNodeEle->FirstAttribute();
while(attr)
{
string pgpStr = attr->Name();
if(pgpStr == "Heading")
{
pgpStr = attr->Value();
mHolder->heading = atof(pgpStr.c_str());
}
else if(pgpStr == "Pitch")
{
pgpStr = attr->Value();
mHolder->pitch = atof(pgpStr.c_str());
}
else if(pgpStr == "Roll")
{
pgpStr = attr->Value();
mHolder->roll = atof(pgpStr.c_str());
}
attr = attr->Next();
}
}

fourthNodeEle = fourthNodeEle->NextSiblingElement();
}
//UpdateModel(rm.name,"airplane",osg::Vec3d(rm.lon,rm.lat,rm.alt),0,0,0);
QString qName = QString::fromLocal8Bit(mHolder->name.c_str());
QString qModelName = QString::fromLocal8Bit(mHolder->modelName.c_str());;
m_DOMng->UpdateModel(qName, qModelName, mHolder->point, mHolder->heading, mHolder->pitch, mHolder->roll);


delete mHolder;
}


thirdNodeEle = thirdNodeEle->NextSiblingElement();
}


secondNodeEle = secondNodeEle->NextSiblingElement();
}


if(firstNodeEle)  
{  
delete firstNodeEle;  
firstNodeEle = 0;  
}
if(secondNodeEle)  
{  
delete secondNodeEle;  
secondNodeEle = 0;  
}
if(thirdNodeEle)  
{  
delete thirdNodeEle;  
thirdNodeEle = 0;  
}
if(fourthNodeEle)  
{  
delete fourthNodeEle;  
fourthNodeEle = 0;  
}
if(attr)  
{  
delete attr;  
attr = 0;  
}
/*if(xmlDoc)
{
delete xmlDoc;
xmlDoc = 0;
}*/
}




生成xml文件:


void Save()
{
TiXmlDocument* saveDocument = new TiXmlDocument();
TiXmlDeclaration* declaration = new TiXmlDeclaration("1.0", "", "");
saveDocument->LinkEndChild(declaration);
TiXmlElement* root = new TiXmlElement("Project");
saveDocument->LinkEndChild(root);


TiXmlElement* firstNodeEle = new TiXmlElement("Points");
TiXmlElement* secondNodeEle = new TiXmlElement("Models");
TiXmlElement* thirdNodeEle = new TiXmlElement("Lines");
TiXmlElement* fourthNodeEle = new TiXmlElement("Polygons");
root->LinkEndChild(firstNodeEle);
root->LinkEndChild(secondNodeEle);
root->LinkEndChild(thirdNodeEle);
root->LinkEndChild(fourthNodeEle);


m_list = m_DOMng->objList();//数据来源
std::map<QString,object*>::iterator firstIter;
for(firstIter = m_list.begin(); firstIter != m_list.end(); ++firstIter)
{
if(model* dModel = dynamic_cast<model*>(firstIter->second))//判断是否是相应对象
{
TiXmlElement* ele = new TiXmlElement("Model");
secondNodeEle->LinkEndChild(ele);


string str = dModel->getName().toUtf8().data();
str = CGb2312String(str.c_str());
ele->SetAttribute("Name", str.c_str());

str = dModel->getFileName().toUtf8().data();
str = CGb2312String(str.c_str());
ele->SetAttribute("ModelName", str.c_str());


ele->SetAttribute("Scale", "3");


if(dModel->getModelVisible())
str = "1";
else
str = "0";
ele->SetAttribute("IsVisible", str.c_str());




TiXmlElement* ele1 = new TiXmlElement("Point");
ele->LinkEndChild(ele1);


osg::Vec3d pos = dModel->getPos();
QString qstr;
double d = pos.x();
qstr.setNum(d);
str = qstr.toStdString();
ele1->SetAttribute("X",str.c_str());


d = pos.y();
qstr.setNum(d);
str = qstr.toStdString();
ele1->SetAttribute("Y",str.c_str());


d = pos.z();
qstr.setNum(d);
str = qstr.toStdString();
ele1->SetAttribute("Z",str.c_str());




TiXmlElement* ele2 = new TiXmlElement("Attribute");
ele->LinkEndChild(ele2);


d = dModel->getHeading();
qstr.setNum(d);
str = qstr.toStdString();
ele2->SetAttribute("Heading", str.c_str());


d = dModel->getPitch();
qstr.setNum(d);
str = qstr.toStdString();
ele2->SetAttribute("Pitch", str.c_str());


d = dModel->getRoll();
qstr.setNum(d);
str = qstr.toStdString();
ele2->SetAttribute("Roll", str.c_str());
}
if(icon* dIcon = dynamic_cast<icon*>(firstIter->second))//判断是否是相应对象
{
TiXmlElement* ele = new TiXmlElement("Point");
firstNodeEle->LinkEndChild(ele);

string str = dIcon->getName().toStdString();
str = CUtf8String(str.c_str());
ele->SetAttribute("Name",str.c_str());

str = dIcon->getFileName().toStdString();
str = CUtf8String(str.c_str());
ele->SetAttribute("ImageName", str.c_str());

str = dIcon->getText();
ele->SetAttribute("Text", str.c_str());

if(dIcon->getIconVisible())
str = "1";
else
str = "0";
ele->SetAttribute("IsVisible",str.c_str());


osg::Vec3d pos = dIcon->getPos();
QString qstr;
double d = pos.x();
qstr.setNum(d);
str = qstr.toStdString();
ele->SetAttribute("X",str.c_str());


d = pos.y();
qstr.setNum(d);
str = qstr.toStdString();
ele->SetAttribute("Y",str.c_str());


d = pos.z();
qstr.setNum(d);
str = qstr.toStdString();
ele->SetAttribute("Z",str.c_str());
}
}


//for()
/*





*/


saveDocument->SaveFile("new.xml");

if(firstNodeEle)  
{  
delete firstNodeEle;  
firstNodeEle = 0;  
}
if(secondNodeEle)  
{  
delete secondNodeEle;  
secondNodeEle = 0;  
}
if(thirdNodeEle)  
{  
delete thirdNodeEle;  
thirdNodeEle = 0;  
}
if(fourthNodeEle)  
{  
delete fourthNodeEle;  
fourthNodeEle = 0;  
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值