C++ SAX Parser ---handling special characters

Discussion in 'XML' started by mamatha.kumar@gmail.com, Apr 17, 2007.

  1. Guest

    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.
    , Apr 17, 2007
    #1
    1. Advertising

  2. wrote:

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




    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Apr 17, 2007
    #2
    1. Advertising

  3. 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.

    --
    Joe Kesselman / Beware the fury of a patient man. -- John Dryden
    Joseph Kesselman, Apr 17, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. leo
    Replies:
    1
    Views:
    625
    Adam Maass
    Apr 13, 2005
  2. Stefan Mueller
    Replies:
    3
    Views:
    32,953
    Stefan Mueller
    Jul 23, 2006
  3. Replies:
    2
    Views:
    1,070
    Ingo Menger
    May 31, 2007
  4. rvino
    Replies:
    0
    Views:
    4,635
    rvino
    Aug 14, 2007
  5. shivaraj
    Replies:
    3
    Views:
    1,324
    shivaraj
    Jul 7, 2008
Loading...

Share This Page