For Programmers: Free Programming Magazines  


Home > Archive > VC Language > September 2005 > How to create a std::string from a _variant_t?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to create a std::string from a _variant_t?
Daniel Lidström

2005-09-07, 3:58 am

Hello!

I'm trying to add a method to an xml-nodewrapper that wraps a
MSXML2::IXMLDOMNodePtr. The method should get the value of the node. Here's
my current attempt:

class CXmlNodeWrapper {
...
MSXML2::IXMLDOMNodePtr m_xmlnode;
};

std::string CXmlNodeWrapper::GetNodeValue() const
{
return (LPCSTR)m_xmlnode->GetnodeValue();
}

This doesn't work because: cannot convert from '_variant_t' to 'LPCSTR'.
How should this be done?
Thanks in advance!

--
Daniel
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Rodrigo Corral [MVP]

2005-09-07, 3:58 am

I haven't tried this but it must work:

std::string CXmlNodeWrapper::GetNodeValue() const
{
return static_cast<_bstr_t>(m_xmlnode->GetnodeValue());
}


--
Un saludo
Rodrigo Corral González [MVP]

FAQ de microsoft.public.es.vc++
http://rcorral.mvps.org


Daniel Lidström

2005-09-07, 7:58 am

On Wed, 7 Sep 2005 10:33:32 +0200, Rodrigo Corral [MVP] wrote:

> I haven't tried this but it must work:
>
> std::string CXmlNodeWrapper::GetNodeValue() const
> {
> return static_cast<_bstr_t>(m_xmlnode->GetnodeValue());
> }


This did compile, except there was a warning I didn't really understand
(this warning was formatted with STLTask):

c:\projects\GEOW\V12\Src\GData\Xml\XmlNo
deWrapper.cpp(615): warning C4927:
illegal conversion; more than one user-defined conversion has been
implicitly applied
while calling the constructor
'basic_string<_Elem,_Traits,_Ax>::basic_string(const _Elem *)'
with
[
_Elem=char,
_Traits=char_traits<char>,
_Ax=allocator<char>
]
c:\Program Files\Microsoft Visual Studio .NET
2003\Vc7\include\xstring(358): see declaration of
'basic_string<_Elem,_Traits,_Ax>::__ctor'
with
[
_Elem=char,
_Traits=char_traits<char>,
_Ax=allocator<char>
]
and
[
_Elem=char,
_Traits=char_traits<char>,
_Ax=allocator<char>
]

Line 615 is this statement:
return static_cast<_bstr_t>(m_xmlnode->GetnodeValue());

I tried debugging anyway, and when my CXmlNodeWrapper says that node type
is 'text', I called GetNodeValue:

//! This is the NodeType method.
//! m_xmlnode is a MSXML2::IXMLDOMNodePtr.
string CXmlNodeWrapper::NodeType()
{
if (IsValid())
return (LPCSTR)m_xmlnode->GetnodeTypeString();
return "";
}

Here's the code I'm debugging (Sorry, it is a bit difficult to provide a
minimum compilable program that demonstrates the problem. If you want it,
I'll try to make one though):

//! Get pointer to child node
CXmlNodeWrapper child_node(...);

//! Now get value of current node
if( child_node.NodeType()=="text" )
{
const char* value = current_node.GetNodeValue().c_str();
...
}

This throws an exception at the GetNodeValue() call. What's going on? I'm
trying to get the value of the node, for example, if the xml looks like
this:

<?xml version="1.0" encoding="utf-8"?>
<Model mod_att1="val1" mod_att2="val2">__model_value__
<MainLine att1="val1" att2="val2">__mainline_value__
<start>1234.5 5234.3</start>
<end>76896.2 24356.3</end>
</MainLine>
</Model>

If my CXmlNodeWrapper (which wraps a MSXML2::IXMLDOMNodePtr) points to the
Model element, I wish to get the __model_value__ only. If I call
m_xmlnode->Gettext(); I get __model_value__ and __mainline_value__ in the
same string. Gettext() apparently gives the current element value along
with all children element values. How can I get just the current element's
value?

Thanks in advance!


--
Daniel
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Daniel Lidström

2005-09-08, 7:02 pm

On Wed, 7 Sep 2005 11:27:06 +0200, Daniel Lidström wrote:
[color=darkred]
> On Wed, 7 Sep 2005 10:33:32 +0200, Rodrigo Corral [MVP] wrote:
>

I figured out how to do it. There was a bug in my program. Thanks Rodrigo!
By the way, here's the required line:

string CXmlNodeWrapper::GetNodeValue() const
{
return (char*)((_bstr_t)m_xmlnode->GetnodeValue());
}


--
Daniel
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Rodrigo Corral [MVP]

2005-09-10, 7:00 pm

I'd rather use c++ casting style, static_cast<>...

--
Un saludo
Rodrigo Corral González [MVP]

FAQ de microsoft.public.es.vc++
http://rcorral.mvps.org


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com