Here's a way to find a solution to Chinese problems when working with XML in Java

Source: Internet
Author: User
Tags getmessage
xml| Solution | problem | Chinese------------
Author:wait4friend
------------

There are three ways to use JDK, Xerces.jar or Jdom.jar, respectively.
Paste the original code directly:

/**
* Indicate to save a XML file, resolving the problem
* About CharacterSet, I mean GB2312 here can is 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 ...
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 ("Teacher");
Org.w3c.dom.Element Wang = doc.createelement ("King");
Wang.appendchild (Doc.createtextnode ("I am Mr Wang"));
Root.appendchild (Wang);
Doc.appendchild (root);

Code to save goes ...
Javax.xml.transform.Transformer Transformer =
Javax.xml.transform.TransformerFactory.newInstance (). Newtransformer ();
Notice this 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 the 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 a 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 ...
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 a 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 ...
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 ());
////        }
////    }
}

Related Article

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.