java源码中xml是什么,这里转贴一个使用java在处理xml时遇到中

该博客介绍了在Java中处理XML文件的三种不同方法,包括仅使用JDK,引入Xerces.jar和使用jdom.jar。通过示例代码展示了如何创建和保存XML文档,并特别关注了中文字符集的正确处理,如设置为GBK编码。
摘要由CSDN通过智能技术生成

------------

author:wait4friend

------------

共有三种方法,分别使用了JDK, xerces.jar或jdom.jar。

直接贴出原码:

http://www.gaodaima.com/41836.html这里转贴一个使用java在处理xml时遇到中

/**

*  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());

        }

    }

}

欢迎大家阅读《这里转贴一个使用java在处理xml时遇到中》,跪求各位点评,若觉得好的话请收藏本文,by 搞代码

e7ce419cf2d6ad34d01da2ceb8829eed.png

微信 赏一包辣条吧~

023a57327877fb4402bcc76911ec18ea.png

支付宝 赏一听可乐吧~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值