00001
00002
00003 #include "xmldocument.h"
00004
00005 #include "xmlnode.h"
00006 #include "xmlnamespace.h"
00007
00008 #include "libxml/tree.h"
00009 #include "libxml/xpathInternals.h"
00010
00011
00012 namespace xml {
00013
00014
00016
00018 CXMLDocument::CXMLDocument(const std::string &filename) {
00019 if(!this->openDocument(filename)) {
00020
00021 this->m_fileOpenSuccess = false;
00022 } else {
00023 this->m_fileOpenSuccess = true;
00024 }
00025 }
00026
00027
00029
00031 CXMLDocument::~CXMLDocument(void) {
00032 xmlFreeDoc(this->m_documentPtr);
00033 }
00034
00035
00037
00039 CXMLNodeSet* CXMLDocument::evaluateXPathExpression(const std::string &expression, CXMLNameSpaceList *nsList) {
00040
00041
00042
00043 xmlXPathContextPtr xpathContext;
00044 xmlXPathObjectPtr resultObject;
00045 CXMLNodeSet *result;
00046 int i;
00047
00048
00049
00050 xpathContext = xmlXPathNewContext(this->m_documentPtr);
00051 if(xpathContext == NULL) {
00052 printf("ERROR: [%s] Could not create XPath context.\n", __FILE__);
00053 return NULL;
00054 }
00055
00056 if(nsList != NULL) {
00057
00058 for(i = 0; i < nsList->getSize(); i++) {
00059 if(xmlXPathRegisterNs(xpathContext, (xmlChar*)nsList->getElementAt(i).m_nsPrefix.c_str(), (xmlChar*)nsList->getElementAt(i).m_nsHref.c_str()) != 0) {
00060 printf("ERROR: [%s] Could not register namespace.\n", __FILE__);
00061 return NULL;
00062 }
00063 }
00064 }
00065
00066
00067 resultObject = xmlXPathEvalExpression((xmlChar*)expression.c_str(), xpathContext);
00068 if(resultObject == NULL) {
00069 printf("ERROR: [%s] Could not evaluate XPath expression.\n", __FILE__);
00070 xmlXPathFreeContext(xpathContext);
00071 return NULL;
00072 }
00073
00074 result = new CXMLNodeSet(resultObject);
00075
00076
00077 xmlXPathFreeContext(xpathContext);
00078 return result;
00079 }
00080
00081
00083
00085 bool CXMLDocument::isFileOpen(void) {
00086 return this->m_fileOpenSuccess;
00087 }
00088
00089
00091
00093 bool CXMLDocument::openDocument(const std::string &filename) {
00094
00095 this->m_documentPtr = xmlParseFile(filename.c_str());
00096
00097
00098 if(this->m_documentPtr == NULL) {
00099
00100 return false;
00101 } else {
00102 return true;
00103 }
00104 }
00105 };