boost::xml_parser wrapper

Dev.Write 2013. 6. 12. 19:46 Posted by zetz
boost::property_tree::ptree wrapper class
 XmlElement.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#pragma once
 
#include <boost\property_tree\ptree.hpp>
 
class XmlElement
{
public:
    typedef boost::property_tree::ptree Node;
    typedef boost::property_tree::ptree::iterator NodeItr;
    typedef boost::property_tree::ptree::key_type NodeKey;  // std::string
    typedef NodeKey String;
 
    XmlElement(Node& element);
    ~XmlElement();
     
    // name
 
    // data
    const String& getData() const;
    void setData(const String& value);
 
    // attribute
    void setAttribute(const String& name, const String& value);
    bool getAttribute(const String& name, String& value) const;
 
    // children
    XmlElement addChild(const String& name);
    void removeChild(const String& name);
    XmlElement findChild(const String& path);
     
    class Iterator {
    public:
        Iterator(NodeItr _beg, NodeItr _end);
        bool hasNext();
        XmlElement getNext();
        NodeItr itr, end;
    };
    Iterator getChilds();
 
private:
    Node&   m_node;
};
 
 
 
class XmlDocument
{
public:
    typedef boost::property_tree::ptree Node;
    typedef std::string String;
 
    XmlElement createRoot(const String& name);
    XmlElement getRoot();
 
    bool load(const String& filename);
    void save(const String& filename);
    void clear();
 
    void dump();
private:
    Node    m_root;
};
XmlElement.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "stdafx.h"
#include "XmlElement.h"
 
#include <boost\property_tree\xml_parser.hpp>
 
#include <fstream>
 
/**
    XmlElement::Iterator
*/
XmlElement::Iterator::Iterator(NodeItr _beg, NodeItr _end)
    : itr(_beg), end(_end)
{
}
 
bool XmlElement::Iterator::hasNext()
{
    return (itr != end);
}
 
XmlElement XmlElement::Iterator::getNext()
{
    XmlElement ret(itr->second);
    ++itr;
    return ret;
}
 
 
/**
    XmlElement
*/
XmlElement::XmlElement(Node& element)
    : m_node(element)
{
}
XmlElement::~XmlElement()
{
 
}
 
const XmlElement::String& XmlElement::getData() const
{
    return m_node.data();
}
 
void XmlElement::setData(const String& value)
{
    m_node.data() = value;
}
 
void XmlElement::setAttribute(const String& name, const String& value)
{
    std::string temp = "<xmlattr>.";
    temp += name;
    m_node.put(temp.c_str(), value);
}
 
bool XmlElement::getAttribute(const String& name, String& value) const
{
    std::string temp = "<xmlattr>.";
    temp += name;
    value = m_node.get(temp.c_str(), std::string());
    return true;
}
 
XmlElement XmlElement::addChild(const String& name)
{
    return XmlElement(m_node.add(name, ""));
}
 
void XmlElement::removeChild(const String& name)
{
    m_node.erase(name);
}
 
XmlElement XmlElement::findChild(const String& path)
{
    return XmlElement(m_node.find(path)->second);
}
 
/**
    XmlDocument
*/
XmlElement XmlDocument::createRoot(const String& name)
{
    return XmlElement(m_root).addChild(name);
}
 
XmlElement XmlDocument::getRoot()
{
    return XmlElement(m_root);
}
 
bool XmlDocument::load(const String& filename)
{
    std::ifstream is(filename.c_str());
    boost::property_tree::read_xml(is, m_root);
    return true;
}
 
void XmlDocument::save(const String& filename)
{
    std::ofstream os(filename.c_str());
    boost::property_tree::write_xml(os, m_root);
}
 
void XmlDocument::clear()
{
    m_root.clear();
}


'Dev.Write' 카테고리의 다른 글

CodePatch  (0) 2013.09.01
a timer using boost  (0) 2013.08.02
list  (0) 2013.04.12
quick sort, bouble sort  (1) 2013.03.25
insert sort, b-search  (0) 2013.03.07