Problem with handling "specific" characters in text (in Java, using an XML parser)

I am having problems handling "specific" characters in texts using the DOM API in Java. The files are in XML format. In a previous post I was told that there is a situation with the ampersand (&) character in XML (and a few more characters like <and>). Here is the post: Special Characters in XML Files - Processing with the DOM API

However, what could I do with other special characters in data such as specific letters in German and French? For example, I have the word "facade" in a text element of an XML document. However, the place for the letter "รง" looks garbled: when I open a file with the vim editor on Linux, it looks like this: "fa ^ Zade" when I open it with another editor as a .txt or .xml file, Space for "รง "looks like a small empty rectangle (or empty space). This applies to German umlauts and other "special" characters in other languages. They create problems when I try to parse files with an XML parser (I get parsing errors). I suppose this is a coding problem. I am using encoding = "UTF-8" in the XML file header.I tried to change it (ie To "Unicode" or others) but that doesn't help.

How can I make it so that these special characters are recognized? Should I be using some special encoding? If they were just two or three characters, which I knew for sure, I could replace them before being processed with the DOM API in Java, as was done with the ampersand (&) character (I converted and &amp;

), however there are many, and potentially any "special" character. Is the problem related to how the data was saved? For example, during the save process, it was necessary to use a special encoding (?) So that characters are now recognized (?). (I have not saved the data myself).
Thanks.

0


a source to share


3 answers


If they were just two or three characters that I knew for sure, I could replace them before being processed with the DOM API in Java, as was done with the ampersand (&) character (I converted and &), however they are a lot. and can potentially be any "special" character.



You don't need to anticipate all possible inputs. Instead, simply convert each such entity to an NCR, or Numeric Character Reference. For example &#x20AC;

is NCR for the Euro symbol โ‚ฌ; this means that 20AC

is the hexadecimal Unicode reference for the Euro symbol.

+1


a source


This doesn't sound like an XML problem, but an encoding problem. XML can handle both UTF-8 and Latin-1. But you need to know the input encoding, or NOT use the reader, but the input stream with XML declaration using the correct encoding attribute.



Are you sure the source is not damaged? What encoding? Is the XML encoding attribute of the declaration on the first line correct? ^ Z is not like UTF-8 encoding!

+1


a source


encoding = "UTF-8" seems like the right way, then you don't have to handle any of these characters differently. You said, "I am using encoding =" UTF-8 "in the XML header, but are you also writing the character data as UTF-8?

In vim you can use "ga", I think to show the code of the character under the cursor, it should help with debugging.

0


a source







All Articles