大家好:)我开发了导出.xml
文件(Android dimens文件)的程序。我的程序将导出 dimens_ldpi.xml
、dimens_mdpi.xml
、dimens_hdpi.xml
和 dimens_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/