cxmlxml-parsingtinyxml

Tinyxml - How to export multiple file


Hi everyone :) I develop program that export .xml file (Android dimens file). My Program will export dimens_ldpi.xml, dimens_mdpi.xml, dimens_hdpi.xml, and dimens_xhdpi.xml. Easy to say, I want to export multiple file of the same structure. But I don't know how to export multiple file to easy.

current my source like this :

//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 ]

As you can see, this is really hard coding and I don't want to hard coding. Is this any way can export to multiple file? Thanks in advance :)


Solution

  • I try to search but tinyXml doesn't support to multiple export.. So I solved it using array. (I don't satisfied but better than original)

    my code :

    #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];
    

    It's batter than hard coding but I hope the better way is in somewhere.