Problem of using selectSingleNode! [modified]
-
Hi all: First ,I declare three variables: IXMLDOMDocumentPtr pXMLDoc; IXMLDOMNodePtr pNode=NULL; HRESULT hr; hr=pXMLDoc.CreateInstance(__uuidof(MSXML2::DOMDocument60)); hr=pXMLDoc->loadXML(_bstr_t(sPath+"MAPDOMAIN.xml")); The variable of hr shows that the pXMLDoc has been loaded successfully,and pXMLDoc is not NULL.But,I use pXMLDoc ::selectSingleNode: pNode=pXMLDoc->selectSingleNode(_bstr_t("MAPDOMAIN"));//MAPDOMAIN is RootNode The strange thing is that the pNode is NULL ,how can that happen? My XML file is like this: ?xml version="1.0" encoding="gb2312"?> 135400 155900 141600 161600 -- modified at 21:23 Saturday 14th April, 2007
-
Hi all: First ,I declare three variables: IXMLDOMDocumentPtr pXMLDoc; IXMLDOMNodePtr pNode=NULL; HRESULT hr; hr=pXMLDoc.CreateInstance(__uuidof(MSXML2::DOMDocument60)); hr=pXMLDoc->loadXML(_bstr_t(sPath+"MAPDOMAIN.xml")); The variable of hr shows that the pXMLDoc has been loaded successfully,and pXMLDoc is not NULL.But,I use pXMLDoc ::selectSingleNode: pNode=pXMLDoc->selectSingleNode(_bstr_t("MAPDOMAIN"));//MAPDOMAIN is RootNode The strange thing is that the pNode is NULL ,how can that happen? My XML file is like this: ?xml version="1.0" encoding="gb2312"?> 135400 155900 141600 161600 -- modified at 21:23 Saturday 14th April, 2007
You are assuming the XML document starts at the Document Element. Your XPath should be "/MAPDOMAIN".
"We make a living by what we get, we make a life by what we give." --Winston Churchill
-
You are assuming the XML document starts at the Document Element. Your XPath should be "/MAPDOMAIN".
"We make a living by what we get, we make a life by what we give." --Winston Churchill
-
Sorry,I didn't understand very well,do you think the the code
pNode=pXMLDoc->selectSingleNode(_bstr_t("MAPDOMAIN"));
shoud be replaced bypNode=pXMLDoc->selectSingleNode(_bstr_t("/MAPDOMAIN"));
? Thanks!Yes. The "/" is the actual root and "MAPDOMAIN" is a child of the root or the Document Element in this case.
"We make a living by what we get, we make a life by what we give." --Winston Churchill
-
Yes. The "/" is the actual root and "MAPDOMAIN" is a child of the root or the Document Element in this case.
"We make a living by what we get, we make a life by what we give." --Winston Churchill
-
Yes. The "/" is the actual root and "MAPDOMAIN" is a child of the root or the Document Element in this case.
"We make a living by what we get, we make a life by what we give." --Winston Churchill
-
I'v tried what you said,but the value of pNode is still NULL. and Could you tell me the difference between Load and LoadXML? When I use LoadXML,it's successful,but the Load is failed,why? Thanks a lot!
"LoadXML" loads the XML document from a string and "Load" from a file. I wrote some code to load from a file: #ifdef _DEBUG #pragma comment(lib, "comsuppwd.lib") #else #pragma comment(lib, "comsuppw.lib") #endif #include #include #import using namespace MSXML2; int _tmain(int argc, _TCHAR* argv[]) { CoInitialize(NULL); HRESULT hr; IXMLDOMDocumentPtr pXMLDom; hr = pXMLDom.CreateInstance(__uuidof(DOMDocument60)); if (FAILED(hr)) { printf("Failed to instantiate DOMDocument80 class\n"); return -1; } pXMLDom->async = VARIANT_FALSE; if(pXMLDom->load(L"MapDomain.xml") != VARIANT_TRUE) { printf("Failed load xml data from file.\n%s\n", (LPCSTR)pXMLDom->parseError->Getreason()); return -1; } IXMLDOMNodePtr pNode = pXMLDom->selectSingleNode(L"/MAPDOMAIN"); if (pNode == NULL) { printf("Invalid node fetched.\n%s\n", (LPCSTR)pXMLDom->parseError->Getreason()); } else { printf("Result from selectSingleNode:\nNode - <%s>:\n", (LPCSTR)pNode->nodeName); } pNode.Release(); pXMLDom.Release(); CoUninitialize(); return 0; } "We make a living by what we get, we make a life by what we give." --Winston Churchill -- modified at 23:52 Saturday 14th April, 2007
-
I'v tried what you said,but the value of pNode is still NULL. and Could you tell me the difference between Load and LoadXML? When I use LoadXML,it's successful,but the Load is failed,why? Thanks a lot!
Also, make sure the pathname to your xml document is correct. An incorrect pathname could cause it to fail.
"We make a living by what we get, we make a life by what we give." --Winston Churchill
-
"LoadXML" loads the XML document from a string and "Load" from a file. I wrote some code to load from a file: #ifdef _DEBUG #pragma comment(lib, "comsuppwd.lib") #else #pragma comment(lib, "comsuppw.lib") #endif #include #include #import using namespace MSXML2; int _tmain(int argc, _TCHAR* argv[]) { CoInitialize(NULL); HRESULT hr; IXMLDOMDocumentPtr pXMLDom; hr = pXMLDom.CreateInstance(__uuidof(DOMDocument60)); if (FAILED(hr)) { printf("Failed to instantiate DOMDocument80 class\n"); return -1; } pXMLDom->async = VARIANT_FALSE; if(pXMLDom->load(L"MapDomain.xml") != VARIANT_TRUE) { printf("Failed load xml data from file.\n%s\n", (LPCSTR)pXMLDom->parseError->Getreason()); return -1; } IXMLDOMNodePtr pNode = pXMLDom->selectSingleNode(L"/MAPDOMAIN"); if (pNode == NULL) { printf("Invalid node fetched.\n%s\n", (LPCSTR)pXMLDom->parseError->Getreason()); } else { printf("Result from selectSingleNode:\nNode - <%s>:\n", (LPCSTR)pNode->nodeName); } pNode.Release(); pXMLDom.Release(); CoUninitialize(); return 0; } "We make a living by what we get, we make a life by what we give." --Winston Churchill -- modified at 23:52 Saturday 14th April, 2007