xerces-c-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David N Bertoni/Cambridge/IBM <david_n_bert...@us.ibm.com>
Subject Re: getNodeValue() crashes
Date Tue, 04 Feb 2003 17:47:37 GMT




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


Mime
View raw message