Add functions to add a child node in Xml 73/71673/4
authorSungbae Yoo <sungbae.yoo@samsung.com>
Thu, 26 May 2016 05:45:02 +0000 (14:45 +0900)
committerSungbae Yoo <sungbae.yoo@samsung.com>
Mon, 30 May 2016 10:43:45 +0000 (19:43 +0900)
Change-Id: I0b4c08a791788ad3c5b95637a08ebc9f7672f8f0
Signed-off-by: Sungbae Yoo <sungbae.yoo@samsung.com>
common/xml/document.cpp
common/xml/document.h
common/xml/node.cpp
common/xml/node.h

index 57685a6..fb85b11 100644 (file)
@@ -24,8 +24,7 @@
 
 namespace xml {
 
-Document::Document(const std::string& version) :
-    rootNode(nullptr),
+Document::Document(const std::string& root, const std::string& version) :
     implementation(xmlNewDoc((const xmlChar*)version.c_str()))
 {
     if (implementation == nullptr) {
@@ -33,6 +32,11 @@ Document::Document(const std::string& version) :
     }
 
     implementation->_private = this;
+
+    xmlNode* rootPtr = xmlNewNode(NULL, xmlStrdup((const xmlChar*)root.c_str()));
+    xmlDocSetRootElement(implementation, rootPtr);
+
+    rootNode = new Node(rootPtr);
 }
 
 Document::Document(xmlDoc* doc)
index aab2ef5..bf5250a 100644 (file)
@@ -30,7 +30,7 @@ namespace xml {
 
 class Document {
 public:
-    Document(const std::string& version = XML_DEFAULT_VERSION);
+    Document(const std::string& root, const std::string& version = XML_DEFAULT_VERSION);
     Document(xmlDoc* doc);
 
     ~Document();
index 5bb7c36..a130b62 100644 (file)
@@ -55,6 +55,14 @@ Node::NodeList Node::getChildren()
     return nodeList;
 }
 
+Node Node::addNewChild(const std::string& name)
+{
+   xmlNode* nodePtr = xmlNewNode(NULL, xmlStrdup((const xmlChar*)name.c_str()));
+   xmlAddChild(implementation, nodePtr);
+
+   return Node(nodePtr);
+}
+
 std::string Node::getName() const
 {
     return implementation->name ? (const char*)implementation->name : "";
@@ -65,16 +73,6 @@ void Node::setName(const std::string& name)
     xmlNodeSetName(implementation, (const xmlChar*)name.c_str());
 }
 
-void Node::setContent(const std::string& content)
-{
-    if (implementation->type != XML_ELEMENT_NODE) {
-        throw runtime::Exception("Can not set content for this node type");
-    }
-
-    auto child = implementation->xmlChildrenNode;
-    xmlNodeSetContent(child, (xmlChar*)content.c_str());
-}
-
 std::string Node::getContent() const
 {
     if (implementation->type != XML_ELEMENT_NODE) {
@@ -87,13 +85,14 @@ std::string Node::getContent() const
     return child->content ? (char*)child->content : "";
 }
 
-void Node::setProp(const std::string& name, const std::string& val)
+void Node::setContent(const std::string& content)
 {
     if (implementation->type != XML_ELEMENT_NODE) {
-        throw runtime::Exception("Can not set properties for this node type");
+        throw runtime::Exception("Can not set content for this node type");
     }
 
-    xmlSetProp(implementation, (xmlChar*)name.c_str(), (xmlChar*)val.c_str());
+    auto child = implementation->xmlChildrenNode;
+    xmlNodeSetContent(child, (xmlChar*)content.c_str());
 }
 
 std::string Node::getProp(const std::string& name) const
@@ -109,6 +108,15 @@ std::string Node::getProp(const std::string& name) const
     return result ? (char*)result : "";
 }
 
+void Node::setProp(const std::string& name, const std::string& val)
+{
+    if (implementation->type != XML_ELEMENT_NODE) {
+        throw runtime::Exception("Can not set properties for this node type");
+    }
+
+    xmlSetProp(implementation, (xmlChar*)name.c_str(), (xmlChar*)val.c_str());
+}
+
 bool Node::isBlank() const
 {
     return xmlIsBlankNode(const_cast<xmlNode*>(implementation));
index b25586e..98bf137 100644 (file)
@@ -40,6 +40,7 @@ public:
     Node& operator=(const Node&) = delete;
 
     NodeList getChildren();
+    Node addNewChild(const std::string& name);
 
     std::string getName() const;
     void setName(const std::string& name);