C++ SAX Parser ---handling special characters

M

mamatha.kumar

Hi

My xml is as follows

<?xml version="1.0" encoding="UTF-8"?>
<StreetName>A &amp; B</StreetName>

But when I parse the xml using the characters fn of C++ SAX Xerces
parser(OS: solaris) , written below

characters( const XMLCh* const chars, const unsigned int leng
th)
{
string value = XMLString::transcode(chars);

if (currentElement=="")
return;

if (currentElement=="StreetName")
cout << "The StreetName is " << value << endl;

}

The output is
The StreetName is A
The StreetName is & B

Why is this happening instead of getting the StreetName as A & B.
What is the mistake am making ?
Any help is appreciated.

Thanks
Kumar.
 
M

Martin Honnen

The output is
The StreetName is A
The StreetName is & B

Why is this happening instead of getting the StreetName as A & B.
What is the mistake am making ?

You seem to expect that the parser pushes the full contents of an
element into your characters handler but it is not required to do so, it
can break up character data into chunks and call the characters handler
several times. So your SAX client application needs to take care of that
and store the character data somewhere and concatenate if you need that.
 
J

Joseph Kesselman

For parser efficiency reasons, SAX does not promise to deliver
contiguous text as a single characters() events. If you need it all in
one string, it's your responsibility as a SAX handler author to do your
own buffering, and defer processing until the next non-characters()
event comes in (indicating end of text).

A good SAX tutorial really should have explained this; it's the single
most common beginner mistake in SAX.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top