欢迎光临
我们一直在努力

这里转贴一个使用java在处理xml时遇到中文问题的解决方法-JSP教程,Java与XML

建站超值云服务器,限时71元/月

------------
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());
////        }
////    }
}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 这里转贴一个使用java在处理xml时遇到中文问题的解决方法-JSP教程,Java与XML
分享到: 更多 (0)

相关推荐

  • 暂无文章