공식 : http://www.grinninglizard.com/tinyxml/index.html

문서 : http://www.grinninglizard.com/tinyxmldocs/index.html

다운로드 : http://sourceforge.net/projects/tinyxml/


  • #include "tinyxml.h"
  • #pragma comment( lib, "tinyxml.lib" )
  • 1. 문서를 연다.
  • TiXmlDocument doc( strFileName ) ;
  • if( !doc.LoadFile( ) )
  • {
  • MessageBox( NULL, _T( "파일 읽기에 실패하였습니다." ), _T( "오류" ), MB_OK ) ;
  • return ;
  • }
  • 혹은
  • TiXmlDocument doc ;
  • if( doc.LoadFile( 파일명 ) == false )
  • {
  • MessageBox( NULL, _T( "파일 읽기에 실패하였습니다." ), _T( "오류" ), MB_OK ) ;
  • return ;
  • }
  • 2. 최상위 노드를 구한다.
  • TiXmlNode *rootNode = doc.FirstChild( "노드명" ) ;
  • TiXmlNode *rootNode = doc.FirstChild( ) ;
  • TiXmlNode *rootNode = doc.IterateChildren( 0 ) ;
  • 3. 원하는 노드로 이동한다.
  • TiXmlNodde *subNode = rootNode->FirstChild() ;
  • 4. 엘리먼트 얻기
  • TiXmlElement *element = subNode->ToElement( ) ;
  • TiXmlElement *element = subNode->FirstChildElement( "노드명" ) ;
  • 5. 엘리먼트에서 값 얻기
  • TixmlAttribute *attribute = element->FirstAttribute( ) ;
  • TixmlAttribute *attribute = element->Attribute( "Attribute이름", &index ) ;
  • attribute->next( ) ;
  • 해당 값을 변수에 담기
  • attribute->IntValue( ) ;
  • attribute->Value( ) ;



  • // 순환

  • // 처음부터 마지막까지 순환
  • TiXmlNode *node = doc.FirstChild( ) ;
  • for( ; node ; node = node->NextSibling( ) )
  • {
  • 노드별로 할일
  • }
  • // 마지막부터 처음까지 순환
  • TiXmlNode *node = doc.LastChild( ) ;
  • for( ; node ; node = node->PreviousSibling( ) )
  • {
  • 노드별로 할일
  • }
  • // iterator사용
  • TiXmlNode *node = doc.IterateChildren( 0 ) ;
  • for( ; node ; node = node->IterateChildren( node ) )
  • {
  • 노드별로 할일
  • }


  • //생성/추가하는 방법
  • // document생성
  • TiXmlDocument doc ;
  • // xml 버전 문자셋 설정
  • doc.InsertEndChild( TiXmlDeclaration( "1.0", "euc-kr", "" ) ) ;
  • // element한개 생성
  • TiXmlElement *rootElement = new TiXmlElement( "rootinfo" ) ;
  • // element에 attribute만들어서 값 설정
  • rootElement->SetAttribute( "Ver", 2 ) ;
  • // element를 doc에 붙여줌
  • doc.LinkEndChild( rootElement ) ;
  • //document를 저장한다.
  • doc.SaveFile( 파일명 ) ;



+ Recent posts