블로그 이미지
Every unexpected event is a path to learning for you.

카테고리

분류 전체보기 (2737)
Unity3D (817)
Programming (474)
Server (33)
Unreal (4)
Gamebryo (56)
Tip & Tech (228)
협업 (58)
3DS Max (3)
Game (12)
Utility (136)
Etc (96)
Link (32)
Portfolio (19)
Subject (90)
iOS,OSX (53)
Android (14)
Linux (5)
잉여 프로젝트 (2)
게임이야기 (3)
Memories (20)
Interest (38)
Thinking (38)
한글 (30)
PaperCraft (5)
Animation (408)
Wallpaper (2)
재테크 (18)
Exercise (3)
나만의 맛집 (3)
냥이 (10)
육아 (16)
Total
Today
Yesterday
04-19 00:04

'WriteXml'에 해당되는 글 2건

  1. 2011.03.23 DataGridView to XML
  2. 2010.12.08 TinyXML Write

DataGridView to XML

Programming/C# / 2011. 3. 23. 17:37


음..테스트 안해봤지만 심플함. 해봐야지..
반응형
Posted by blueasa
, |

TinyXML Write

Programming/TinyXML / 2010. 12. 8. 14:36

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


출처 : http://dolphin.ivyro.net/file/algorithm/etc/tinyXml_write.html

http://dolphin.ivyro.net/file/algorithm/etc/tinyXml_write.html

반응형

'Programming > TinyXML' 카테고리의 다른 글

TinyXml 사용법 요약  (0) 2010.12.08
TinyXML Read  (2) 2010.12.08
XML introduce(XML 문법 설명)  (0) 2010.04.27
Tiny XML 링크  (0) 2010.04.27
Posted by blueasa
, |