这里转贴一个使用java在处理xml时遇到中文问题的解决方法

80酷酷网    80kuku.com

  xml|解决|问题|中文------------
author:wait4friend
------------

共有三种方法,分别使用了JDK, xerces.jar或jdom.jar。
直接贴出原码:

/**
*  Use this program to indicate how to save a XML file, resolving the problem
*  about CharacterSet, I mean GB2312 here can be dealt with correctly
*
* author Michael Zeng
*/
package classes;

import java.io.*;

public class DOMTest
{
    private String inFile = "E:/About XML/Java_XML/XmlData/mapping.xml";
    private String outFile = "E:/About XML/Java_XML/XmlData/my.xml";
    
    public static void main(String args[])
    {
        new DOMTest();
    }

    //Approach 1:    only use the JDK 1.4
    //In this case, I handle the Chinese correctly with the TransFormer.setOutputProperty()
    //These packages are necessary:
    //    org.w3c.dom
    //    javax.xml.parsers
    //    javax.xml.transform
    //    javax.xml.transform.dom
    //    javax.xml.transform.stream
    public DOMTest()
    {
        try
        {    
            //code to create a new DOM document goes here...
            javax.xml.parsers.DocumentBuilder builder =
                javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder();
            org.w3c.dom.Document doc = builder.newDocument();
            
            //Add some elements here...
            org.w3c.dom.Element root = doc.createElement("老师");
            org.w3c.dom.Element wang = doc.createElement("王");
            wang.appendChild(doc.createTextNode("我是王老师"));
            root.appendChild(wang);
            doc.appendChild(root);
            
            //code to save goes here...
            javax.xml.transform.Transformer transformer =
                javax.xml.transform.TransformerFactory.newInstance().newTransformer();
            //Notice this first sentence below, which resolves the problem of Chinese
            transformer.setOutputProperty(javax.xml.transform.OutputKeys.ENCODING, "gb2312");
            transformer.setOutputProperty(javax.xml.transform.OutputKeys.INDENT, "yes");
            
            transformer.transform(new javax.xml.transform.dom.DOMSource(doc),
                                    new javax.xml.transform.stream.StreamResult(outFile));
        }
        catch (Exception e)
        {
            System.out.println (e.getMessage());
        }
    }
    
    //Approach 2:    use Xerces additionally. The xerces.jar must have been in
    //                your CLASSPATH
    //In this case, Chinese characters can be handled successfully.
    //These packages are necessary:
    //    org.w3c.dom
    //    org.apache.xerces.parsers
    //    org.apache.xml.serialize
////    public DOMTest()
////    {
////        try
////        {
////            //code to parse an existed XML file goes here...
////            org.apache.xerces.parsers.DOMParser parser =
////                            new org.apache.xerces.parsers.DOMParser();
////            parser.parse(inFile);
////            org.w3c.dom.Document.doc = parser.getDocument();
////            
////            //code to save goes here...
////            FileWriter writer = new FileWriter(outFile);
////            //Pay attention to the OutputFormat constructor, which set the GB2312
////            org.apache.xml.serialize.OutputFormat outputFormat =
////                            new org.apache.xml.serialize.OutputFormat(doc, "GB2312", true);
////            
////            org.apache.xml.serialize.XMLSerializer serializer =
////                            new org.apache.xml.serialize.XMLSerializer(writer, outputFormat);
////            serializer.serialize(doc);
////            writer.close();
////        }
////        catch (Exception e)
////        {
////            System.out.println (e.getMessage());
////        }
////    }

    //Approach 3:     use the JDOM package, and this is the simplest one. Additionally,
    //                the jdom.jar must have been in your CLASSPATH
    //These package are necessary:
    //    org.jdom
    //    org.jdom.input
    //    org.jdom.output
////    public DOMTest()
////    {
////        try
////        {
////            //code to parse an existed XML file goes here...
////            org.jdom.input.SAXBuilder builder = new org.jdom.input.SAXBuilder();
////            org.jdom.Document doc = builder.build(inFile);
////            
////            //code to save goes here...
////            FileWriter writer = new FileWriter(outFile);
////            
////            org.jdom.output.XMLOutputter outputter =
////                    new org.jdom.output.XMLOutputter("  ", true, "GB2312");
////            outputter.output(doc, writer);
////            writer.close();
////        }
////        catch (Exception e)
////        {
////            System.out.println (e.getMessage());
////        }
////    }
}



分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: