最近工作中遇到在xml文件中写入CDATA数据的问题,我使用的开源框架是tinyxml2。在网上搜了一下发现没有介绍的,只能自己去tinyxml2.h中去找用法,拿出来分享一下:
1.CDATA介绍
CDATA使用格式为
![](/attachment/201510/22/29870969_1445517612b2v0.png)
,使用这个格式的目的是在xml进行解析时跳过其中包含的字符串。
2.tinyxml2代码实现
int main(int argh, char *argv[])
{
static const char *xml = "
![](/attachment/201510/22/29870969_1445517628a76x.png)
";
XMLDocument doc;
/* XML解码 */
doc.Parse(xml);
/* 找到要插入的节点 */
XMLElement *ele = doc.FirstChildElement();
/* 申请节点 */
XMLText *text = doc.NewText("");
/* 设置为CDATA格式 */
text->SetCData(true);
/* 插入 */
ele->InsertFirstChild(text);
/* 打印 */
doc.Print();
return 0;
}
阅读(1386) | 评论(0) | 转发(0) |