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. Adding a Node to a XML file

Adding a Node to a XML file

Scheduled Pinned Locked Moved C / C++ / MFC
questionxmlhelp
9 Posts 3 Posters 1 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.
  • G Offline
    G Offline
    Gilfrog
    wrote on last edited by
    #1

    I'm having a problem adding a node to a XML file. I am able to open the file and read the elements but when i try to add a node is bombs. MSXML::IXMLDOMElement * node; node = document->createElement("Test"); document->appendChild(node); It doesn't like the append to child. What is the correct way to add a new node in a XML docuement? Thanks Scott

    P S 2 Replies Last reply
    0
    • G Gilfrog

      I'm having a problem adding a node to a XML file. I am able to open the file and read the elements but when i try to add a node is bombs. MSXML::IXMLDOMElement * node; node = document->createElement("Test"); document->appendChild(node); It doesn't like the append to child. What is the correct way to add a new node in a XML docuement? Thanks Scott

      P Offline
      P Offline
      Paul M Watt
      wrote on last edited by
      #2

      try this instead: MSXML::IXMLDOMElementPtr node; node = document->createElement("Test"); document->appendChild(node); This way you will use the smart pointer classes that make querying and managing interfaces a lot simpler. You will also need to use the #inport statement to load teh MSXML library and provide thses smart pointers for you.


      Build a man a fire, and he will be warm for a day
      Light a man on fire, and he will be warm for the rest of his life!

      G 1 Reply Last reply
      0
      • P Paul M Watt

        try this instead: MSXML::IXMLDOMElementPtr node; node = document->createElement("Test"); document->appendChild(node); This way you will use the smart pointer classes that make querying and managing interfaces a lot simpler. You will also need to use the #inport statement to load teh MSXML library and provide thses smart pointers for you.


        Build a man a fire, and he will be warm for a day
        Light a man on fire, and he will be warm for the rest of his life!

        G Offline
        G Offline
        Gilfrog
        wrote on last edited by
        #3

        What do i need to import i currently have #import "msxml.dll" named_guids Scott

        P 1 Reply Last reply
        0
        • G Gilfrog

          What do i need to import i currently have #import "msxml.dll" named_guids Scott

          P Offline
          P Offline
          Paul M Watt
          wrote on last edited by
          #4

          That's what you need. Try the smart pointer implementation that I mentioned. If that does not work then make sure that you have a valid document created.


          Build a man a fire, and he will be warm for a day
          Light a man on fire, and he will be warm for the rest of his life!

          G 1 Reply Last reply
          0
          • P Paul M Watt

            That's what you need. Try the smart pointer implementation that I mentioned. If that does not work then make sure that you have a valid document created.


            Build a man a fire, and he will be warm for a day
            Light a man on fire, and he will be warm for the rest of his life!

            G Offline
            G Offline
            Gilfrog
            wrote on last edited by
            #5

            no that bombs the same

            P 1 Reply Last reply
            0
            • G Gilfrog

              no that bombs the same

              P Offline
              P Offline
              Paul M Watt
              wrote on last edited by
              #6

              Have you tried reading the error response codes to see why it bombs? It would also be useful to see the code that you are using starting with where your Document is created.


              Build a man a fire, and he will be warm for a day
              Light a man on fire, and he will be warm for the rest of his life!

              G 1 Reply Last reply
              0
              • P Paul M Watt

                Have you tried reading the error response codes to see why it bombs? It would also be useful to see the code that you are using starting with where your Document is created.


                Build a man a fire, and he will be warm for a day
                Light a man on fire, and he will be warm for the rest of his life!

                G Offline
                G Offline
                Gilfrog
                wrote on last edited by
                #7

                Heres the code String strPathName; strPathName = "C:\\My Documents\\C_Projects\\XML\\books.xml"; CString csString; HRESULT hr; hr = CoInitialize(NULL); if (FAILED(hr)) { return; } hr = CoCreateInstance(MSXML::CLSID_DOMDocument, NULL, CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER, MSXML::IID_IXMLDOMDocument, (LPVOID*)&document); if (!document) { return; } BSTR bstr = NULL; document->put_async(VARIANT_FALSE); bstr = strPathName.AllocSysString(); VARIANT_BOOL varOkay = document->load(bstr); SysFreeString(bstr); BSTR nodeName; if (varOkay) { hr = document->get_documentElement(&element); if (FAILED(hr) || element == NULL) { MessageBox(_T("Empty document!"), _T("Error Loading XML"), MB_ICONWARNING); return ; } element->get_nodeName(&nodeName); m_List.AddString(CString(nodeName)); if(element->hasChildNodes()) { MSXML::IXMLDOMNode* firstChild = NULL; HRESULT hr; hr = element->get_firstChild(&firstChild); if (SUCCEEDED(hr) && firstChild != NULL) { ((MSXML::IXMLDOMElement*)firstChild)->get_nodeName(&nodeName); m_List.AddString(CString(nodeName)); } } MSXML::IXMLDOMElementPtr node; node = document->createElement("Test"); document->appendChild(node); } else { long line, linePos; BSTR reason = NULL; document->get_parseError(&parseError); parseError->get_errorCode(&hr); parseError->get_line(&line); parseError->get_linepos(&linePos); parseError->get_reason(&reason); CString strMsg; strMsg.Format(_T("Error 0x%.8X on line %d, position %d\r\nReason: %s"), hr, line, linePos, CString(reason)); MessageBox(strMsg, _T("Error Loading XML"), MB_ICONWARNING); SysFreeString(reason); return; } on the line -- document->appendChild(node); is gives the error Unhandled exception in XML.exe(kernal32.dll); 0xEO6D7363; Microsoft C++ Exception Scott

                P 1 Reply Last reply
                0
                • G Gilfrog

                  Heres the code String strPathName; strPathName = "C:\\My Documents\\C_Projects\\XML\\books.xml"; CString csString; HRESULT hr; hr = CoInitialize(NULL); if (FAILED(hr)) { return; } hr = CoCreateInstance(MSXML::CLSID_DOMDocument, NULL, CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER, MSXML::IID_IXMLDOMDocument, (LPVOID*)&document); if (!document) { return; } BSTR bstr = NULL; document->put_async(VARIANT_FALSE); bstr = strPathName.AllocSysString(); VARIANT_BOOL varOkay = document->load(bstr); SysFreeString(bstr); BSTR nodeName; if (varOkay) { hr = document->get_documentElement(&element); if (FAILED(hr) || element == NULL) { MessageBox(_T("Empty document!"), _T("Error Loading XML"), MB_ICONWARNING); return ; } element->get_nodeName(&nodeName); m_List.AddString(CString(nodeName)); if(element->hasChildNodes()) { MSXML::IXMLDOMNode* firstChild = NULL; HRESULT hr; hr = element->get_firstChild(&firstChild); if (SUCCEEDED(hr) && firstChild != NULL) { ((MSXML::IXMLDOMElement*)firstChild)->get_nodeName(&nodeName); m_List.AddString(CString(nodeName)); } } MSXML::IXMLDOMElementPtr node; node = document->createElement("Test"); document->appendChild(node); } else { long line, linePos; BSTR reason = NULL; document->get_parseError(&parseError); parseError->get_errorCode(&hr); parseError->get_line(&line); parseError->get_linepos(&linePos); parseError->get_reason(&reason); CString strMsg; strMsg.Format(_T("Error 0x%.8X on line %d, position %d\r\nReason: %s"), hr, line, linePos, CString(reason)); MessageBox(strMsg, _T("Error Loading XML"), MB_ICONWARNING); SysFreeString(reason); return; } on the line -- document->appendChild(node); is gives the error Unhandled exception in XML.exe(kernal32.dll); 0xEO6D7363; Microsoft C++ Exception Scott

                  P Offline
                  P Offline
                  Paul M Watt
                  wrote on last edited by
                  #8

                  Try appending your newly created node to the "element" or root nod eof your document rather than your document. I beleive your problem is that there is already a root element in the document and that is why there is a problem. However appending your new node ot the root element of the document seems to work fine. If you have any other questions feel free to ask. Good Luck


                  Build a man a fire, and he will be warm for a day
                  Light a man on fire, and he will be warm for the rest of his life!

                  1 Reply Last reply
                  0
                  • G Gilfrog

                    I'm having a problem adding a node to a XML file. I am able to open the file and read the elements but when i try to add a node is bombs. MSXML::IXMLDOMElement * node; node = document->createElement("Test"); document->appendChild(node); It doesn't like the append to child. What is the correct way to add a new node in a XML docuement? Thanks Scott

                    S Offline
                    S Offline
                    Stephane Rodriguez
                    wrote on last edited by
                    #9

                    Might not solve the issue, but the API says the child must be a IXMLDOMNode*, not a IXMLDOMElement*.


                    MS quote (http://www.microsoft.com/ddk) : As of September 30, 2002, the Microsoft® Windows® 2000 DDK, the Microsoft Windows 98 DDK, and the Microsoft Windows NT® 4.0 DDK will no longer be available for purchase or download on this site.

                    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