Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Dom4j change XML coding     - Under Linux using Magent + Memcached cache server cluster deployment (Server)

- Android Service service applications and the phone SMS Listener Listener (Programming)

- Linux and Unix systems really do network more secure (Linux)

- Oracle set the timer task JOB scheduler to execute stored procedures or PL / SQL code block (Database)

- Summary Linux operating system some tips to prevent attacks (Linux)

- Java memory analysis tool uses detailed MAT (Programming)

- Java look and feel mode (Facade mode) (Programming)

- OpenSSH version smooth upgrade method (Linux)

- Shell Script: Bulk add users, and set the random character password (Programming)

- SSH service configuration to use a certificate login Linux server (Server)

- Linux GCC 5.1.0 compiler installation (Linux)

- Red Hat Enterprise Linux 6.4 Configuring VNC Remote Desktop login access (Linux)

- sudoers file parsing (Linux)

- Kubuntu 14.04 desktop to the user how to upgrade KDE 4.13.2 (Linux)

- Ubuntu: To install chat client Chatty 0.6.1 (Linux)

- MongoDB version 3.2 WiredTiger storage engine performance tests (Database)

- C ++ handling text input (Programming)

- The most concise explanation of JavaScript closures (Programming)

- SQLite3 simple operation (Database)

- Ubuntu 14.10 / 14.04 how to install Quick Start tool Mutate 2.2 (Linux)

  Dom4j change XML coding
  Add Date : 2018-11-21      
  Use "document.setXMLEncoding" such settings file is still generated xml utf-8 encoding.

OutputFormat need to set the output file encoding format.

Element rootElement = document.addElement ( "data");
document.setXMLEncoding ( "GBK"); // default utf-8

public static void writeXMLFile (Document document, File file, String Encoding) {try {OutputFormat format = OutputFormat.createPrettyPrint (); // output do not want to beautify the landscaping can use the new OutputFormat (); format.setEncoding (Encoding.toUpperCase ()); OutputStream out = new FileOutputStream (file); XMLWriter writer = new XMLWriter (out, format); writer.write (document); writer.close ();} catch (IOException e) {e.printStackTrace ();}

Use "OutputFormat", you can set the output file xml encoded and xml file will also change at the statement.

Quote others answer: explain "document.setXMLEncoding" and "format.setEncoding" set difference coding

public class TestXML {@Test public void test () throws IOException {Document doc = new DefaultDocument (); doc.addElement ( "root"); // print out here is the default utf-8 System.out.println (doc. asXML ()); doc.setXMLEncoding ( "utf-16"); // print out here is modified utf-16 System.out.println (doc.asXML ()); // there is no set default encoding format saved is utf-8, look at the source code to know dom4j saveXML (doc, "D: \ temp \ test \ test1.xml", null); // set here so save after encoding format is big5 saveXML (doc, ! "D: \ temp \ test \ test2.xml", "big5");} private void saveXML (Document doc, String filePath, String encode) throws IOException {OutputFormat format = new OutputFormat (); if (null = encode) {format.setEncoding (encode.toUpperCase ());} XMLWriter xmlWriter = new XMLWriter (new FileOutputStream (filePath), format); xmlWriter.write (doc); xmlWriter.flush (); xmlWriter.close ();}}

Finally this:

XMLWriter can pass OutputStream or Writer
XMLWriter writer = new XMLWriter (OutputStream, OutputFormat);
XMLWriter writer = new XMLWriter (Writer, OutputFormat);
Initially tried to pass a new FileWriter (file), as follows
try {
XMLWriter writer = new XMLWriter (new FileWriter (f), format);
writer.write (document);
writer.close ();
result = fileName;
} Catch (IOException e) {// TODO Auto-generated catch block
e.printStackTrace ();
But the result is not right. Modified as follows, the results are correct.

try {
OutputFormat format = OutputFormat.createPrettyPrint ();
XMLWriter xmlWriter = new XMLWriter (new FileOutputStream (f), format);
xmlWriter.write (document);
xmlWriter.flush ();
xmlWriter.close ();
result = fileName;
} Catch (IOException e) {// TODO Auto-generated catch block
e.printStackTrace ();
LOG.error ( "trans for XML error:", e);
- libreadline.so.6: can not open shared object file problem solution (Linux)
- Comparison of sorting algorithms (Programming)
- Docker deployment practices in Ubuntu (Server)
- Top command: the Task Manager under linux (Linux)
- TPCC-MySQL Benchmark (Database)
- Basic Tutorial: Linux novice should know 26 commands (Linux)
- Ubuntu 12.04 / 14.04 users to install software LyX document processing (Linux)
- A new method for Linux hidden files (Linux)
- Linux Network Programming - raw socket instance: MAC Address Scanner (Programming)
- LVM management parameters commonly used commands explained in detail (Linux)
- Docker build private warehouse (Server)
- Gentoo: existing preserved libs problem solving (Linux)
- Element content of Java HashSet change issues (Programming)
- SQLite3 simple operation (Database)
- Installation and Configuration rsync server under CentOS 6.3 (Server)
- Java proxy mode (Programming)
- Linux Crontab Timing task command Detailed (Linux)
- Ubuntu 14.04 LTS to compile the source code Android4.4.2 (Linux)
- Linux batch copy data script (Linux)
- CentOS 7 hard disk expansion toss record (Linux)
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.