[jdom-interest] setting the encoding after a transformation

Altner, Bruce (HQ-LD070)[SAICIS/CDI] bruce.altner-1 at nasa.gov
Sat Jun 18 06:10:42 PDT 2005


Greetings:

 

I am trying to control the encoding of a transformed xml document but it
stubbornly refuses to change (it always comes back as UTF-8, which is
the JDOM default).  

 

Here is a snippet of code:

 

    private void transform(Transformer transformer,

                           Document xmlDoc, String outFile) {

      //uses a JDOM Document as input (supplemental data added)

      

      try {

          DOMSource ds = new DOMSource(new
DOMOutputter().output(xmlDoc));

        

 
transformer.setOutputProperty(OutputKeys.ENCODING,"iso-8859-1");

          transformer.transform(ds,new StreamResult(new File(outFile)));

 

The input xmlDoc is an existing JDOM document and the output outFile is
the file I want to have a different encoding.

 

The only way I know of to control this with JDOM is with XMLOutputter,
so I tried that too, as follows:

 

    private void transform(Transformer transformer,

                           Document xmlDoc, String outFile) {

      //uses a JDOM Document as input (supplemental data added)

      

      try {

          DOMSource ds = new DOMSource(new
DOMOutputter().output(xmlDoc));

          DOMResult dr = new DOMResult();

 
transformer.setOutputProperty(OutputKeys.ENCODING,"iso-8859-1");

          transformer.transform(ds,dr);

 

          //sets encoding of in a somewhat screwy way

          //because in JDOM this can only be done through an
XMLOutputter

        

          Node node = dr.getNode();

          Document newdoc = (new
DOMBuilder()).build(node.getOwnerDocument());

         

          XMLOutputter out = new XMLOutputter();

          out.setEncoding("iso-8859-1");

          out.output(newdoc,new FileWriter(new File(outFile)));

 

After much contortion to get outFile via XMLOutputter I was surprised
that it still had an encoding of UTF-8.

 

Any suggestions for forcing it to accept the desired encoding? Note:
This still uses Jdom 0.8 as I haven't converted the rest of the code to
use 1.0 yet. Is the new Format object the answer to this problem?

 

Thanks, in advance,

Bruce

 

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.jdom.org/pipermail/jdom-interest/attachments/20050618/9715650d/attachment.htm


More information about the jdom-interest mailing list