TinyXML Write

TinyXML ReadÀÇ test.xml°ú °°Àº Æ÷¸ËÀ¸·Î XML ÆÄÀÏÀ» ÀúÀåÇØ º¸ÀÚ.
ÀúÀåµÉ ÆÄÀÏÀº ´ÙÀ½°ú °°´Ù.

test_write.xml

<?xml version="1.0" encoding="euc-kr" ?>

<root>

    <!--°¢±¹ÀÇ ´ëÇ¥ À½½ÄµéÀÌ´Ù.-->

    <food num="1">

        <name>±èÄ¡</name>

        <country>´ëÇѹα¹</country>

    </food>

</root>

XML Çü½Ä ¼±¾ð

TiXmlDocument doc; 

TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0", "euc-kr", "" ); 

doc.LinkEndChild( decl );

¿ì¸®³ª¶ó ¹®ÀÚ¼ÂÀÇ 1.0 ¹öÀüÀÌ ¸¸µé¾î Áø´Ù.

¼­ºê ³ëµå Ãß°¡

TiXmlElement * root = new TiXmlElement( "root" ); 

doc.LinkEndChild( root );

"root" ¿¤¸®¸ÕÆ®¸¦ Ãß°¡ÇÑ´Ù. TinyXML¿¡¼­ ³ëµå¸¦ Ãß°¡ ÇÒ ¶§, new·Î »ý¼ºÇÑ ¿¤¸®¸ÕÆ®´Â
³»ºÎ¿¡¼­ ÀÚµ¿À¸·Î »èÁ¦ µÇ±â ¶§¹®¿¡ ÇØÁ¦ ÇÒ ÇÊ¿ä°¡ ¾ø´Ù.

ÁÖ¼® Ãß°¡

TiXmlComment * comment = new TiXmlComment();

comment->SetValue( "°¢±¹ÀÇ ´ëÇ¥ À½½ÄµéÀÌ´Ù." ); 

root->LinkEndChild( comment );

rootÀÇ ÀÚ½ÄÀ¸·Î "food" ¼­ºê ³ëµå Ãß°¡

TiXmlElement *food = new TiXmlElement( "food" ); 

root->LinkEndChild( food );

rootÀÇ ÀÚ½ÄÀÌ µÇ´Â "food" ¼­ºê ³ëµå¸¦ Ãß°¡ÇÑ´Ù.

foodÀÇ Attribute·Î "num" Ãß°¡

food->SetAttribute("num", 1);   //food->SetAttribute("num", "1")¿Í µ¿ÀÏ

food->SetAttribute( "num", "1" )µµ µ¿ÀÏÇÑ °á°úÀÌ´Ù.

foodÀÇ ÀÚ½ÄÀ¸·Î name ¼­ºê ³ëµå¿Í °ª Ãß°¡

TiXmlElement *name = new TiXmlElement( "name" ); 

food->LinkEndChild( name ); 

name->LinkEndChild( new TiXmlText( "񊎭" ));

foodÀÇ ÀÚ½Ä ³ëµå·Î "name"À» Ãß°¡ÇÏ°í °ªÀº "±èÄ¡"ÀÌ´Ù.

foodÀÇ ÀÚ½Ä ³ëµå·Î country ¼­ºê ³ëµå¿Í °ª Ãß°¡

TiXmlElement *country = new TiXmlElement( "country" ); 

food->LinkEndChild( country ); 

country->LinkEndChild( new TiXmlText( "´ëÇѹα¹" ));

foodÀÇ ÀÚ½Ä ³ëµå·Î "country"¸¦ Ãß°¡ÇÏ°í °ªÀº "´ëÇѹα¹"ÀÌ´Ù.

ÀúÀå Çϱâ

doc.SaveFile("test_write.xml");

SaveFile()À» ÅëÇØ ¿øÇÏ´Â ÆÄÀϸíÀ¸·Î °ªÀ» ÀúÀåÇÑ´Ù.

Àüü ¼Ò½º

void WriteXml()

{

    //Çü½Ä ¼±¾ð

    TiXmlDocument doc; 

    TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0", "euc-kr", "" ); 

    doc.LinkEndChild( decl );

 

    //¼­ºê ³ëµå Ãß°¡

    TiXmlElement * root = new TiXmlElement( "root" ); 

    doc.LinkEndChild( root ); 

 

    //ÁÖ¼® Ãß°¡

    TiXmlComment * comment = new TiXmlComment();

    comment->SetValue( "°¢±¹ÀÇ ´ëÇ¥ À½½ÄµéÀÌ´Ù." ); 

    root->LinkEndChild( comment ); 

 

    //rootÀÇ ÀÚ½ÄÀ¸·Î food ¼­ºê ³ëµå Ãß°¡

    TiXmlElement *food = new TiXmlElement( "food" ); 

    root->LinkEndChild( food ); 

 

    //foodÀÇ Attribute·Î "num" Ãß°¡

    food->SetAttribute("num", 1);   //food->SetAttribute("num", "1")¿Í µ¿ÀÏ

 

    //foodÀÇ ÀÚ½ÄÀ¸·Î name ¼­ºê ³ëµå¿Í °ª Ãß°¡

    TiXmlElement *name = new TiXmlElement( "name" ); 

    food->LinkEndChild( name ); 

    name->LinkEndChild( new TiXmlText( "±èÄ¡" ));

 

    //foodÀÇ ÀÚ½ÄÀ¸·Î country ¼­ºê ³ëµå¿Í °ª Ãß°¡

    TiXmlElement *country = new TiXmlElement( "country" ); 

    food->LinkEndChild( country ); 

    country->LinkEndChild( new TiXmlText( "´ëÇѹα¹" ));

 

    doc.SaveFile("test_write.xml");

}

ÄÜ¼Ö Ã¢À¸·Î XML ÆÄÀÏ º¸¿©ÁÖ±â

void PrintXml()

{

    TiXmlDocument doc( "test.xml" );

    doc.LoadFile();

 

    //¹®ÀÚ¿­·Î..

    TiXmlPrinter printer;

    printer.SetStreamPrinting();

    doc.Accept( &printer );

    const char* pStr = printer.CStr();        // char* ¸¦ ¹ÝȯÇÑ´Ù.

    printf( pStr );

#ifdef  TIXML_USE_STL

    const std::string str = printer.Str();    // std::stringÀ¸·Î ¹ÝȯÇÑ´Ù.

#endif

}

XML ÆÄÀÏÀ» ÄÜ¼Ö Ã¢À¸·Î º¸¿©ÁØ´Ù...

¼Ò½º:

tinyxml.zip

ÂüÁ¶:

http://hanburn.tistory.com/7

http://hanburn.tistory.com/8