You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xalan.apache.org by Peter McEvoy <pe...@iona.com> on 2000/10/26 13:11:08 UTC
Entities and outputting XML
I've been struggling the last few days with something that seems to me to be
quite a basic, but I'm finding REALLY frustrating.
If this is not the correct forum for this issue (I could not find a
xalan-users maillist) then let me know politely and I will "go bug them".
Anyway, I have been writing Java classes that use xalan (1.2) and xerces
(1.2-0) classes to parse and manipulate XML. The classes are within a
servlet application, and these in turn generate XML fragments. In
particluar, I have a master XML file that is read in by a servlet, and the
servlet can then answer xpath queries sent to it over http. The servlet
needs to return XML.
So anyway, I have successfully mananged to get the servlet reading the XML
(which contains lots of & ' "e; and even one or two ® .
So this is fine - the XML is clean going in and is parsed nicely into a DOM
managed within the servlet. I then perform an XPath query using the
XPathAPI helper class that is in the samples directory, and I return the
result (ala ApplyXpath sample). However, I find that the XML returned is
munged: my clean XML has now been converted into unclean with ' ("),
(r) (®) and non-breaking spaces ( ) in it. It was OK in the DOM,
but the entities don't get re-coded in output.
So I take a look at the XpathAPI code and see that it uses a FormatterToXML
class, which in turn tries to do some escaping - but not for all entities -
in fact even the ApplyXpath example returns XML that contains ' characters.
So the comments say that FormatterToXML is replaced by xerces xml serialiser
class - but I found I had similar problems with that and had ended up
dropping it in favour of my own ineffcient DOM walker that outputs the XML
and scans does search and replace on each character on the way out. Talk
about slow (but at least it's clean XML).
So I would like to know:
how can I effeciently output a DOM as XML that is clean and suitable for
further parsing by another process? Any code examples (surely this can be
done in two or three lines of code?)
once again, if this is the wrong forum for this, then please point me in the
right direction and I will move away...
Peter McEvoy
Senior Technical Analyst
IONA Technologies PLC