The processing method of special characters of XML _java

Source: Internet
Author: User
Tags base64 parse error serialization hadoop mapreduce

The problem with this is that I try to store an object in an XML file after serializing it, and then read the string from the XML file and deserialize the object (the Hadoop mapreduce program passes jobconf to Tasktracker).

About serialization:

When I put the serialized data directly into the XML, I encountered a parse error parsing the XML because there were illegal characters. After reading the XML instructions in detail, we found that:< > ' "& is not allowed as XML pcdata. To use these characters, you must replace them with an BUILTIN entity:

Entity reference characters

< <
> >
& &
" "
' '

Therefore, the most direct way is to control themselves, write the XML file when the special character into the entity reference, in the reading, and then turn back. There is, of course, another way to use a string instead of a pcdata, rather than as a CDATA (without testing).

The method of using entity references is too cumbersome, and my application XML format cannot be defined by itself, so I have to find another way: BASE64 encoding. This is a commonly used in the network to transmit data encoding method. The characters that exist in these illegal characters are encoded into Base64 encoding, and there is no such character. Most importantly, the coded encoder and decoder are provided in the Java API, located in the Sun.misc Base64encoder and Base64decoder. This allows you to convert the byte stream that you get after serialization into a string. And can also be stored in an XML file.

However, these two classes are not Sun's open APIs, and compilation is a warning.

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.