TinyXml 사용법 요약
1.<!--?xml version="1.0" ?--> <myapp> <welcome>Welcome to MyApp</welcome> <farewell>Thank you for using MyApp</farewell> <windows> <windowname="MainFrame" w="400" h="250"y="15" x="5"> </window></windows> <connectiontimeout="123.456000"ip="192.168.0.1"> </connection></myapp>파일에서 읽을때
1.TiXmlDocument document; document.LoadFile(_File_Name_);문자열로 읽을때
1.TiXmlDocument document; document.Parse(szXML);Node와 Element를 가져올때
1.TiXmlElement* pRoot = document.FirstChildElement("MyApp"); if( NULL == pRoot ) return FALSE; char* szRootName = pRoot->Value(); //Value()를 사용하면 Node의 이름을 알수있다각 태그의 속성값 읽어올때
여기에선 위예제의 windows 태그의 속성을 읽어본다.
1.TiXmlElement* pElement = pRoot->FirstChildElement("Windows"); char* szName = pElement->Attribute("name"); char* szX = pElement->->Attribute("x"); char* szY = pElement->->Attribute("y"); char* szW = pElement->->Attribute("w"); char* szH = pElement->->Attribute("h");정수형으로 읽고 싶으면~
1.int x;pElement->Attribute("x", &x);모든 속성값을 한번에 읽어올때
1.TiXmlElement* pElement = pRoot->FirstChildElement("Windows"); TiXmlAttribute* attrib = pElement ->FirstAttribute(); //pElement의 속성을 받아온다. while(attrib) { const char* szAttribute = attrib->Value(); attrib = attrib->Next(); }Child Node를 순회 할때
1.TiXmlElement* pRoot = doc.RootElement(); TiXmlElement* pChild; for(pChild = pRoot ->FirstChildElement() ; pChild != 0 ; pChild = pChild->NextSiblingElement()) { //Node를 돌면서.. }Save XML
Xml 형식선언할때
1.TiXmlDocument doc; TiXmlElement* msg; TiXmlDeclaration* decl = newTiXmlDeclaration( "1.0", "", "" ); doc.LinkEndChild( decl ); //결과값 : <!--?xml version="1.0" ?-->서브 노드를 추가할때
1.TiXmlElement * root = new TiXmlElement( "MyApp" ); doc.LinkEndChild( root );주석문장을 추가할때
1.TiXmlComment * comment = new TiXmlComment();comment->SetValue(" Settings for MyApp " ); root->LinkEndChild( comment );Message 서브노드와 하위 노드및 데이터를 추가할때
1.TiXmlElement * msgs = new TiXmlElement( "Messages" ); root->LinkEndChild( msgs ); msg = new TiXmlElement( "Welcome"); msg->LinkEndChild( new TiXmlText( "Welcome to MyApp")); msgs->LinkEndChild( msg ); msg = new TiXmlElement("Farewell" ); msg->LinkEndChild( new TiXmlText( "Thank you for using MyApp" )); msgs->LinkEndChild( msg );노드를 추가하고 Attribute를 설정할때 레벨을 맞추기 위해서 root의 하위로 추가 한것을 주의 깊게 봐야 한다.
1.TiXmlElement * windows = new TiXmlElement( "Windows" ); root->LinkEndChild( windows ); TiXmlElement * window; window =new TiXmlElement( "Window" ); windows->LinkEndChild( window ); window->SetAttribute("name", "MainFrame"); window->SetAttribute("x", 5); window->SetAttribute("y", 15); window->SetAttribute("w", 400); window->SetAttribute("h", 250);Double 값 (소수점 값) 을 설정할때
1.TiXmlElement * cxn = new TiXmlElement( "Connection" ); root->LinkEndChild( cxn ); cxn->SetAttribute("ip","192.168.0.1"); cxn->SetDoubleAttribute("timeout", 123.456); // floating point attribXml파일로 저장할때
1.// 파일로 저장 doc.SaveFile("text.xml"); //문자열로.. TiXmlPrinter printer; printer.SetStreamPrinting(); Doc.Accept( &printer ); char* pAA = printer.CStr(); // char* 를 반환한다. std::string str = printer.Str(); // std::string으로 반환한다.'Programming > TinyXML' 카테고리의 다른 글
| TinyXML Read (2) | 2010.12.08 |
|---|---|
| TinyXML Write (0) | 2010.12.08 |
| XML introduce(XML 문법 설명) (0) | 2010.04.27 |
| Tiny XML 링크 (0) | 2010.04.27 |
