Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6078663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:51:32+00:00 2026-05-23T10:51:32+00:00

My question is similar to Why are "control" characters illegal in XML 1.0? –

  • 0

My question is similar to Why are "control" characters illegal in XML 1.0? – however I’m looking for a solution to the problem below, rather than why the XML spec disallows control characters in XML.

I have a servlet, which prints a String containing an XML upon user request. One particular element contains a CDATA section which is required to contain some user input text.

Now it so happens that in one particular case, our user input contains the character U+0001 (a control character). And even though I specify the charset as UTF-8, the servlet throws an error:

Error: not well-formed
Location: 

<![CDATA[ 

Is there a way I can process the Java String to make it “XML safe” ? Particularly, to make it safe when put in the CDATA section?

I hope my question is clear!

Thanks in advance,
Raj

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:51:33+00:00Added an answer on May 23, 2026 at 10:51 am

    The only conforming way to make this XML-safe is to add your own encoding.

    You can do one of those two (for example):

    • Store all data as textual data and replace all forbidden characters with some unicode-escape mechanism (other than the one defined in XML itself!). For example you could use the one used by Java: \u0001 for U+0001. or
    • store the data as binary data and use base64Binary of hexBinary to store your data in XML.

    Both of those approaches need explicit support in both the consumer and the producer. The second approach has the advantage of using well-defined data types with wide support, but if your content is actually text, you need to specify (or communicate) the encoding used in the byte stream (a necessity that is otherwise negated by XML itself).

    If removing all non-transferable characters would be appropriate, then this regex should do the trick:

    Pattern XML_INVALID_CHARS = Pattern.compile("[^\u0009\n\r\u0020-\uD7FF\uE000-\uFFFD\uD800\uDC00-\uDBFF\uDFFF ]+");
    String xmlSafe = XML_INVALID_CHARS.matcher(input).replaceAll("");
    

    Note that the spec suggests that document authors be even more restrictive with the set of characters allowed in a note. That regex would be a bit longer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The question is similar to one . However, it differs in putting all subdirectories
I've found a similar question on SO , however, that seems not exactly what
My question is similar to below, except that i dont know whats the alternative.
My question is similar to this one JsonConvert.DeserializeObject and "d" wrapper in WCF but
Note: This question is similar to How to prevent the copy of XML documentation
This question is very similar to this one Why can't I dynamic_cast "sideways" during
This question is similar to: How do you store "int" values in an NSMutableArray*
A similar question has been posted at but i could not find the solution
I apologize in advance about asking a so similar question, but i'm rather frustrated,
My problem is similar to the one in this question, but the opposite. How

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.