ludocluba wrote:
Hi,
What is the best way to set a string attribute of a class from a XML Tree:
And the best way to set a double value
Here I'm just interest in the XMLString::transcode and XMLString::release
mechanism
Example of what I planned to do:
<node1>
<node>value</node>
<double>1.123</double>
</node1>
class A
{
string strvalue;
float fvalue;
}
You should probably use double instead of float here.
(...)
DOMNode* node1 = node->getFirstChild();
while(node1)
{
char* temp1 =XMLString::transcode(node->getNodeName());
if (strcmp(temp1,"node1") == 0)
{
DOMNode* node2 = node1->getFirstChild();
while (node2)
{
char* temp2 = XMLString::transcode(node2->getNodeName());
if (strcmp(temp2,"node") == 0)
{
DOMNode* node3 = node2->getFirstChild();
A.strvalue = XMLString::transcode(node3->getNodeValue());
//Should I do the release in the destructor of A?
That won't help, because the temporary pointer has already been lost.
Instead, do this:
char* temp = XMLString::transcode(node3->getNodeValue());
A.strvalue = temp;
XMLString::release(&temp);
}
else if (strcmp(temp2,"double") == 0)
{
DOMNode* node3 = node2->getFirstChild();
char* temp3 =
XMLString::transcode(node3->getNodeValue());
A.fvalue = atof(temp3);
XMLString::release(&temp3);
}
XMLString::release(&temp2);
node2 = node2->getNextSibling();
}
}
XMLString::release(&temp1);
node1 = node1->getNextSibling();
}
(...)
Is it the best way?
A better idea for the node name comparsions is to avoid transcoding, and
use some constant UTF-16 strings instead:
#include "xercesc/util/XMLUni.h"
XERCES_CPP_NAMESPACE_USE
const XMLCh node1[] = {
chLatin_n,
chLatin_o,
chLatin_d,
chLatin_e,
chDigit_1,
chNull
};
if (XMLString::equals(node->getNodeName(), node1))
{
...
etc.
Dave
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]