RapidXml 1.13 의 샘플
RapidXml : http://rapidxml.sourceforge.net/
[test.xml]
<?xml version=
"1.0"
encoding=
"utf-8"
standalone=
"yes"
?>
<root ver=
"1"
>
<entry stop=
"1"
>
<name type=
"man1"
>man1</name>
<content>content1</content>
</entry>
<entry step=
"2"
>
<name type=
"man2"
>name2</name>
<content>content2</content>
</entry>
</root>
[test2.xml]
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<root ver=
"1"
>
<child>child1</child>
</root>
[main.cpp]
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include "rapidxml-1.13\rapidxml.hpp" // use rapidxml
#include "rapidxml-1.13\rapidxml_print.hpp" // to wirte file
// 노드 출력
void
printNode(rapidxml::xml_node<
wchar_t
>* node)
{
// print node
std::wcout << node->name() << L
"="
<< node->value();
// print attribute
for
( rapidxml::xml_attribute<
wchar_t
>* attr=node->first_attribute(); attr; attr=attr->next_attribute() )
{
std::wcout << L
"\t"
<< attr->name() << L
"="
<< attr->value();
}
std::wcout << std::endl;
}
// xml 파일 읽기
void
ReadXml(std::wstring& filepath)
{
std::wifstream xmlFile;
xmlFile.open(filepath.c_str(), std::ios_base::in);
if
(!xmlFile.is_open())
{
return
;
}
xmlFile.seekg(0, std::ios::end);
// 마지막 파일 포인터로 이동
std::wifstream::pos_type size = xmlFile.tellg();
// 파일 크기
xmlFile.seekg(0);
// 처음 파일 포인터로 이동
// xml 파일을 vector로 읽음
std::vector<
wchar_t
> xmlData((
int
)size + 1, 0);
xmlFile.read(&xmlData.front(), (std::streamsize)size);
xmlFile.close();
// parsing
rapidxml::xml_document<
wchar_t
> doc;
doc.parse<rapidxml::parse_default>(&xmlData.front());
// get root
rapidxml::xml_node<
wchar_t
>* root = doc.first_node();
// print root node & attribute
printNode(root);
// print child node
for
( rapidxml::xml_node<
wchar_t
>* item=root->first_node(); item; item=item->next_sibling() )
{
printNode(item);
for
( rapidxml::xml_node<
wchar_t
>* subitem=item->first_node(); subitem; subitem=subitem->next_sibling() )
{
printNode(subitem);
}
}
doc.clear();
}
// xml 파일 저장
void
SaveXml(std::wstring& filename)
{
rapidxml::xml_document<
wchar_t
> doc;
// append xml declaration
rapidxml::xml_node<
wchar_t
>* header = doc.allocate_node(rapidxml::node_declaration);
header->append_attribute(doc.allocate_attribute(L
"version"
, L
"1.0"
));
header->append_attribute(doc.allocate_attribute(L
"encoding"
, L
"utf-8"
));
doc.append_node(header);
// append root node
rapidxml::xml_node<
wchar_t
>* root = doc.allocate_node(rapidxml::node_element, L
"root"
);
root->append_attribute(doc.allocate_attribute(L
"ver"
, L
"1"
));
doc.append_node(root);
// append child node
rapidxml::xml_node<
wchar_t
>* child = doc.allocate_node(rapidxml::node_element, L
"child"
);
child->value(L
"child1"
);
root->append_node(child);
std::wstring xmlString;
rapidxml::print(std::back_inserter(xmlString), doc);
// 문자를 UTF-8로 변환해서 저장해야 하는데
// 샘플이므로 처리 하지 않아 실제로 ANSI 파일 형식으로 저장됨
std::wofstream wstream;
wstream.open(filename.c_str());
wstream << xmlString;
wstream.clear();
}
int
_tmain(
int
argc, _TCHAR* argv[])
{
ReadXml(std::wstring(L
"test.xml"
));
SaveXml(std::wstring(L
"test2.xml"
));
return
0;
}
반응형
'프로그래밍(Programming) > 자원관리(Assets)' 카테고리의 다른 글
xerces C++ 오픈 XML 라이브러리 컴파일 (0) | 2012.11.26 |
---|---|
C++ 파일 입출력 처리 (0) | 2012.11.10 |
XML 파서, CMarkup (0) | 2012.11.02 |
3ds Max에서 추출한 xml 파일 분석 (0) | 2012.11.02 |
<MSXML> VC++에서 MSXML 사용법 (COM) (0) | 2012.10.31 |