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 Da...@ptb.de on 2008/09/02 13:14:30 UTC

Problem with not existing Node

Hello I have the following Problem:

xerces shut down my programm when it wants to output an non existing node, 
here an exmple.
I want to read out the text of the elements.

<xml>
        <element_a >test</element_a>
        <element_b />test</element_b>
</xml>

The source code:

DOMNodeList* nodes = 
root->getElementsByTagName(XMLString:transcode("elementa"));

  cout << nodes->item(0)->getTextContent() << endl;

This code makes no Problem while i ask for elemts which are in the xmlfile 
below, but if i ask for element_c which is not in the xml file the code 
before exit my programm  by reaching the code line 
 nodes->item(0)->getTextContent().

I have also tried to get out if the pointer is empty or there is no nodes 
with nodes->getlength() , but also here xerces is closing down my programm 
without an error message.

If someone had an idea where the problem ist or how i can solve the 
problem please write it to me

Thanks David 

P.S I hope you understand my problem

Re: Problem with not existing Node

Posted by Jaya Nageswar <ja...@gmail.com>.
Hi David,

Thanks for the update. I translated the characters from UCS-2 to UTF-8 using
C APIs. Actually i took these chinese characters(您是如) from Goolge Translate
and used in xml file to test the unicode support.When i translated these
characters from UCS-2 to UTF-8 using C APIs, i got these characters(귦꺡髧„).
Now i am not getting the errors from xerces parser.

But i have a question. Will the characters themselves change from one format
to another format? If i have a string "abcd", will it change from one format
to another format? I understand the encoding in different formats is
different but i do not understand why the characters themselves are chaning
from one format to another format. Any information related to this will be a
great help to me.

Thanks,
Jaya Nageswar.

On Wed, Sep 3, 2008 at 12:43 PM, <Da...@ptb.de> wrote:

> Hello  Jesse Pelton, thank you for the hint with XMLString::transcode() .
>
> I found the Error, a few lines before I destroyed the root element and a
> pointer to an destroyed pointer is always evil. ( I think that was an
> normal noob error).
>
> I hope the next time I find the error  earlier and i don't get on your
> nerves again.
>
>
> Thank you David
>
>
> "Jesse Pelton" <js...@PKC.com> schrieb am 02.09.2008 14:28:46:
>
> > The documentation seems to imply that you'll always get a node list back
> > from getElementsByTagName(), but it might be worth checking whether your
> > nodes variable is null before dereferencing it.  That's basic defensive
> > programming.
> >
> > Assuming nodes points to a valid node list, you don't need to check the
> > list's length.  Just fetch nodes->item(0) and check to see whether the
> > result is null before dereferencing it.
> >
> > Note that your snippet has a memory leak.  XMLString::transcode()
> > returns a pointer to allocated memory; you need to release that memory.
> >
> > -----Original Message-----
> > From: David.Sander@ptb.de [mailto:David.Sander@ptb.de]
> > Sent: Tuesday, September 02, 2008 7:15 AM
> > To: c-users@xerces.apache.org
> > Subject: Problem with not existing Node
> >
> > Hello I have the following Problem:
> >
> > xerces shut down my programm when it wants to output an non existing
> > node,
> > here an exmple.
> > I want to read out the text of the elements.
> >
> > <xml>
> >         <element_a >test</element_a>
> >         <element_b />test</element_b>
> > </xml>
> >
> > The source code:
> >
> > DOMNodeList* nodes =
> > root->getElementsByTagName(XMLString:transcode("elementa"));
> >
> >   cout << nodes->item(0)->getTextContent() << endl;
> >
> > This code makes no Problem while i ask for elemts which are in the
> > xmlfile
> > below, but if i ask for element_c which is not in the xml file the code
> > before exit my programm  by reaching the code line
> >  nodes->item(0)->getTextContent().
> >
> > I have also tried to get out if the pointer is empty or there is no
> > nodes
> > with nodes->getlength() , but also here xerces is closing down my
> > programm
> > without an error message.
> >
> > If someone had an idea where the problem ist or how i can solve the
> > problem please write it to me
> >
> > Thanks David
> >
> > P.S I hope you understand my problem
>

