You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by Dan Bennett <da...@interati.co.uk> on 2000/06/26 19:04:39 UTC

Expanded entities in XMLSerializer

Quick question:

If I have an XML file thus:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE test SYSTEM "test.dtd">
<test>&deg;This is a sample document.</test>

with a DTD thus:

<!--test dtd -->
<!ENTITY deg    "&#176;" ><!--=degree sign-->
<!ELEMENT	test			(#PCDATA)>

and use the following Java to read and write the XML:

DOMParser parser = new DOMParser();
parser.parse(new InputSource(new FileInputStream("test.xml")));
Document doc = parser.getDocument();
XMLSerializer serializer = new XMLSerializer();
serializer.setOutputCharStream(new StringWriter());
serializer.asDOMSerializer();
serializer.serialize(doc);
System.out.println(writer.toString());

I get output with the 'deg' entity converted to &#176;. Rightly or wrongly,
this isn't what I want, so, my question is, how do I stop entity expansion?
(This is, I believe, definitely something on XMLSerializer because if I use
the sample DOMWriter it works correctly.

My apologies if I am being stupid.

Thanks

Dan

Dan Bennett
www.interati.co.uk


Re: Expanded entities in XMLSerializer

Posted by Andy Clark <an...@apache.org>.
Dan Bennett wrote:
> I get output with the 'deg' entity converted to &#176;. Rightly or 
> wrongly, this isn't what I want, so, my question is, how do I stop 
> entity expansion? (This is, I believe, definitely something on 
> XMLSerializer because if I use the sample DOMWriter it works 
> correctly.

This is definitely a problem with the XMLSerializer. This class
extends BaseMarkupSerializer that only prints entity reference
node contents, not the entity reference node. I'm not exactly
sure why this isn't an option but I have a pretty good guess.

When you serialize a document, you want to be able to read the
document back in again without loss of data. In order to print
the entity ref nodes, then you would need to also print a DTD
that contains entity declarations. Without those an XML parser
would not be able to read something like "&deg;".

-- 
Andy Clark * IBM, JTC - Silicon Valley * andyc@apache.org