You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Alexander Broekhuis <a....@admiraalkruys.nl> on 2004/06/29 12:03:07 UTC
Saxparser and DefaultHandler decoding special characters
Hi,
I have a SAXParser and a Handler to parse XML files. These XML files can
contain special characters like &, < etc.
If i parse these XML files the special characters show up decoded, &
instead of &. How can i make my parser show the encoded characters?
TiA
Alexander
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org
Re: Saxparser and DefaultHandler decoding special characters
Posted by Andy Clark <an...@cyberneko.net>.
Elliotte Rusty Harold wrote:
> At 12:03 PM +0200 6/29/04, Alexander Broekhuis wrote:
>> I have a SAXParser and a Handler to parse XML files. These XML files can
>> contain special characters like &, < etc.
>> If i parse these XML files the special characters show up decoded, &
>> instead of &. How can i make my parser show the encoded characters?
>
> You can't and you shouldn't. Part of the job of the parser is to decode
> these.
You can but you shouldn't.
Most of the times, this is a serialization issue (i.e. how your
application writes the data once it reads it). So, as Elliotte
says, you normally don't need this. And if it's truly the data
that is in error, then you should fix the source of the data.
However... if you don't have control over the generation of the
data and need your application to see the special characters in
their escaped form (e.g. "&" instead of "&"), there are ways
around it using Xerces.
Please refer to the Features page in the documentation. There
are two features of particular importance to you:
http://apache.org/xml/features/scanner/notify-char-refs
http://apache.org/xml/features/scanner/notify-builtin-refs
Setting these features to true will make Xerces report the
start and end boundaries of character and built-in entities
(e.g.   and &). In SAX, this equates to the start/
endEntity callbacks in the LexicalHandler interface. You can
also access them in XNI with the start/endGeneralEntity
callbacks in the XMLDocumentHandler interface. In the latter,
you can simply extend the parser of your choice and override
these methods.
Please note, however, that you should capture all of the
entity boundary callbacks for the special characters you
are interested in. In other words, don't let these callbacks
propagate down your application pipeline and corrupt the
rest of the application.
But, like I said before, you should attempt to fix the error
at the source. If you can't, then this is an alternative for
you. Good luck and I hope this helps!
--
Andy Clark * andyc@apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org
Re: Saxparser and DefaultHandler decoding special characters
Posted by Elliotte Rusty Harold <el...@metalab.unc.edu>.
At 2:03 PM +0200 6/29/04, Alexander Broekhuis wrote:
>And what if that is what I want? Is there some other way to do this?
>
I can't tell you you don't want that. Obviously you're the best judge
of what you want or don't want. I can tell you you don't need that. I
discussed this extensively in Effective XML. In particular see Items
9, 14, 29 and especially Item 15:
http://www.cafeconleche.org/books/effectivexml/chapters/15.html
--
Elliotte Rusty Harold
elharo@metalab.unc.edu
Effective XML (Addison-Wesley, 2003)
http://www.cafeconleche.org/books/effectivexml
http://www.amazon.com/exec/obidos/ISBN%3D0321150406/ref%3Dnosim/cafeaulaitA
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org
Re: Saxparser and DefaultHandler decoding special characters
Posted by Alexander Broekhuis <a....@admiraalkruys.nl>.
And what if that is what I want? Is there some other way to do this?
TiA,
Alexander
Elliotte Rusty Harold said:
> At 12:03 PM +0200 6/29/04, Alexander Broekhuis wrote:
>>Hi,
>>
>>I have a SAXParser and a Handler to parse XML files. These XML files can
>>contain special characters like &, < etc.
>>If i parse these XML files the special characters show up decoded, &
>>instead of &. How can i make my parser show the encoded characters?
>
> You can't and you shouldn't. Part of the job of the parser is to decode
> these.
> --
>
> Elliotte Rusty Harold
> elharo@metalab.unc.edu
> Effective XML (Addison-Wesley, 2003)
> http://www.cafeconleche.org/books/effectivexml
> http://www.amazon.com/exec/obidos/ISBN%3D0321150406/ref%3Dnosim/cafeaulaitA
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-user-help@xml.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org
Re: Saxparser and DefaultHandler decoding special characters
Posted by Elliotte Rusty Harold <el...@metalab.unc.edu>.
At 12:03 PM +0200 6/29/04, Alexander Broekhuis wrote:
>Hi,
>
>I have a SAXParser and a Handler to parse XML files. These XML files can
>contain special characters like &, < etc.
>If i parse these XML files the special characters show up decoded, &
>instead of &. How can i make my parser show the encoded characters?
You can't and you shouldn't. Part of the job of the parser is to decode these.
--
Elliotte Rusty Harold
elharo@metalab.unc.edu
Effective XML (Addison-Wesley, 2003)
http://www.cafeconleche.org/books/effectivexml
http://www.amazon.com/exec/obidos/ISBN%3D0321150406/ref%3Dnosim/cafeaulaitA
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org