Samchon Framework for CPP  1.0.0
samchon::library::XML Class Reference

XML is a class representing xml object. More...

#include <XML.hpp>

Collaboration diagram for samchon::library::XML:

Public Member Functions

 XML ()
 Default Constructor. More...
 
 XML (const XML &)
 Copy Constructor. More...
 
 XML (XML &&)
 Move Constructor. More...
 
 XML (WeakString)
 Constructor by string. More...
 
void push_back (const WeakString &)
 Add children xml objects by string representing them. More...
 
void push_back (const std::shared_ptr< XML >)
 Add children xml. More...
 
void addAllProperty (const std::shared_ptr< XML >)
 Add all properties from another XML. More...
 
void setTag (const std::string &)
 Set tag (identifier) of the XML. More...
 
template<typename T >
void setValue (const T &val)
 Set value of the XML. More...
 
template<typename T >
void setProperty (const std::string &key, const T &val)
 Set a property with its key. More...
 
void eraseProperty (const std::string &)
 Erase a property by its key. More...
 
void clearProperties ()
 Remove all properties in the XML. More...
 
auto getTag () const -> std::string
 Get key; identifer of the XML. More...
 
template<class T = std::string>
auto getValue () const -> T
 Get value of the XML. More...
 
template<class T = std::string>
auto getProperty (const std::string &key) const -> T
 Get property. More...
 
auto hasProperty (const std::string &) const -> bool
 Test wheter a property exists or not. More...
 
auto getPropertyMap () const -> const HashMap< std::string, std::string > &
 Get propertyMap. More...
 
auto toString (size_t level=0) const -> std::string
 Get the string content. More...
 
- Public Member Functions inherited from samchon::HashMap< std::string, std::shared_ptr< XMLList > >
auto has (const std::string &key) const -> bool
 Whether have the item or not. More...
 
auto get (const std::string &key) -> std::shared_ptr< XMLList > &
 Get element. More...
 
void set (const std::string &key, const std::shared_ptr< XMLList > &val)
 Set element. More...
 
auto pop (const std::string &key) -> std::shared_ptr< XMLList >
 Pop item. More...
 

Private Member Functions

 XML (XML *, WeakString &)
 Protected Constructor by string for child. More...
 

Private Attributes

std::string tag
 Tag name. More...
 
std::string value
 Value of the XML. More...
 
HashMap< std::string, std::string > propertyMap
 Properties belongs to the XML. More...
 

Detailed Description

XML is a class representing xml object.

The XML class provides methods and properties for working with XML objects.

The XML class (along with the XMLList and Namespace) implements the powerful XML-handling standard defined in ECMAScript for XML (E4X) specification.

XML class has a recursive, hierarchical relationship.

All XML objects're managed by shared_ptr.

  • XML contains XMLList from dictionary of shared pointer<XMLList>
  • XMLList contains XML from vector of shared pointer<XML>
  • Even if user creates an XML object directly, it's the basic principle to use shared pointer
library_xml.png
Note

Parsing comment is not supported yet.

It's not recommeded to creating an XML object which is not being managed by shared pointer.

Warning

Do not abuse values for expressing member variables.

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

Definition at line 72 of file XML.hpp.

Constructor & Destructor Documentation

XML::XML ( )

Default Constructor.

Warning
Declare XML to managed by shared pointer

Definition at line 36 of file XML.cpp.

Referenced by push_back(), and XML().

Here is the caller graph for this function:

XML::XML ( const XML xml)

Copy Constructor.

Not copying (shared) pointer of children xml objects, but copying the real objects of children xml

Definition at line 42 of file XML.cpp.

References propertyMap, tag, and value.

XML::XML ( XML &&  xml)

Move Constructor.

Definition at line 65 of file XML.cpp.

References propertyMap, tag, and value.

XML::XML ( WeakString  wStr)

Constructor by string.

Parses a string so that constructs an XML object

Parameters
strA string representing xml object
Warning
Declare XML to managed by shared pointer

Definition at line 74 of file XML.cpp.

References samchon::WeakString::between(), samchon::WeakString::find(), samchon::HashMap< Key, T, Hash, Pred, Alloc >::pop(), samchon::WeakString::size(), samchon::WeakString::substr(), and samchon::WeakString::substring().

Here is the call graph for this function:

XML::XML ( XML parent,
WeakString str 
)
private

Protected Constructor by string for child.

Parses a string so that creates an XML object It is called for creating children XML objects from parent XML object.

Parameters
parentParent object who will contains this XML object
strA string to be parsed

Definition at line 117 of file XML.cpp.

References samchon::WeakString::find(), propertyMap, push_back(), samchon::WeakString::rfind(), samchon::HashMap< Key, T, Hash, Pred, Alloc >::set(), samchon::WeakString::size(), samchon::WeakString::substr(), samchon::WeakString::substring(), tag, samchon::WeakString::trim(), value, and XML().

Here is the call graph for this function:

Member Function Documentation

void XML::push_back ( const WeakString str)

Add children xml objects by string representing them.

Parameters
strA string representing xml objects whould be belonged to this XML

Definition at line 439 of file XML.cpp.

