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. XML / XSL
  4. change the att value and name of a node (Visual C++ && DOM - )

change the att value and name of a node (Visual C++ && DOM - )

Scheduled Pinned Locked Moved XML / XSL
c++tutorialquestionhtmlhelp
4 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.
  • S Offline
    S Offline
    SimplySane
    wrote on last edited by
    #1

    1.) How do you change the the attribute value of a certain node?? Or add an attribute to a node?? Say for example:

    <book id="bk112" status="New">

    Then you want to change the status value to "Old" and add another attribute like Cover="SoftBound". The IXMLDOMNode::Getattributes() can be used to get the attributes of a node. This will return a ptr to IXMLDOMNamedNodeMap. To access each attribute, the Getitem() function which will return a ptr to IXMLDOMNode. I know how to get the attributes name and value but to change it, it seems like Im stuck... I can see a setAttribute() function in IXMLDOMElement... It looks like the answer to my first question.. BUT, how am I gonna implement when the Getitem returns a ptr to IXMLDOMNode and not to IXMLDOMElement?? IXMLDOMElement inherits from IXMLDOMNode but this can't be solve by casting... 2. How can IXMLDOMNode be converted to IXMLDOMElement?? ANy ideas please?? or any solution.. Thanks!

    L 1 Reply Last reply
    0
    • S SimplySane

      1.) How do you change the the attribute value of a certain node?? Or add an attribute to a node?? Say for example:

      <book id="bk112" status="New">

      Then you want to change the status value to "Old" and add another attribute like Cover="SoftBound". The IXMLDOMNode::Getattributes() can be used to get the attributes of a node. This will return a ptr to IXMLDOMNamedNodeMap. To access each attribute, the Getitem() function which will return a ptr to IXMLDOMNode. I know how to get the attributes name and value but to change it, it seems like Im stuck... I can see a setAttribute() function in IXMLDOMElement... It looks like the answer to my first question.. BUT, how am I gonna implement when the Getitem returns a ptr to IXMLDOMNode and not to IXMLDOMElement?? IXMLDOMElement inherits from IXMLDOMNode but this can't be solve by casting... 2. How can IXMLDOMNode be converted to IXMLDOMElement?? ANy ideas please?? or any solution.. Thanks!

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      SimplySane wrote:

      IXMLDOMElement inherits from IXMLDOMNode but this can't be solve by casting...

      If you are using COM interfaces then you use COM interfaces right? QueryInterface()? However for working with the MSXML DOM in C++ I recommend using #import to generate ATL code for you. If you do then you can cast because the ATL code will perform the QueryInterface for you. ;) http://www.ddj.com/windows/184416877[^]

      led mike

      S 1 Reply Last reply
      0
      • L led mike

        SimplySane wrote:

        IXMLDOMElement inherits from IXMLDOMNode but this can't be solve by casting...

        If you are using COM interfaces then you use COM interfaces right? QueryInterface()? However for working with the MSXML DOM in C++ I recommend using #import to generate ATL code for you. If you do then you can cast because the ATL code will perform the QueryInterface for you. ;) http://www.ddj.com/windows/184416877[^]

        led mike

        S Offline
        S Offline
        SimplySane
        wrote on last edited by
        #3

        Yes, you're right.. I was able to cast succesfully but I got a runtime error when I tried to use the setAttribute.. Here's what I did:

        // assume I have a loop here
        IXMLDOMNodePtr pAttN = pMapAtt->Getitem( y );
        IXMLDOMElementPtr pAtt;

        CString csOutAttName = static_cast<LPCTSTR>( pAttN->nodeName );
        CString csOutAttText = static_cast<LPCTSTR>( pAttN->Gettext() );

        pAtt = (IXMLDOMElementPtr) pAttN;
        BSTR bstrOutAttName = csOutAttName.AllocSysString();
        pAtt->setAttribute( bstrOutAttName, static_cast<_variant_t>( csInAttText ) );
        ::SysFreeString( bstrOutAttName );

        I've got an exception in this line:

        pAtt->setAttribute( bstrOutAttName, static_cast<_variant_t>( csInAttText ) );

        Looking up at setAttribute function, it has this syntax: setAttribute(BSTR, VARIANT) I havn't figure out what's causing the problem here and Im thinking that passing the _variant_t to a VARIANT might be the problem?? (Assuming I cast the right way..)So I've tried searching for ways on how to cast CString to VARIANT, but didn't find anything that works.. Any idea please... Thanks!

        L 1 Reply Last reply
        0
        • S SimplySane

          Yes, you're right.. I was able to cast succesfully but I got a runtime error when I tried to use the setAttribute.. Here's what I did:

          // assume I have a loop here
          IXMLDOMNodePtr pAttN = pMapAtt->Getitem( y );
          IXMLDOMElementPtr pAtt;

          CString csOutAttName = static_cast<LPCTSTR>( pAttN->nodeName );
          CString csOutAttText = static_cast<LPCTSTR>( pAttN->Gettext() );

          pAtt = (IXMLDOMElementPtr) pAttN;
          BSTR bstrOutAttName = csOutAttName.AllocSysString();
          pAtt->setAttribute( bstrOutAttName, static_cast<_variant_t>( csInAttText ) );
          ::SysFreeString( bstrOutAttName );

          I've got an exception in this line:

          pAtt->setAttribute( bstrOutAttName, static_cast<_variant_t>( csInAttText ) );

          Looking up at setAttribute function, it has this syntax: setAttribute(BSTR, VARIANT) I havn't figure out what's causing the problem here and Im thinking that passing the _variant_t to a VARIANT might be the problem?? (Assuming I cast the right way..)So I've tried searching for ways on how to cast CString to VARIANT, but didn't find anything that works.. Any idea please... Thanks!

          L Offline
          L Offline
          led mike
          wrote on last edited by
          #4

          first why use CString unless you are manipulating the string value? this should work pAtt->setAttribute( pAttN->nodeName, pAttN->Gettext()); When you must have a BSTR variable prefer using _bstr_t rather than LPCSTR or CString _bstr_t sname = pAttN->nodeName; _bstr_t svalue = pAttN->Gettext(); pAtt->setAttribute(sname,svalue); Take a look at _bstr_t it has built in conversions to/from BSTRT-LPCTSTR I believe, I haven't worked with this stuff for a couple years now, but I think it's comprehensive, ctors that take either and conversion operators that get either so all you do is cast to the type you want.

          led mike

          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