Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Problem of using selectSingleNode! [modified]

Problem of using selectSingleNode! [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
xmlhelpquestionannouncement
9 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nothend
    wrote on last edited by
    #1

    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

    G 1 Reply Last reply
    0
    • N Nothend

      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

      G Offline
      G Offline
      George L Jackson
      wrote on last edited by
      #2

      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

      N 1 Reply Last reply
      0
      • G George L Jackson

        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

        N Offline
        N Offline
        Nothend
        wrote on last edited by
        #3

        Sorry,I didn't understand very well,do you think the the code pNode=pXMLDoc->selectSingleNode(_bstr_t("MAPDOMAIN")); shoud be replaced by pNode=pXMLDoc->selectSingleNode(_bstr_t("/MAPDOMAIN"));? Thanks!

        G 1 Reply Last reply
        0
        • N Nothend

          Sorry,I didn't understand very well,do you think the the code pNode=pXMLDoc->selectSingleNode(_bstr_t("MAPDOMAIN")); shoud be replaced by pNode=pXMLDoc->selectSingleNode(_bstr_t("/MAPDOMAIN"));? Thanks!

          G Offline
          G Offline
          George L Jackson
          wrote on last edited by
          #4

          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

          N 2 Replies Last reply
          0
          • G George L Jackson

            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

            N Offline
            N Offline
            Nothend
            wrote on last edited by
            #5

            I'v tried what you said,but the value of pNode is still NULL.

            1 Reply Last reply
            0
            • G George L Jackson

              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

              N Offline
              N Offline
              Nothend
              wrote on last edited by
              #6

              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!

              G 2 Replies Last reply
              0
              • N Nothend

                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!

                G Offline
                G Offline
                George L Jackson
                wrote on last edited by
                #7

                "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

                N 1 Reply Last reply
                0
                • N Nothend

                  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!

                  G Offline
                  G Offline
                  George L Jackson
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  • G George L Jackson

                    "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

                    N Offline
                    N Offline
                    Nothend
                    wrote on last edited by
                    #9

                    Thank you very much!The problem is solved!!! Thanks!

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups