|
00001 00012 #include <atlbase.h> 00013 #import <msxml3.dll> 00014 00015 #define IXMLDOMNodePtr MSXML2::IXMLDOMNodePtr 00016 #define IXMLDOMDocumentPtr MSXML2::IXMLDOMDocumentPtr 00017 #define IXMLDOMNodeListPtr MSXML2::IXMLDOMNodeListPtr 00018 #define IXMLDOMAttribute MSXML2::IXMLDOMAttribute 00019 00020 #include <assert.h> 00021 #include "XmlNode.h" 00022 #include "../common/standard.h" 00023 00024 #define NODE (*((IXMLDOMNodePtr*)node)) 00025 #define NODEDOC (*((IXMLDOMDocumentPtr*)node)) 00026 #define NODELIST (*((IXMLDOMNodeListPtr*)nodeListPtr)) 00027 00028 #define PNODE ((IXMLDOMNodePtr*)node) 00029 #define PNODEDOC ((IXMLDOMDocumentPtr*)node) 00030 #define PNODELIST ((IXMLDOMNodeListPtr*)nodeListPtr) 00031 00032 00033 00034 XmlNode::XmlNode() 00035 { 00036 CoInitialize (NULL); 00037 node = NULL; 00038 bIsDoc = false; 00039 }; 00040 00041 00042 XmlNode::XmlNode (void *ptr) 00043 { 00044 if (!ptr) 00045 throw ArgError("XmlNode::XmlNode(): bad ptr param."); 00046 00047 node = ptr; 00048 bIsDoc = false; 00049 } 00050 00051 00052 XmlNode::~XmlNode() 00053 { 00054 if (node) 00055 { 00056 if (bIsDoc) 00057 delete PNODEDOC; 00058 else 00059 delete PNODE; 00060 } 00061 } 00062 00063 00064 void XmlNode::loadFile (const char *filename) 00065 { 00066 IXMLDOMDocumentPtr *doc = new IXMLDOMDocumentPtr; 00067 if (FAILED (doc->CreateInstance ("Msxml2.DOMDocument"))) 00068 { 00069 throw Error("XmlDoc::loadFromFile(): unable to create msxml2 document."); 00070 } 00071 00072 if (!(*doc)->load(bstr_t(filename))) 00073 throw RuntimeError("%s is not a valid XML file.", filename); 00074 00075 node = doc; 00076 bIsDoc = true; 00077 } 00078 00079 XmlNodeList XmlNode::getNodes (char *str) 00080 { 00081 IXMLDOMNodeListPtr *nodes = new IXMLDOMNodeListPtr; 00082 *nodes = NODE->selectNodes(str); 00083 if (*nodes == NULL) 00084 throw Error("XmlNode::getNodes(): unable to select %s nodes.", str); 00085 00086 return XmlNodeList (nodes); 00087 } 00088 00089 void XmlNode::getAttrib (int &rtn, char *str) const 00090 { 00091 if (1 != sscanf (getAttrib(str).c_str(), "%d", &rtn)) 00092 throw Error("Can't find a number in the following xml tag: %s", str); 00093 } 00094 00095 00096 void XmlNode::getAttrib (float &rtn, char *str) const 00097 { 00098 if (1 != sscanf (getAttrib(str).c_str(), "%f", &rtn)) 00099 throw Error("Can't find a number in the following xml tag: %s", str); 00100 } 00101 00102 void XmlNode::getAttrib (double &rtn, char *str) const 00103 { 00104 if (1 != sscanf (getAttrib(str).c_str(), "%lf", &rtn)) 00105 throw Error("Can't find a number in the following xml tag: %s", str); 00106 } 00107 00108 std::string XmlNode::getAttrib (char *str) const 00109 { 00110 IXMLDOMNodePtr foundNode = NODE->selectSingleNode(bstr_t(str)); 00111 00112 if (foundNode == NULL) 00113 throw Error("XmlDoc::getAttribute(): unable to get string for node %s", str); 00114 00115 CComQIPtr<IXMLDOMAttribute> attr = foundNode; 00116 if ( attr.p == NULL ) 00117 throw Error("Unable to query for 'IXMLDOMAttribute' XML element interface, for %s", str); 00118 00119 return std::string(attr->Gettext()); 00120 } 00121 00122 00123 XmlNode XmlNodeList::getNode (int index) 00124 { 00125 IXMLDOMNodePtr *rtn = new IXMLDOMNodePtr; 00126 *rtn = NODELIST->Getitem(index); 00127 00128 if (NULL == *rtn) 00129 throw Error("XmlNodeList::getNode(): can't find node #%d", index); 00130 00131 return XmlNode (rtn); 00132 } 00133 00134 int XmlNodeList::getCount() 00135 { 00136 return NODELIST->Getlength(); 00137 } 00138 00139 00140 XmlNodeList::XmlNodeList () 00141 { 00142 CoInitialize (NULL); 00143 nodeListPtr = NULL; 00144 } 00145 00146 00147 XmlNodeList::XmlNodeList (void *nl) 00148 { 00149 CoInitialize (NULL); 00150 if (nl == NULL) 00151 ArgError( "XmlNodeList::XmlNodeList(): void pointer param unacceptable."); 00152 nodeListPtr = nl; 00153 } 00154 00155 00157 XmlNodeList::~XmlNodeList() 00158 { 00159 if (nodeListPtr) 00160 delete PNODELIST; 00161 }