-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoXMLParser.cpp
110 lines (98 loc) · 2.37 KB
/
RoXMLParser.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 <atlbase.h>
#include "RoXMLParser.h"
#include <iostream>
#include <codecvt>
class CRoPugiXMLNode : public CRoXMLNode
{
public:
CRoPugiXMLNode(){}
CRoPugiXMLNode(pugi::xml_node node):m_xmlNode(node){}
~CRoPugiXMLNode()
{}
virtual std::wstring getName()
{
std::string strName = m_xmlNode.name();
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> convert;
return convert.from_bytes(strName);
}
virtual bool getAttribute(const std::wstring& wsAttr, std::wstring& wsValue)
{
if (!m_xmlNode.empty())
{
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> convert;
std::string strAttr = convert.to_bytes(wsAttr);
std::string strValue = m_xmlNode.attribute(strAttr.c_str()).as_string();
wsValue = convert.from_bytes(strValue);
return true;
}
return false;
}
virtual std::shared_ptr<CRoXMLNode> getFirstChild()
{
std::shared_ptr<CRoXMLNode> xmlNode;
if (!m_xmlNode.empty())
{
pugi::xml_node firstNode = m_xmlNode.first_child();
if (!firstNode.empty())
{
xmlNode = std::make_shared<CRoPugiXMLNode>(firstNode);
}
}
return xmlNode;
}
virtual bool hasChildNodes()
{
if (!m_xmlNode.empty())
{
return !(m_xmlNode.first_child()).empty();
}
return false;
}
virtual std::shared_ptr<CRoXMLNode> getNextSibling()
{
std::shared_ptr<CRoXMLNode> xmlNode;
if (!m_xmlNode.empty())
{
pugi::xml_node sibling = m_xmlNode.next_sibling();
if (!sibling.empty())
{
xmlNode = std::make_shared<CRoPugiXMLNode>(sibling);
}
}
return xmlNode;
}
protected:
pugi::xml_node m_xmlNode;
};
CRoPugiXMLParser::CRoPugiXMLParser()
{
}
CRoPugiXMLParser::~CRoPugiXMLParser()
{
}
bool CRoPugiXMLParser::parse(const std::wstring& wsXmlText)
{
if (wsXmlText.empty())
{
return false;
}
pugi::xml_parse_result result = m_doc.load_buffer(wsXmlText.c_str(), wsXmlText.size()*sizeof(wchar_t));
//std::cout << result.description() << std::endl;
return (result);
}
std::shared_ptr<CRoXMLNode> CRoPugiXMLParser::getFirstChild()
{
std::shared_ptr<CRoXMLNode> xmlNode;
if (m_doc.empty())
{
return xmlNode;
}
pugi::xml_node firstNode = m_doc.first_child();
if (firstNode.empty())
{
return xmlNode;
}
xmlNode = std::make_shared<CRoPugiXMLNode>(firstNode);
return xmlNode;
}