Discovery of format settings when using dom4j to implement XML file output

Source: Internet
Author: User
Tags compact
I encountered a problem when I configured the report in Xiaoshan school yesterday. I asked the output XML file to keep my original content (the content contains many spaces ), however, dom4j automatically removes these spaces when outputting files. The following findings have been found:
When we use dom4j to process XML file output, we may encounter the following problems: we require the text in each element to retain the original information I have written, for example, spaces cannot be removed;
For example, we want to output the content in the XML file as follows:
<? XML version = "1.0" encoding = "gb2312"?>
<Root>
<Author name = "James" location = "UK"> James Strachan </author>
<Author name = "Bob" location = "us"> China Bob McWhirter </author>
</Root>
Note that the content in author contains many spaces;
Assume that we have used the following methods to write the above document:
Public document createdocument (){
Document document = incluenthelper. createdocument ();
Element root = Document. addelement ("root ");
Element author1 = root. addelement ("author ")
. Addattribute ("name", "James ")
. Addattribute ("location", "UK ")
. Addtext ("James Strachan ");
Element author2 = root. addelement ("author ")
. Addattribute ("name", "Bob ")
. Addattribute ("location", "us ")
. Addtext ("China Bob McWhirter ");
Return document;
}
In dom4j, there are two ways to write document directly or any node into an XML file:
1. This is also the simplest method: directly output through the write method, as shown below:
Filewriter fw = new filewriter ("test. xml ");
Document. Write (FW );
At this time, the output XML file is the default UTF-8 encoding, no format, space is not removed, is actually a string; its output is as follows:
<? XML version = "1.0" encoding = "UTF-8"?>
<Root> <author name = "James" location = "UK"> James Strachan </author> <author name = "Bob" location = "us"> China Bob McWhirter </ author> </root>
2. Use the write method in the xmlwriter class. You can set the output format by yourself, for example, compact and compact:
Outputformat format = outputformat. createprettyprint (); // reduced format
// Outputformat format = outputformat. createcompactformat (); // compact format
Format. setencoding ("gb2312"); // sets the Encoding
// Format. settrimtext (false); // you can specify whether to delete unnecessary spaces in text.
Xmlwriter XW = new xmlwriter (FW, format );
XW. Write (DOM. createdocument ());
In this case, the output XML file is encoded as gb2312, in reduced format, but the extra spaces have been cleared:
<? XML version = "1.0" encoding = "gb2312"?>

<Root>
<Author name = "James" location = "UK"> James Strachan </author>
<Author name = "Bob" location = "us"> China Bob McWhirter </author>
</Root> if you want to set the output format of an XML file, you must use the xmlwriter class, but we need to keep the spaces in it. In this case, you need to set the format, that is, add the format. settrimtext (false );
In this way, both the output format of the XML file and the space can be kept. The output is as follows:
<? XML version = "1.0" encoding = "gb2312"?>

<Root>
<Author name = "James" location = "UK"> James Strachan </author>
<Author name = "Bob" location = "us"> China Bob McWhirter </author>
</Root> PS: If the attribute value in element contains spaces, spaces are not removed under any circumstances;

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.