Samchon Framework for CPP  1.0.0
xml.hpp
1 #pragma once
2 
3 #include <iostream>
4 #include <samchon/library/XML.hpp>
5 
6 using namespace std;
7 using namespace samchon::library;
8 
9 namespace samchon
10 {
11 namespace examples
12 {
13 namespace xml
14 {
15  struct Member
16  {
17  std::string id;
18  std::string name;
19  int age;
20  int grade;
21 
22  auto toXML() const -> std::shared_ptr<library::XML>
23  {
24  std::shared_ptr<library::XML> xml(new library::XML());
25  xml->setTag("member");
26 
27  xml->setProperty("id", id);
28  xml->setProperty("name", name);
29  xml->setProperty("age", age);
30  xml->setProperty("grade", grade);
31 
32  return xml;
33  };
34  };
35 
36  void main()
37  {
38  using namespace std;
39  using namespace samchon::library;
40 
41  //DECLARE An XML WITH MEMBER_ARRAY TAG
42  shared_ptr<XML> xml(new XML());
43  xml->setTag("memberArray");
44 
45  //ADD MEMBER(S) TO XML
46  vector<struct Member> memberArray =
47  {
48  { "samchon", "Jeongho Nam", 27, 1 },
49  { "gkyu", "Kwangkyu Ko", 25, 1 },
50  { "guest", "John Doe", 99, 4 }
51  };
52  for (size_t i = 0; i < memberArray.size(); i++)
53  xml->push_back(memberArray[i].toXML());
54 
55  //ADD FILE_LIST BY STRING DIRECTLY
56  xml->push_back
57  (
58  string("") +
59  "<fileList>\n" +
60  "<file extension='pdf' name='API' />" +
61  "<file extension='pdf' name='Guidance+For+Developer'>Damaged</file>" +
62  "<file extension='docx' name='Resume' />" +
63  "<file extension='jpg' name='My+House' />" +
64  "<file extension='xlsx' name='Grades' />" +
65  "</fileList>"
66  );
67 
68  cout << "-----------------------------------------------------------------" << endl;
69  cout << " Get Properties And Values" << endl;
70  cout << "-----------------------------------------------------------------" << endl;
71  cout << "Age of 2nd member: " << xml->get("member")->at(1)->getProperty<int>("age") << endl;
72  cout << "Age of 1st member: " << xml->get("member")->at(0)->getProperty("id") << endl << endl;
73 
74  cout << "File name and extension of 5th: "
75  << xml->get("fileList")->at(0)->get("file")->at(4)->getProperty("name") << "."
76  << xml->get("fileList")->at(0)->get("file")->at(4)->getProperty("extension") << endl;
77  cout << "Value of 2nd file: " << xml->get("fileList")->at(0)->get("file")->at(1)->getValue() << endl << endl;
78 
79  cout << "-----------------------------------------------------------------" << endl;
80  cout << " XML to String: " << endl;
81  cout << "-----------------------------------------------------------------" << endl;
82  cout << xml->toString() << endl;
83  };
84 };
85 };
86 };