C++使用TinyXML怎么为空字段写入数据
如<?xmlversion="1.0"encoding="gb2312"?><StudentInfo><NORMAL><NAME>1</NAME><ID></ID><LE...
如<?xml version="1.0" encoding="gb2312" ?>
<StudentInfo>
<NORMAL>
<NAME>1</NAME>
<ID></ID> <LESSON />
</NORMAL></StudentInfo>
如何为ID、LESSON 写入TEXT? 展开
<StudentInfo>
<NORMAL>
<NAME>1</NAME>
<ID></ID> <LESSON />
</NORMAL></StudentInfo>
如何为ID、LESSON 写入TEXT? 展开
1个回答
2013-07-16
展开全部
bool GetNodePtrByName(TiXmlElement *pRootEle, const std::string& sNodeName, TiXmlElement *pEle)
{
if (pRootEle->Value() == sNodeName)
{
pEle = pRootEle;
return true;
}
for ( TiXmlElement *pEleTemp = pRootEle->FirstChildElement(); pEleTemp;
pEleTemp->NextSiblingElement() )
{
if ( GetNodePtrByName(pEleTemp, sNodeName, pEle) ) return true;
}
return false;
}
bool SetNodeText(const std::string& sXmlFile, const std::string& sNodeName, const std::string& sText)
{
TiXmlDocument *pDoc = new TiXmlDocument();
if (pDoc == NULL) return false;
pDoc->LoadFile(sXmlFile);
TiXmlElement *pRootEle = pDoc->RootElement();
if (pRootEle == NULL) return false;
TiXmlElement *pEle = NULL;
if ( !GetNodePtrByName(pRootEle, sNodeName, pEle) ) return false;
if (pEle == NULL) return false;
pEle->Clear();
TiXmlText pValue = new TiXmlText(sText);
pEle->LinkEndChild(pValue);
pDoc->SaveFile(sXmlFile);
return true;
}
SetNodeText("你的XML文件路径", "ID", "0111111");
SetNodeText("你的XML文件路径", "LESSON", "课程")
{
if (pRootEle->Value() == sNodeName)
{
pEle = pRootEle;
return true;
}
for ( TiXmlElement *pEleTemp = pRootEle->FirstChildElement(); pEleTemp;
pEleTemp->NextSiblingElement() )
{
if ( GetNodePtrByName(pEleTemp, sNodeName, pEle) ) return true;
}
return false;
}
bool SetNodeText(const std::string& sXmlFile, const std::string& sNodeName, const std::string& sText)
{
TiXmlDocument *pDoc = new TiXmlDocument();
if (pDoc == NULL) return false;
pDoc->LoadFile(sXmlFile);
TiXmlElement *pRootEle = pDoc->RootElement();
if (pRootEle == NULL) return false;
TiXmlElement *pEle = NULL;
if ( !GetNodePtrByName(pRootEle, sNodeName, pEle) ) return false;
if (pEle == NULL) return false;
pEle->Clear();
TiXmlText pValue = new TiXmlText(sText);
pEle->LinkEndChild(pValue);
pDoc->SaveFile(sXmlFile);
return true;
}
SetNodeText("你的XML文件路径", "ID", "0111111");
SetNodeText("你的XML文件路径", "LESSON", "课程")
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询