You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-dev@xerces.apache.org by Boris Glawe <bo...@boris-glawe.de> on 2003/02/04 18:03:29 UTC

getNodeValue() crashes

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: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org


Re: getNodeValue() crashes

Posted by David N Bertoni/Cambridge/IBM <da...@us.ibm.com>.



> 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 ??

I don't know what you mean by "assigning this value to a string," so I
can't really answer your question.  I suspect the answer is no, but all I
can really tell you is the pointer which is returned by
XMLString::transcode() is a pointer to memory which was dynamically
allocated, so you must delete it when you're finished with it.

> >   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....!??

Elements have no text value, so you get a null pointer when you call
getNodeValue()-- that's the way the API is designed to work.  Your code
crashes probably because you're passing a null pointer to
XMLString::transcode(), which you shouldn't do.

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

You can either call getChildNodes() to get a list of the children, or call
getFirstChild() to get the first child, then call getNextSibling() on each
child node, until you reach the end of the children.  Take a look at some
of the sample programs for more information and code samples.

Dave


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org


Re: getNodeValue() crashes

Posted by Boris Glawe <bo...@boris-glawe.de>.
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: xerces-c-dev-unsubscribe@xml.apache.org
>For additional commands, e-mail: xerces-c-dev-help@xml.apache.org
>
>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
>For additional commands, e-mail: xerces-c-dev-help@xml.apache.org
>
>
>
>  
>


-- 
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: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org


RE: getNodeValue() crashes

Posted by Steve Smith <st...@adacel.com>.
> From: David N Bertoni/Cambridge/IBM [mailto:david_n_bertoni@us.ibm.com]
> Hi Boris,
>
> Two things:
>
>    1. XMLString::transcode() returns a pointer to dynamic memory that you
>    must delete.  If you don't, it's a memory leak:

Alternatively, you can use the version of transcode that allows you to
supply your own string (which may be allocated on the stack and will clean
itself up).

Cheers,
Steve


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org


Re: getNodeValue() crashes

Posted by David N Bertoni/Cambridge/IBM <da...@us.ibm.com>.



Hi Boris,

Two things:

   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;

   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.

Dave



                                                                                                                                      
                      Boris Glawe                                                                                                     
                      <boris@boris-gla         To:      xerces-c-dev@xml.apache.org                                                   
                      we.de>                   cc:      (bcc: David N Bertoni/Cambridge/IBM)                                          
                                               Subject: getNodeValue() crashes                                                        
                      02/04/2003 09:03                                                                                                
                      AM                                                                                                              
                      Please respond                                                                                                  
                      to xerces-c-dev                                                                                                 
                                                                                                                                      



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: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org




---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org