반응형

http://yamoe.tistory.com/228




RapidXml 1.13 의 샘플





RapidXml : http://rapidxml.sourceforge.net/

Platform
Compiler
strlen()RapidXmlpugixml 0.3pugxmlTinyXml
Pentium 4
MSVC 8.0
2.5
5.4
7.0
61.7
298.8
Pentium 4
gcc 4.1.1
0.8
6.1
9.5
67.0
413.2
Core 2
MSVC 8.0
1.0
4.5
5.0
24.6
154.8
Core 2
gcc 4.1.1
0.6
4.6
5.4
28.3
229.3
Athlon XP
MSVC 8.0
3.1
7.7
8.0
25.5
182.6
Athlon XP
gcc 4.1.1
0.9
8.2
9.2
33.7
265.2
Pentium 3
MSVC 8.0
2.0
6.3
7.0
30.9
211.9
Pentium 3
gcc 4.1.1
1.0
6.7
8.9
35.3
316.0







[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;
}

반응형

+ Recent posts