Jdom output UTF-8 XML perfect solution

Source: Internet
Author: User

Paste Address: http://java.chinaitlab.com/advance/755393.html

Phenomenon Description: Jdom output XML file, when using character encoding GBK normal, and output UTF-8 garbled.

The perfect solution starts with the rumor:
1 Jdom Whether the UTF-8 file is generated is independent of format settings, only the output of other character encoding needs to be set, see the following comment.
2) Jdom output UTF-8 file garbled the root cause is not in Jdomapi, but in the JDK.

    Description:
        jdom Output class Xmloutputter has two outputs interface, In addition to having one document parameter, the writer and outputstream parameters are accepted respectively.
        This gives us an illusion that two interfaces can be used arbitrarily.
        First we use output (doc,system.out) to do the test, at this time get garbled,
           then we have to test output (doc,new printwriter (System.out)), the outputs are not garbled,
           in other words, be sure to use a writer interface to wrap the console.
        then we use output (doc,new FileWriter (path)) to do the test, the result is garbled,
           then we test it as output (doc,new fileoutputstream (path)), which is not garbled,
          in other words, you must use a OutputStream interface to package the output file.
        crazy. Oh, very funny is it. After the source code to Jdom debugging, found no problem, the problem is in the JDK.

Corresponding interface processing within the JDK:
1 The PrintWriter class has a outputstream construction method, so it can be packaged from System.out to PrintWriter
2 The FileWriter class does not have a outputstream construction method, so it cannot be wrapped from FileOutputStream to FileWriter
3 If the PrintWriter class uses the writer construction method (writer implementation is FileWriter), the final output is garbled
4 If you use a FileOutputStream to package a console output, is also garbled
Therefore, for the JDK within the various output system, a variety of inputstream, OutputStream, reader and writer to fully understand, otherwise very easy to appear some unexpected problems.

Jdom version of the test: 1.0, 1.1
Test code:

Import Java.io.File; Import Java.io.FileOutputStream; Import Java.io.FileWriter; Import Java.io.PrintWriter; Import Java.util.HashMap; Import org.jdom.Document; Import org.jdom.Element; Import Org.jdom.output.Format; Import Org.jdom.output.XMLOutputter; public class buildXML {public static void main (string[] args) throws exception{file Xmlfile=new file ("c://edittemp//xml/ /abc.xml "); Chinese problem//GBK is no problem, but UTF-8 is the problem//reason://1) for disk files, you must use the output stream FileOutputStream//FileWriter out=new FileWriter (xmlfile); will result in garbled//2) for the console output, you must use PrintWriter, if the direct use of System.out will also appear garbled//PrintWriter out=new PrintWriter (System.out); FileOutputStream out=new FileOutputStream (xmlfile); Element eroot=new Element ("root"); Eroot.addcontent ((New Element ("Code")). Addcontent ("Codes"); Eroot.addcontent ((New Element ("DS")). Addcontent ("data source"); Eroot.addcontent ((New Element ("SQL")). Addcontent ("Retrieving SQL"); Eroot.addcontent ((New Element ("Order")). Addcontent ("sort")); Document Doc=new document (Eroot); Xmloutputter outputter = new XmlouTputter (); If format is not set, the XML is utf-8 only without indentation, so format is not necessary for format F = Format.getprettyformat (); F.setencoding ("UTF-8");//default=utf-8 Outputter.setformat (f); Outputter.output (Doc, out); Out.close (); } }

An additional method that will jdom the document object, according to the specified encoded output string:

/** * This method returns the Jdom document object based on the specified encoded conversion string. * @param xmldoc The Jdom object to be transformed * @param the encoding used for the encoding output string * @return String document processed generated String * @throws ioexception * * * Publ IC static string ToXML (Document xmldoc, string encoding) throws IOException {Bytearrayoutputstream byterep = new Bytearra Youtputstream (); PrintWriter out=new PrintWriter (BYTEREP); Format format = Format.getprettyformat (); format.setencoding (encoding); Xmloutputter docwriter = new Xmloutputter (format); try {docwriter.output (xmldoc, out); \ catch (Exception e) {} return byterep.tostring (); }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.