00001 #include "xmlnode.h" 00002 #include "exceptions.h" 00003 00004 namespace freedaisy { 00005 XMLNode::XMLNode() { 00006 _xmlnode = xmlNewNode(NULL, (xmlChar*)""); 00007 } 00008 00009 XMLNode::XMLNode(std::string name) { 00010 _xmlnode = xmlNewNode(NULL, (xmlChar*)name.c_str()); 00011 } 00012 00013 XMLNode::XMLNode(xmlNodePtr src) { 00014 _xmlnode = src; 00015 } 00016 00017 XMLNode::XMLNode(XMLNode *src) { 00018 _xmlnode = src->getC(); 00019 } 00020 00021 std::string XMLNode::operator[](std::string attr) { 00022 return XMLNode::getAttr(attr); 00023 } 00024 00025 std::string XMLNode::getName() { 00026 return std::string((char*)_xmlnode->name); 00027 } 00028 00029 std::string XMLNode::getContent() { 00030 return std::string((char*)xmlNodeGetContent(_xmlnode)); 00031 } 00032 00033 void XMLNode::setContent(std::string content) { 00034 xmlNodeSetContent(_xmlnode, (xmlChar*)content.c_str()); 00035 } 00036 00037 int XMLNode::hasAttr(std::string name) { 00038 if (xmlHasProp(_xmlnode, (xmlChar*)name.c_str())) { 00039 return 0; 00040 } else { 00041 return 1; 00042 } 00043 } 00044 00045 std::string XMLNode::getAttr(std::string name) { 00046 return std::string((char*)xmlGetProp(_xmlnode, (xmlChar*)name.c_str())); 00047 } 00048 00049 void XMLNode::setAttr(std::string name, std::string attr) { 00050 xmlSetProp(_xmlnode, (xmlChar*)name.c_str(), (xmlChar*)attr.c_str()); 00051 } 00052 00053 void XMLNode::addChild(XMLNode* cur) { 00054 xmlNodePtr curnode = cur->getC(); 00055 if (xmlAddChild(_xmlnode, curnode) == NULL) 00056 throw Exceptions::XMLException("Failed to add child"); 00057 } 00058 00059 std::vector<XMLNode> XMLNode::getChildren() { 00060 return XMLNode(_xmlnode->children).getNextNodes(true); 00061 } 00062 00063 std::vector<XMLNode> XMLNode::getNextNodes(bool withme) { 00064 std::vector<XMLNode> nodelist; 00065 xmlNodePtr nextnode = _xmlnode; 00066 if ((nextnode->type == XML_ELEMENT_NODE) && (withme == true)) { 00067 nodelist.push_back(nextnode); 00068 } 00069 while ((nextnode = nextnode->next) != NULL) { 00070 if (nextnode->type == XML_ELEMENT_NODE) 00071 nodelist.push_back(XMLNode(nextnode)); 00072 } 00073 return nodelist; 00074 } 00075 00076 XMLNode XMLNode::getParent() { 00077 return XMLNode(_xmlnode->parent); 00078 } 00079 00080 void XMLNode::unlink() { 00081 xmlUnlinkNode(_xmlnode); 00082 } 00083 00084 xmlNodePtr XMLNode::getC() { 00085 return _xmlnode; 00086 } 00087 }
1.4.6