00001
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "XmlDocument.h"
00024 #include "XmlException.h"
00025
00026 namespace Xmlw {
00027
00028 XmlDocument::XmlDocument(const std::string& filename,const std::string& verify_ns,const std::string& verify_root)
00029 : m_doc(NULL)
00030 , m_ok(false)
00031 {
00032 xmlNodePtr cur;
00033 xmlNsPtr ns;
00034
00035 if (!(m_doc = xmlParseFile(filename.c_str() )))
00036 {
00037 throw XmlException("Parse of file failed: " + filename);
00038 }
00039 if (!(cur = xmlDocGetRootElement(m_doc)))
00040 {
00041 xmlFreeDoc(m_doc);
00042 m_doc = NULL;
00043 throw XmlException("Document is empty: " + filename);
00044 }
00045 if (verify_ns.size())
00046 {
00047 if (!(ns = xmlSearchNsByHref(m_doc, cur, (const xmlChar *) verify_ns.c_str() )))
00048 {
00049 xmlFreeDoc(m_doc);
00050 m_doc = NULL;
00051 throw XmlException("Document namespace != " + verify_ns);
00052 }
00053 }
00054 if (verify_root.size())
00055 {
00056 if (xmlStrcmp(cur -> name, (const xmlChar *) verify_root.c_str() ))
00057 {
00058 xmlFreeDoc(m_doc);
00059 m_doc = NULL;
00060 throw XmlException("Document root != " + verify_root);
00061 }
00062 }
00063 m_ok = true;
00064 }
00065
00066
00067 XmlDocument::~XmlDocument()
00068 {
00069 if (m_doc)
00070 {
00071 xmlFreeDoc(m_doc);
00072 }
00073 }
00074
00075
00076 XmlDocument::operator xmlDocPtr()
00077 {
00078 return m_doc;
00079 }
00080
00081
00082 }