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