TinyXML
TinyXML is a small, simple XML parser for the C++ language. It is free and open source software, distributed under the terms of the license of zlib/libpng.
TinyXML은 작고 간단한 C++XML파서이다. 사용하기 편리하고 다른프로그램으로 이식하기도 수월하다.
Requirements
다른 프로젝트에 필요한 최소 파일은 아래와 같다.
- tinystr.cpp
- tinystr.h
- tinyxml.cpp
- tinyxml.h
- tinyxmlerror.cpp
- tinyxmlparser.cpp
TinyXML-2
TinyXML-2 is a simple, small, efficient, C++ XML parser that can be easily integrating into other programs.
TinyXML-1 served my needs for many years; but it uses memory inefficiently, and doesn't perform as well as desired for mobile devices. I wanted an XML parser that was a little more modern, a little simpler (the "tiny" had been lost a little over the years), and was a good fit for Android.
There are 2 files in TinyXML-2
설치에 필요한 두 개의 파일:
- tinyxml2.cpp
- tinyxml2.h
간단한 사용방법
TinyXML 라이브러리의 간단한 사용방법을 설명한다. 아래와 같은 XML 정보가 존재한다 가정한다.
<?xml version="1.0" ?>
<MyApp>
<!-- Settings for MyApp -->
<Messages>
<Welcome>Welcome to MyApp</Welcome>
<Farewell>Thank you for using MyApp</Farewell>
</Messages>
<Windows>
<Window name="MainFrame" x="5" y="15" w="400" h="250" />
</Windows>
<Connection ip="192.168.0.1" timeout="123.456000" />
</MyApp>
- 파일에서 XML정보를 읽는 방법은 아래와 같다.
문자열에서 XML정보를 읽는 방법은 아래와 같다.
엘리먼트는 아래와 같이 가져올 수 있다.
<Welcome>태그 정보는 아래와 같이 가져올 수 있다.
pElement = pRoot->FirstChildElement("Message");
pElement = pElement->FirstChildElement("Welcome");
char * pAA = pElement->Value(); // pAA는 "Welcome" 이다.
pAA = pElement->GetText(); // pAA는 "Welcome to MyApp" 이다.
<Windows>태그의 name속성은 아래와 같이 읽을 수 있다.
pElement = pRoot->FirstChildElement("Windows");
char * pAA = pElement->Attribute("name"); // pAA에 "MainFrame"이 들어온다.
int x;
pElement->Attribute("x", &x); // x 에 숫자 5가 들어온다.
루프를 돌면서 child노드를 순환하는 방법은 아래와 같다.
pNode = pRoot->FirstChild("sub_node");
for (pNode; pNode; pNode = pNode->NextSibling()) {
pAA = pNode->Value();
pElement = pNode->FirstChildElement("item");
pAA = pElement->GetText();
}
XML저장 방법
설정이나 원하는 정보를 XML로 만드는 작업을 진행한다.
- 형식선언은 아래와 같다.
TiXmlDocument doc;
TiXmlElement * msg;
TiXmlDeclaration * decl = new TiXmlDeclaration("1.0", "", "");
doc.LinkEndChild(decl);
위와 같이 진행하면
<?xml version="1.0" ?>
이 생성된다.
- 서브 노드는 아래와 같이 추가한다.
new로 생성한 TiXmlElement는 내부저으로 자동삭제되므로 삭제할 필요는 없다.
- 주석문은 아래와 같이 추가한다.
TiXmlComment * comment = new TiXmlComment();
comment->SetValue(" Settings for MyApp " );
root->LinkEndChild(comment);
주석은 TiXmlComment 클래스를 사용하면 된다.
Message서브노드와 하위 노드및 데이터는 아래와 같이 추가한다.
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의 하위로 추가한 것을 주의 깊게 봐야 한다.)
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값(소수점 값) 설정은 아래와 같다.
TiXmlElement * cxn = new TiXmlElement( "Connection" );
root->LinkEndChild(cxn);
cxn->SetAttribute("ip", "192.168.0.1");
cxn->SetDoubleAttribute("timeout", 123.456);
파일은 아래와 같이 저장한다.
문자열저장은 아래와 같다.
TiXmlPrinter printer;
printer.SetStreamPrinting();
Doc.Accept(&printer);
char * pAA = printer.CStr();
std::string str = printer.Str();
Local Download
- TinyXML2 3.0.0
-
Tinyxml2-3.0.0.tar.gz