RE: Problem with not existing Node

Posted by Da...@ptb.de.
Hello  Jesse Pelton, thank you for the hint with XMLString::transcode() .

I found the Error, a few lines before I destroyed the root element and a 
pointer to an destroyed pointer is always evil. ( I think that was an 
normal noob error).

I hope the next time I find the error  earlier and i don't get on your 
nerves again.


Thank you David


"Jesse Pelton" <js...@PKC.com> schrieb am 02.09.2008 14:28:46:

> The documentation seems to imply that you'll always get a node list back
> from getElementsByTagName(), but it might be worth checking whether your
> nodes variable is null before dereferencing it.  That's basic defensive
> programming.
> 
> Assuming nodes points to a valid node list, you don't need to check the
> list's length.  Just fetch nodes->item(0) and check to see whether the
> result is null before dereferencing it.
> 
> Note that your snippet has a memory leak.  XMLString::transcode()
> returns a pointer to allocated memory; you need to release that memory.
> 
> -----Original Message-----
> From: David.Sander@ptb.de [mailto:David.Sander@ptb.de] 
> Sent: Tuesday, September 02, 2008 7:15 AM
> To: c-users@xerces.apache.org
> Subject: Problem with not existing Node
> 
> Hello I have the following Problem:
> 
> xerces shut down my programm when it wants to output an non existing
> node, 
> here an exmple.
> I want to read out the text of the elements.
> 
> <xml>
>         <element_a >test</element_a>
>         <element_b />test</element_b>
> </xml>
> 
> The source code:
> 
> DOMNodeList* nodes = 
> root->getElementsByTagName(XMLString:transcode("elementa"));
> 
>   cout << nodes->item(0)->getTextContent() << endl;
> 
> This code makes no Problem while i ask for elemts which are in the
> xmlfile 
> below, but if i ask for element_c which is not in the xml file the code 
> before exit my programm  by reaching the code line 
>  nodes->item(0)->getTextContent().
> 
> I have also tried to get out if the pointer is empty or there is no
> nodes 
> with nodes->getlength() , but also here xerces is closing down my
> programm 
> without an error message.
> 
> If someone had an idea where the problem ist or how i can solve the 
> problem please write it to me
> 
> Thanks David 
> 
> P.S I hope you understand my problem

RE: Problem with not existing Node

Posted by Jesse Pelton <js...@PKC.com>.
The documentation seems to imply that you'll always get a node list back
from getElementsByTagName(), but it might be worth checking whether your
nodes variable is null before dereferencing it.  That's basic defensive
programming.

Assuming nodes points to a valid node list, you don't need to check the
list's length.  Just fetch nodes->item(0) and check to see whether the
result is null before dereferencing it.

Note that your snippet has a memory leak.  XMLString::transcode()
returns a pointer to allocated memory; you need to release that memory.

-----Original Message-----
From: David.Sander@ptb.de [mailto:David.Sander@ptb.de] 
Sent: Tuesday, September 02, 2008 7:15 AM
To: c-users@xerces.apache.org
Subject: Problem with not existing Node

Hello I have the following Problem:

xerces shut down my programm when it wants to output an non existing
node, 
here an exmple.
I want to read out the text of the elements.

<xml>
        <element_a >test</element_a>
        <element_b />test</element_b>
</xml>

The source code:

DOMNodeList* nodes = 
root->getElementsByTagName(XMLString:transcode("elementa"));

  cout << nodes->item(0)->getTextContent() << endl;

This code makes no Problem while i ask for elemts which are in the
xmlfile 
below, but if i ask for element_c which is not in the xml file the code 
before exit my programm  by reaching the code line 
 nodes->item(0)->getTextContent().

I have also tried to get out if the pointer is empty or there is no
nodes 
with nodes->getlength() , but also here xerces is closing down my
programm 
without an error message.

If someone had an idea where the problem ist or how i can solve the 
problem please write it to me

Thanks David 

P.S I hope you understand my problem