C++ XML文件解析操作

1、XML文件格式:

<?xml version="1.0" ?>
<root>
    <Panel PanelName="WoodPanel" Length="2440" Width="1220" Thick="15" />
    <Panel PanelName="AluPanel" Length="2440" Width="1220" Thick="15" Height="20" />
</root>

2、XML文件生成操作

void WriteXml()
{
/*第一种
TiXmlDocument doc;
//根节点
TiXmlElement* root = new TiXmlElement("Panel");
doc.LinkEndChild(root);


//第一节点
TiXmlElement* WoodPanelElement = new TiXmlElement("WoodPanel");
root->LinkEndChild(WoodPanelElement);


//第二节点
TiXmlElement* LengthElement = new TiXmlElement("Length");
WoodPanelElement->LinkEndChild(LengthElement);


TiXmlElement* WidthElement = new TiXmlElement("Width");
WoodPanelElement->LinkEndChild(WidthElement);


TiXmlElement* ThickElement = new TiXmlElement("Thick");
WoodPanelElement->LinkEndChild(ThickElement);


doc.SaveFile("C:\\Users\\user\\Desktop\\重写\\b.xml");
doc.Clear();
*/
/*第二种
const char* xmlFile = "C:\\Users\\user\\Desktop\\重写\\b.xml";
TiXmlDocument doc;
TiXmlDeclaration* decl = new TiXmlDeclaration("1.0", "", "");
doc.LinkEndChild(decl);
TiXmlElement* firstLevel = new TiXmlElement("root");
for (int i = 0; i < 1; ++i)
{
TiXmlElement* secondLevel = new TiXmlElement("Panel");
secondLevel->SetAttribute("PanelName", "WoodPanel");
for (int j = 0; j < 3; ++j)
{
TiXmlElement* thirdLevel = new TiXmlElement(AttributeName[j]);
thirdLevel->LinkEndChild(new TiXmlText(Attribute[i][j]));
secondLevel->LinkEndChild(thirdLevel);
}
firstLevel->LinkEndChild(secondLevel);
}
doc.LinkEndChild(firstLevel);
doc.SaveFile(xmlFile);
*/
//第三种
const char* xmlFile = "C:\\Users\\user\\Desktop\\重写\\b.xml";
TiXmlDocument doc;
TiXmlDeclaration* decl = new TiXmlDeclaration("1.0", "", "");
doc.LinkEndChild(decl);
TiXmlElement* firstLevel = new TiXmlElement("root");
for (int i = 0; i < 1; ++i)
{
TiXmlElement* secondLevel = new TiXmlElement("Panel");
secondLevel->SetAttribute("PanelName", "WoodPanel");
secondLevel->SetAttribute("Length", "2440");
secondLevel->SetAttribute("Width", "1220");
secondLevel->SetAttribute("Thick", "15");
firstLevel->LinkEndChild(secondLevel);
}
doc.LinkEndChild(firstLevel);
doc.SaveFile(xmlFile);
}

3、XML文件解析操作

void ParseXML1(map<string, vector<double>>& map1)
{
const char * xmlFile = "C:\\Users\\user\\Desktop\\重写\\b.xml";
TiXmlDocument doc;
doc.LoadFile(xmlFile);


TiXmlElement* firstLevel = doc.RootElement();
TiXmlElement* secondLevel = firstLevel->FirstChildElement();
while (secondLevel != NULL)
{
TiXmlAttribute* pAttribute = secondLevel->FirstAttribute();  // 获得属性及数值
//std::cout << pAttribute->Name() << endl;
const char* ccAttribute = pAttribute->Value();
string str(ccAttribute);
std::cout << pAttribute->Value() << endl;

vector<double> vec1;
TiXmlAttribute* pAttributeSecond = pAttribute->Next();  // 获得属性及数值
while (pAttributeSecond)
{
std::cout << pAttributeSecond->Value() << endl;
const char* ccAttributeSecond = pAttributeSecond->Value();
double dAttributeSecond = atof(ccAttributeSecond);
vec1.push_back(dAttributeSecond);
pAttributeSecond = pAttributeSecond->Next();
}
map1[str] = vec1;
//delete pAttributeSecond;
secondLevel = secondLevel->NextSiblingElement();
}


//delete secondLevel;
//delete firstLevel;
}