References samchon::WeakString::empty(), samchon::HashMap< std::string, std::shared_ptr< XMLList > >::has(), tag, and XML().

Referenced by XML().

Here is the call graph for this function:

Here is the caller graph for this function:

void samchon::library::XML::push_back ( const std::shared_ptr< XML )

Add children xml.

Parameters
xmlAn xml object you want to add
void XML::addAllProperty ( const std::shared_ptr< XML )

Add all properties from another XML.

Copies all properties from target to here.
Warning
Not a category of assign, but an insert.
Parameters
xmlTarget xml object to deliver its properties

Definition at line 469 of file XML.cpp.

References propertyMap.

void XML::setTag ( const std::string &  tag)

Set tag (identifier) of the XML.

See also
XML::tag

Definition at line 384 of file XML.cpp.

References tag.

template<typename T >
void samchon::library::XML::setValue ( const T &  val)
inline

Set value of the XML.

Template Parameters
_TyType of the value
Parameters
valThe value to set
Warning
Do not abuse values for expressing member variables
Standard Usage Non-standard usage abusing value
<memberList>
    <member id='jhnam88' name='Jeongho+Nam' birthdate='1988-03-11' />
    <member id='master' name='Administartor' birthdate='2011-07-28' />
</memberList>
<member>
<id>jhnam88</id>
<name>Jeongho+Nam</name>
<birthdate>1988-03-11</birthdate>
</member>

Definition at line 229 of file XML.hpp.

References samchon::WeakString::str().

Here is the call graph for this function:

template<typename T >
void samchon::library::XML::setProperty ( const std::string &  key,
const T &  val 
)
inline

Set a property with its key.

Definition at line 259 of file XML.hpp.

References samchon::HashMap< Key, T, Hash, Pred, Alloc >::set(), and samchon::WeakString::str().

Here is the call graph for this function:

void XML::eraseProperty ( const std::string &  tag)

Erase a property by its key.

Parameters
keyThe key of the property to erase
Exceptions
exceptionUnable to find the element

Definition at line 476 of file XML.cpp.

References propertyMap.

void XML::clearProperties ( )

Remove all properties in the XML.

Definition at line 480 of file XML.cpp.

References propertyMap.

auto XML::getTag ( ) const -> std::string

Get key; identifer of the XML.

Returns
tag, identifer of the XML
See also
XML::tag

Definition at line 330 of file XML.cpp.

References tag.

template<class T = std::string>
auto samchon::library::XML::getValue ( ) const -> T
inline

Get value of the XML.

Definition at line 303 of file XML.hpp.

template<class T = std::string>
auto samchon::library::XML::getProperty ( const std::string &  key) const -> T
inline

Get property.

Definition at line 326 of file XML.hpp.

References samchon::HashMap< Key, T, Hash, Pred, Alloc >::get().

Here is the call graph for this function:

auto XML::hasProperty ( const std::string &  tag) const -> bool

Test wheter a property exists or not.

Definition at line 491 of file XML.cpp.

References samchon::HashMap< Key, T, Hash, Pred, Alloc >::has(), propertyMap, samchon::WeakString::replaceAll(), tag, and samchon::WeakString::trim().

Here is the call graph for this function:

auto XML::getPropertyMap ( ) const -> const HashMap<std::string, std::string>&

Get propertyMap.

Definition at line 485 of file XML.cpp.

References propertyMap.

auto XML::toString ( size_t  level = 0) const -> std::string

Get the string content.

Returns a string representation of the XML and its all children.

Returns
A string represents the XML.

Definition at line 647 of file XML.cpp.

References propertyMap, tag, and value.

Member Data Documentation

std::string samchon::library::XML::tag
private

Tag name.

  • <tag label='property' />: tag => "tag"
  • <price high='1500' low='1300' open='1450' close='1320' />: tag => "price"

Definition at line 85 of file XML.hpp.

Referenced by getTag(), hasProperty(), push_back(), setTag(), toString(), and XML().

std::string samchon::library::XML::value
private

Value of the XML.

  • <parameter name='age' type='int'>26</parameter>: value => 26
  • <price high='1500' low='1300' open='1450' close='1320' />: tag => null

Definition at line 94 of file XML.hpp.

Referenced by toString(), and XML().

HashMap<std::string, std::string> samchon::library::XML::propertyMap
private

Properties belongs to the XML.

A Dictionary of properties accessing each property by its key.

  • <price high='1500' low='1300' open='1450' close='1320' />: propertyMap => {{"high": 1500}, {"low": 1300}, {"open": 1450}, {"close", 1320}}
  • <member id='jhnam88' name='Jeongho+Nam' comment='Hello.+My+name+is+Jeongho+Nam' >: propertyMap => {{"id", "jhnam88"}, {"name", "Jeongho Nam http://samchon.org"}, {"comment", "Hello. My name is Jeongho Nam http://samchon.org"}}

Definition at line 106 of file XML.hpp.

Referenced by addAllProperty(), clearProperties(), eraseProperty(), getPropertyMap(), hasProperty(), toString(), and XML().


The documentation for this class was generated from the following files: