00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "utils/xml.h"
00023
00024 #include "log.h"
00025
00026 #include "resources/resourcemanager.h"
00027
00028 namespace XML
00029 {
00030 Document::Document(const std::string &filename):
00031 mDoc(0)
00032 {
00033 int size;
00034 ResourceManager *resman = ResourceManager::getInstance();
00035 char *data = (char*) resman->loadFile(filename.c_str(), size);
00036
00037 if (data) {
00038 mDoc = xmlParseMemory(data, size);
00039 free(data);
00040
00041 if (!mDoc)
00042 logger->log("Error parsing XML file %s", filename.c_str());
00043 } else {
00044 logger->log("Error loading %s", filename.c_str());
00045 }
00046 }
00047
00048 Document::Document(const char *data, int size)
00049 {
00050 mDoc = xmlParseMemory(data, size);
00051 }
00052
00053 Document::~Document()
00054 {
00055 if (mDoc)
00056 xmlFreeDoc(mDoc);
00057 }
00058
00059 xmlNodePtr Document::rootNode()
00060 {
00061 return mDoc ? xmlDocGetRootElement(mDoc) : 0;
00062 }
00063
00064 int getProperty(xmlNodePtr node, const char* name, int def)
00065 {
00066 int &ret = def;
00067
00068 xmlChar *prop = xmlGetProp(node, BAD_CAST name);
00069 if (prop) {
00070 ret = atoi((char*)prop);
00071 xmlFree(prop);
00072 }
00073
00074 return ret;
00075 }
00076
00077 double getFloatProperty(xmlNodePtr node, const char* name, double def)
00078 {
00079 double &ret = def;
00080
00081 xmlChar *prop = xmlGetProp(node, BAD_CAST name);
00082 if (prop) {
00083 ret = atof((char*)prop);
00084 xmlFree(prop);
00085 }
00086
00087 return ret;
00088 }
00089
00090 std::string getProperty(xmlNodePtr node, const char *name,
00091 const std::string &def)
00092 {
00093 xmlChar *prop = xmlGetProp(node, BAD_CAST name);
00094 if (prop) {
00095 std::string val = (char*)prop;
00096 xmlFree(prop);
00097 return val;
00098 }
00099
00100 return def;
00101 }
00102
00103 xmlNodePtr findFirstChildByName(xmlNodePtr parent, const char *name)
00104 {
00105 for_each_xml_child_node(child, parent)
00106 if (xmlStrEqual(child->name, BAD_CAST name))
00107 return child;
00108
00109 return NULL;
00110 }
00111
00112 }