David N Bertoni/Cambridge/IBM wrote:


1. XMLString::transcode() returns a pointer to dynamic memory that you
must delete. If you don't, it's a memory leak:

char* str = XMLString::transcode(...);

delete [] str;

I am assigning this value to a string... do I have to delete this string, too ??

  2. Element nodes have no value, so getValue() is probably returning a
  null pointer.  The "value" of an Element is contained in its Text
  children, so you must iterate through the children to get the value.

According to the API getNodeValue() returns a const XMLCh*.. what does the returnvalue contain, if not the text of the node 'element' ?? Even if it returns null, why does it crash ?? this null value should be assigned to string....!??

Do you have any hints, which classes/methods/mechanisms to use, when iterating through the Text Children ??

thanks Boris

I am a beginner, so hope you're not bored with my problem...

I am using gcc3.2 on Redhat 8.0... I compiled the xerces-c library
myself...
The program crashes, as soon, as I try to call the getNodeValue() method
of a DOMNode...
The used xml file (test.xml) is at the bottom....
According to the API all types are correct... What am I doing wrong ??
Read the comments in the prgram
for further information.
It compiles without any warnings (though -Wall), and without errors...
Maybe, I've something done wrong, when compiling the xerces-c library
(I used './runConfigure -p linux -c gcc -x g++' and then 'gmake' as
recommended in the docs)??

//#####################################
#include <iostream>
#include <string>

#include <xercesc/dom/DOM.hpp>
#include <xercesc/parsers/XercesDOMParser.hpp>
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/sax/HandlerBase.hpp>

using namespace std;

int main(int argc, char* argv[]) {

try {
XMLPlatformUtils::Initialize();
}
catch(const XMLException& toCatch){
cout << "Exception message is:" <<
XMLString::transcode(toCatch.getMessage())
<< endl;
return -1;
}

XercesDOMParser* parser = new XercesDOMParser();

ErrorHandler* err = (ErrorHandler*) new HandlerBase();
parser->setErrorHandler(err);


char* xmlFile = "/home/boris/test.xml";

try {
parser->parse(xmlFile);
}
catch (XMLException& toCatch){
cout << "Exception is: " << XMLString::transcode (toCatch.getMessage())
<< endl;
return -1;
}
catch (DOMException& toCatch){
cout << "Exception is: " << XMLString::transcode (toCatch.msg) << endl;
return -1;
}
catch (...) {
cout << "unexpected Exception: " << endl;
return -1;
}

DOMDocument* doc = parser->getDocument();
DOMNodeList* list =
doc->getElementsByTagName(XMLString::transcode("element"));
string text = "";

DOMNode* node = list->item(0);
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

/*Problem starts here... program crashes when calling getNodeValue()
* tried also char* as type for text. Shouldn't it assign the value
* 'hallo' to the string text ??
*/

if (node){
text = XMLString::transcode(node->getNodeValue());
//program crashes here....!!!!!!!!!!!!!!!
}
else {
// node ist not null, thus this is not executed..
cout << "Failure: maybe node is empty" << endl;
}
cout << text << endl;

//this does also not work
//cout << XMLString::transcode(list->item(0)->getNodeValue()) << endl;

//this again works, when commenting out the code above.. and shows
//the name 'element' and that there is one element in the nodelist...
cout << XMLString::transcode(list->item(0)->getNodeName()) << endl;
cout << list->getLength() << endl;

cout << "Exiting normally" << endl;
return 0;

}


########## test.xml ########################################

<?xml version="1.0"?>

<test xmlns="http://www.boris-glawe.de/";>
<element>
hallo
</element>
</test>







--
Stop TCPA and Palladium !!!

watch these sites if you want to learn more:

english:
http://www.cl.cam.ac.uk/~rja14/tcpa-faq.html

german:
http://moon.hipjoint.de/tcpa-palladium-faq-de.html


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]





--
Stop TCPA and Palladium !!!

watch these sites if you want to learn more:

english:
http://www.cl.cam.ac.uk/~rja14/tcpa-faq.html

german:
http://moon.hipjoint.de/tcpa-palladium-faq-de.html



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to