在c++中动态生成XML代码
Dynamic XML code generation in C++
我想把我自己的类的一些c++对象转换成XML代码。我猜有几个库提供c++到xml映射,但我想保持库开销简单,并制作我自己的东西。
生成XML构建的合适方法是什么?在Java中,有一些注释可以用来动态生成XML。也许是模板机制?
我使用TinyXML到目前为止。我真的很喜欢用它。
下面是我想生成的一个示例:
std::string XMLBuilder::buildThreadInformation(vector<threadinfo> threadinfos) {
TiXmlDocument document;
TiXmlDeclaration *declaration = new TiXmlDeclaration("1.0", "", "");
TiXmlElement *messageWrapElement = new TiXmlElement("message");
TiXmlElement *threadsElement = new TiXmlElement("threads");
messageWrapElement->LinkEndChild(threadsElement);
std::string numberString;
for (vector<threadinfo>::const_iterator it = threadinfos.begin(); it
!= threadinfos.end(); ++it) {
TiXmlElement *threadElement = new TiXmlElement("thread");
threadsElement->LinkEndChild(threadElement);
TiXmlElement *threadNumberElement = new TiXmlElement("number");
threadElement->LinkEndChild(threadNumberElement);
numberString = boost::lexical_cast<std::string>((*it).thread_number);
TiXmlText *threadNumber = new TiXmlText(numberString.c_str());
threadNumberElement->LinkEndChild(threadNumber);
TiXmlElement *threadNameElement = new TiXmlElement("name");
threadElement->LinkEndChild(threadNameElement);
TiXmlText *threadName = new TiXmlText((*it).name.c_str());
threadNameElement->LinkEndChild(threadName);
}
document.LinkEndChild(declaration);
document.LinkEndChild(messageWrapElement);
TiXmlPrinter printer;
document.Accept(&printer);
std::string result = printer.CStr();
return result;
}
类threadinfo将由int number和std::string name组成。
RapidXML也很容易使用,可以为您创建动态xml
相关文章:
- 用于C++代码的 API 监视器类型定义 (XML)
- 如何使用所有其他文件信息来编译代码,例如HAAR级联训练的权重的XML文件
- 用于人脸检测的Haar级联xml文件代码解释OpenCV
- 使用代码合成c++更改XML值
- 使用 XML 编写代码
- 为什么使用valgrind时管道会变得混乱的代码 --xml-fd
- 无法对代码合成 XML 类型执行操作
- qt项目代码到xml文件的转换
- 使用puixml在代码块(C++)中打开一个XML文件
- 代码合成XSD解析/数据绑定xml字符串,而不是xml文件
- 我可以得到C/ c++ /Java代码的XML AST而不编译它吗?
- 关于使用C, XML或JavaScript和win32 API编写代码的想法
- 在WinCE环境中生成c# / c++ Xml代码文档
- 可生成xml输出的unix c++代码复杂度分析
- 如何在Visual Studio 2013中快速向c++代码中添加XML注释
- 没有托管代码的xml序列化
- 解析xml文件并动态生成c++代码以构建面板(UI)
- 数据绑定- XML数据绑定和c++代码生成器
- 在c++中动态生成XML代码
- 代码合成似乎没有生成"valid" xml树