We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › xml and special characters
Page Index Toggle Pages: 1
xml and special characters (Read 684 times)
xml and special characters
Aug 28th, 2007, 9:37pm
 
hi,

i want to parse a xml file which contains special characters like ö ü ä ß ? ! @. the parsing of the structure works fine with the processing.xml library as well as with the proxml library but the special characters are replaced for some other weird characters in both approaches. the encoding of my xml file is UTF-8 which shoud support those characters.

does anyone know how to preserve those characters?

i thought about getting rid of those in the first place by escaping all the strings when writing the xml file (this is not done with processing) but i don't know how to unescape escaped strings in processing and didn't find anything in the reverence.

Re: xml and special characters
Reply #1 - Aug 29th, 2007, 12:40am
 
for processing.xml, the characters will work properly if the file is stored in your machine's native encoding (e.g. MacRoman or CP1252), but will break if you switch to another platform to run the sketch. that's no fun, so to get UTF8 to work properly, the current workaround is to use:
Code:

InputStream is = openStream("yourfile.xml");
InputStreamReader isr = new InputStreamReader(is, "UTF8");
BufferedReader reader = new BufferedReader(isr);
XMLElement xml = new XMLElement(reader);


more about how all that works is here:
http://java.sun.com/docs/books/tutorial/i18n/text/stream.html

the longer term solution is that we have to make a switch to where we just use utf8 for everything, unless specified otherwise (e.g. unless you write code like the example above). this is a messy change since it affects the api, but if you think about it, it needs to be done for .pde files as well. we'll probably make this shift in the next couple releases (but definitely before 1.0).
Re: xml and special characters
Reply #2 - Aug 29th, 2007, 7:57pm
 
thanks a lot fry!

i had to put your code inside try/catch. now it works perfectly fine.

if somebody has the same problem, this is the code embeded in a function.

XMLElement fixEncoding(String filename) {
 try {
   InputStream is = openStream(filename);
   InputStreamReader isr = new InputStreamReader(is, "UTF8");
   BufferedReader reader = new BufferedReader(isr);
   XMLElement xml = new XMLElement(reader);
   return xml;
 }
 catch (IOException e) {
   e.printStackTrace();
   return null;
 }
}
Page Index Toggle Pages: 1