c - Tinyxml - 如何导出多个文件

标签 c xml xml-parsing tinyxml

大家好:)我开发了导出.xml文件(Android dimens文件)的程序。我的程序将导出 dimens_ldpi.xmldimens_mdpi.xmldimens_hdpi.xmldimens_xhdpi.xml。说起来容易,我想导出多个相同结构的文件。但我不知道如何轻松导出多个文件。

当前我的来源如下:

//xml type declaration
TiXmlDocument ldpi_doc, mdpi_doc, hdpi_doc, xhdpi_doc;
TiXmlDeclaration* ldpi_pDec1 = new TiXmlDeclaration("1.0", "utf-8", "");
TiXmlDeclaration* mdpi_pDec1 = new TiXmlDeclaration("1.0", "utf-8", "");
TiXmlDeclaration* hdpi_pDec1 = new TiXmlDeclaration("1.0", "utf-8", "");
TiXmlDeclaration* xhdpi_pDec1 = new TiXmlDeclaration("1.0", "utf-8", "");
ldpi_doc.LinkEndChild(ldpi_pDec1);
mdpi_doc.LinkEndChild(mdpi_pDec1);
hdpi_doc.LinkEndChild(hdpi_pDec1);
xhdpi_doc.LinkEndChild(xhdpi_pDec1);

//Root add node
TiXmlElement* ldpi_pRoot = new TiXmlElement("resources");
TiXmlElement* mdpi_pRoot = new TiXmlElement("resources");
TiXmlElement* hdpi_pRoot = new TiXmlElement("resources");
TiXmlElement* xhdpi_pRoot = new TiXmlElement("resources");
ldpi_doc.LinkEndChild(ldpi_pRoot);
mdpi_doc.LinkEndChild(mdpi_pRoot);
hdpi_doc.LinkEndChild(hdpi_pRoot);
xhdpi_doc.LinkEndChild(xhdpi_pRoot);

//Add sub node
TiXmlElement* ldpi_pElem;
TiXmlElement* mdpi_pElem;
TiXmlElement* hdpi_pElem;
TiXmlElement* xhdpi_pElem;

[ SKIP ]

如您所见,这确实是硬编码,但我不想硬编码。有什么方法可以导出到多个文件吗?预先感谢:)

最佳答案

我尝试搜索,但tinyXml不支持多重导出。所以我使用数组解决了它。 (我不满意,但比原来的好)

我的代码:

#define RESOLUTION_TYPE     4
#define ENCODING            new TiXmlDeclaration("1.0", "utf-8", "")    //encoding
#define FIRST_NODE          new TiXmlElement("resources")               //first common node

//xml type declaration
TiXmlDocument doc[RESOLUTION_TYPE];
TiXmlDeclaration* dec[RESOLUTION_TYPE] = { ENCODING, ENCODING, ENCODING, ENCODING };
for (int i = 0; i < RESOLUTION_TYPE; i++){
    doc[i].LinkEndChild(dec[i]);
}

//Root add node
TiXmlElement* pRoot[RESOLUTION_TYPE] = { FIRST_NODE, FIRST_NODE, FIRST_NODE, FIRST_NODE };
for (int i = 0; i < RESOLUTION_TYPE; i++) {
    doc[i].LinkEndChild(pRoot[i]);
}

//Add sub node
TiXmlElement* pElement[RESOLUTION_TYPE];

它比硬编码更好,但我希望更好的方法在某个地方。

关于c - Tinyxml - 如何导出多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30576697/

相关文章:

c - 尝试仅使用 getchar/putchar 输出用户输入

c - 如何在没有 pcap_breakloop 的情况下中断 pcap_loop 循环

android - 如何将数据绑定(bind)与微调器一起使用并获取当前值?

xml - 将子元素的属性直接解析为 Go struct

c - 是否需要初始化 char 数组以获得准确的长度?

c - 使用 ""初始化字符串

c# - 在 asp net core web api 中进行模型绑定(bind)时忽略 XML 命名空间

php - 如何使用 PHP 解析此 XML 字符串

python - 如何使用 python 忽略许多 XML 文件中的标签

java - 萨克斯 - ExpatParser$ParseException