프로그래밍/C/C++/Obj-C

tinyxml2 간단 예제

AlwaysNR 2015. 7. 27. 14:12

xml data  읽기


#include <iostream>

#include <string>

#include "tinyxml2.h"

using namespace tinyxml2;

using namespace std;


int main()

{

char buf[] = "\

<TESTNODE1 version='1.0'>\n\

    <TESTNODE2 value='hello' />\n\

    <TESTNODE3>\

hello, testnode3\

    </TESTNODE3>\n\

</TESTNODE1>";

XMLError xmlerr=XML_NO_ERROR;

XMLDocument doc;

xmlerr = doc.Parse(buf);

if (xmlerr == XML_SUCCESS)

{

cout << "Parse Success" << endl;

XMLElement *pelem = doc.FirstChildElement();

cout << pelem->Name();

if(pelem->GetText() != NULL)

cout << "    text:" << pelem->GetText() << endl;

else

cout << endl;

for (const XMLAttribute* pattr = pelem->FirstAttribute(); pattr != NULL; pattr = pattr->Next())

{

cout << pattr->Name() << " = " << pattr->Value() << endl;

}

pelem = pelem->FirstChildElement();

while(pelem != NULL)

{

cout << pelem->Name();

if(pelem->GetText() != NULL)

cout << "    text:" << pelem->GetText() << endl;

else

cout << endl;

for (const XMLAttribute* pattr = pelem->FirstAttribute(); pattr != NULL; pattr = pattr->Next())

{

cout << pattr->Name() << " = " << pattr->Value() << endl;

}

pelem = pelem->NextSiblingElement();

}

}

else

cout << "Parse Error : " << xmlerr << endl; 

    return 0;

}