4、数组元素应用操作

map<string, vector<double>> ConfigInfoMap;
ParseXML1(ConfigInfoMap);


/*
for (map<string, vector<double>>::iterator itrMap = ConfigInfoMap.begin(); itrMap != ConfigInfoMap.end(); itrMap++)
{
if (0==(itrMap->first).compare("WoodPanel"))
{
cout << itrMap->first;
for (vector<double>::iterator itrVec = itrMap->second.begin(); itrVec != itrMap->second.end(); itrVec++)
{
std::cout << *(itrVec); //读取数组元素操作
std::cout << ";";
std::cout << endl;
}
}
}*/
for (map<string, vector<double>>::iterator itrMap = ConfigInfoMap.begin(); itrMap != ConfigInfoMap.end(); itrMap++)
{
if (0 == (itrMap->first).compare("WoodPanel"))
{
cout << (itrMap->second)[0]<<endl;
cout << (itrMap->second)[1] << endl;
cout << (itrMap->second)[2] << endl;
}
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果需要源码可以发送邮件到sdiwen1982@sohu.com索取 /*************************************** 功能:读取xml文件 参数:[in] xml文件路径 返回:true读取成功 false读取失败 **************************************/ bool ReadXml(CString sXmlPath /*xml文件路径*/); /*************************************** 功能:写入xml文件 参数:[in] xml文件路径 返回:true写入成功 false写入失败 **************************************/ bool WriteXml(CString sXmlPath /*xml文件路径*/); /*************************************** 功能:释放空间 参数:无 返回:无 **************************************/ void Release(); /*************************************** 功能:删除指定的节点 参数:无 返回:true删除成功 false删除失败 **************************************/ bool DeleteNode(); /*************************************** 功能:删除当前节点的所有子节点,当前节点不删除 参数:无 返回:true删除成功 false删除失败 **************************************/ bool DeleteAllSon(); /*************************************** 功能:根据索引删除子节点 参数:无 返回:true删除成功 false删除失败 **************************************/ bool DeleteSonByIndex(unsigned int nIndex); /*************************************** 功能:删除节点所有属性 参数:无 返回:true删除成功 false删除失败 **************************************/ bool DeleteAllAttr(); /*************************************** 功能:根据属性名删除属性 参数:无 返回:true删除成功 false删除失败 **************************************/ bool DeleteAttrByName(CString sName); /*************************************** 功能:根据索引删除属性 参数:无 返回:true删除成功 false删除失败 **************************************/ bool DeleteAttrByIndex(unsigned int nIndex); /*************************************** 功能:根据属性名设置属性值 参数:[in] sName 属性名 [in] sValue 属性值 返回:true设置成功 false设置失败 **************************************/ bool SetAttrValueByName(CString sName, CString sValue); /*************************************** 功能:插入属性 参数:[in] sName 属性名 [in] sValue 属性值 返回:true插入成功 false插入失败 **************************************/ bool InsertAttr(CString sName, CString sValue); /*************************************** 功能:设置节点值 参数:[in] sValue 属性值 返回:true设置节点值成功 false设置节点值失败 **************************************/ bool SetNodeValue(CString sValue); /*************************************** 功能:插入子节点 参数:[in] sName 节点名 返回:非空表示插入节点成功,返回插入的新节点指针 空表示插入失败 **************************************/ CXMLNode * InsertNode(CString sName); /*************************************** 功能:判断是否存在子节点 参数:[in] pNode 子节点文件指针, 返回:false没有子节点 true有子节点 **************************************/ bool HasChild(MSXML2::IXMLDOMNodePtr pNode);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值