00001
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "BaseXMLFile.h"
00025
00026
00027
00028
00029 BaseXMLFile::BaseXMLFile(const std::string& filename,const std::string& verify_ns,const std::string& verify_root)
00030 : m_doc(NULL)
00031 ,m_current(NULL)
00032 {
00033 xmlNodePtr cur;
00034 xmlNsPtr ns;
00035
00036 if (!(m_doc = xmlParseFile(filename.c_str() )))
00037 {
00038 fprintf(stderr,"xmlParseFile('%s') failed\n",filename.c_str());
00039 return;
00040 }
00041
00042 if (!(cur = xmlDocGetRootElement(m_doc)))
00043 {
00044 fprintf(stderr, "Empty document\n");
00045 xmlFreeDoc(m_doc);
00046 m_doc = NULL;
00047 return;
00048 }
00049
00050 if (!verify_ns.size())
00051 {
00052 return;
00053 }
00054 if (!(ns = xmlSearchNsByHref(m_doc, cur, (const xmlChar *) verify_ns.c_str() )))
00055 {
00056 fprintf(stderr, "Document namespace != '%s'\n", verify_ns.c_str());
00057 xmlFreeDoc(m_doc);
00058 m_doc = NULL;
00059 return;
00060 }
00061
00062 if (!verify_root.size())
00063 {
00064 return;
00065 }
00066 if (xmlStrcmp(cur -> name, (const xmlChar *) verify_root.c_str() ))
00067 {
00068 fprintf(stderr, "Document root != '%s'\n",verify_root.c_str());
00069 xmlFreeDoc(m_doc);
00070 m_doc = NULL;
00071 return;
00072 }
00073
00074 }
00075
00076
00077 BaseXMLFile::~BaseXMLFile()
00078 {
00079 if (m_doc)
00080 {
00081 xmlFreeDoc(m_doc);
00082 }
00083 }
00084
00085
00086 xmlNodePtr BaseXMLFile::GetRootElement()
00087 {
00088 m_current = m_doc ? xmlDocGetRootElement(m_doc) : NULL;
00089 return m_current;
00090 }
00091
00092
00093 std::string BaseXMLFile::GetProperty(const std::string& name)
00094 {
00095 xmlChar *p = m_current ? xmlGetProp(m_current, (const xmlChar *) name.c_str() ) : NULL;
00096 if (!p)
00097 {
00098 return "";
00099 }
00100 std::string str = (char *)p;
00101 xmlFree(p);
00102 return str;
00103 }
00104
00105
00106 xmlNodePtr BaseXMLFile::GetChildrenNode()
00107 {
00108 m_current = m_current ? m_current -> xmlChildrenNode : NULL;
00109 return m_current;
00110 }
00111
00112
00113 xmlNodePtr BaseXMLFile::GetNextNode()
00114 {
00115 do
00116 {
00117 m_current = m_current ? m_current -> next : NULL;
00118 } while (m_current && xmlIsBlankNode( m_current ));
00119 return m_current;
00120 }
00121
00122
00123 const std::string& BaseXMLFile::GetNodeName()
00124 {
00125 if (m_current)
00126 {
00127 m_current_name = (char *)m_current -> name;
00128 }
00129 else
00130 {
00131 m_current_name = "";
00132 }
00133 return m_current_name;
00134 }
00135
00136
00137 xmlNsPtr BaseXMLFile::GetNodeNs()
00138 {
00139 if (m_current)
00140 return m_current -> ns;
00141 return NULL;
00142 }
00143
00144
00145 const std::string& BaseXMLFile::GetNodeNsPrefix()
00146 {
00147 if (m_current && m_current -> ns && m_current -> ns -> prefix)
00148 {
00149 m_ns_prefix = (char *)m_current -> ns -> prefix;
00150 }
00151 else
00152 {
00153 m_ns_prefix = "";
00154 }
00155 return m_ns_prefix;
00156 }
00157
00158
00159 const std::string& BaseXMLFile::GetNodeNsHref()
00160 {
00161 if (m_current && m_current -> ns && m_current -> ns -> href)
00162 {
00163 m_ns_href = (char *)m_current -> ns -> href;
00164 }
00165 else
00166 {
00167 m_ns_href = "";
00168 }
00169 return m_ns_href;
00170 }
00171
00172
00173 xmlNodePtr BaseXMLFile::GetFirstElement(const std::string& name)
00174 {
00175 GetRootElement();
00176 xmlNodePtr p = GetChildrenNode();
00177 while (p)
00178 {
00179 if (name == GetNodeName())
00180 {
00181 return p;
00182 }
00183 p = GetNextNode();
00184 }
00185 return NULL;
00186 }
00187
00188
00189 xmlNodePtr BaseXMLFile::GetFirstElement(xmlNodePtr base,const std::string& name)
00190 {
00191 SetCurrent(base);
00192 xmlNodePtr p = GetChildrenNode();
00193 while (p)
00194 {
00195 if (name == GetNodeName())
00196 {
00197 return p;
00198 }
00199 p = GetNextNode();
00200 }
00201 return NULL;
00202 }
00203
00204
00205 xmlNodePtr BaseXMLFile::GetNextElement(xmlNodePtr p,const std::string& name)
00206 {
00207 SetCurrent(p);
00208 p = GetNextNode();
00209 while (p)
00210 {
00211 if (name == GetNodeName())
00212 {
00213 return p;
00214 }
00215 p = GetNextNode();
00216 }
00217 return NULL;
00218 }
00219
00220