From: Sungbae Yoo Date: Thu, 26 May 2016 05:45:02 +0000 (+0900) Subject: Add functions to add a child node in Xml X-Git-Tag: accepted/tizen/ivi/20160602.231346~9 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F73%2F71673%2F4;p=platform%2Fcore%2Fsecurity%2Fdevice-policy-manager.git Add functions to add a child node in Xml Change-Id: I0b4c08a791788ad3c5b95637a08ebc9f7672f8f0 Signed-off-by: Sungbae Yoo --- diff --git a/common/xml/document.cpp b/common/xml/document.cpp index 57685a6..fb85b11 100644 --- a/common/xml/document.cpp +++ b/common/xml/document.cpp @@ -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) diff --git a/common/xml/document.h b/common/xml/document.h index aab2ef5..bf5250a 100644 --- a/common/xml/document.h +++ b/common/xml/document.h @@ -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(); diff --git a/common/xml/node.cpp b/common/xml/node.cpp index 5bb7c36..a130b62 100644 --- a/common/xml/node.cpp +++ b/common/xml/node.cpp @@ -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(implementation)); diff --git a/common/xml/node.h b/common/xml/node.h index b25586e..98bf137 100644 --- a/common/xml/node.h +++ b/common/xml/node.h @@ -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);