You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-users@xerces.apache.org by Stephen Torri <st...@torri.org> on 2006/10/04 11:32:27 UTC

Accepted way to convert a XMLString to std::string

The following code causes the STL std::string class to throw a
std::logic_error when I try to initialize a std::string with the results
of XMLString::transcode.

    std::string form_str = XMLString::transcode ( elem_ptr->getNodeValue() );

elem_ptr is a DOMElement*.

Is there an accepted way to convert to a std::string?

Stephen




Re: Accepted way to convert a XMLString to std::string

Posted by Stephen Torri <st...@torri.org>.
On Wed, 2006-10-04 at 13:39 +0200, Alberto Massari wrote:
> The code you wrote would work, but not on DOMElement nodes; 
> getNodeValue in that case returns NULL, so XMLString::transcode 
> returns NULL, and std::string doesn't like that. If the element has 
> only one child node you can use 
> elem_ptr->getFirstChild()->getNodeValue(), or even use 
> elem_ptr->getTextContent() [note: this will raise your memory consumption]

Thanks for the help.

Stephen


Re: Accepted way to convert a XMLString to std::string

Posted by Alberto Massari <am...@datadirect.com>.
Hi Stephen,

At 04.32 04/10/2006 -0500, Stephen Torri wrote:
>The following code causes the STL std::string class to throw a
>std::logic_error when I try to initialize a std::string with the results
>of XMLString::transcode.
>
>     std::string form_str = XMLString::transcode ( elem_ptr->getNodeValue() );
>
>elem_ptr is a DOMElement*.
>
>Is there an accepted way to convert to a std::string?

The code you wrote would work, but not on DOMElement nodes; 
getNodeValue in that case returns NULL, so XMLString::transcode 
returns NULL, and std::string doesn't like that. If the element has 
only one child node you can use 
elem_ptr->getFirstChild()->getNodeValue(), or even use 
elem_ptr->getTextContent() [note: this will raise your memory consumption]

Alberto