change the att value and name of a node (Visual C++ && DOM - )
-
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!
-
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!
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
-
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
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!
-
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!
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 thanLPCSTR
orCString _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/fromBSTRT
-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