aspose file tools*
The moose likes Java in General and the fly likes Writing Russian characters to XML file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Writing Russian characters to XML file" Watch "Writing Russian characters to XML file" New topic
Author

Writing Russian characters to XML file

Guru Rao
Greenhorn

Joined: Apr 24, 2009
Posts: 11
Hi,

I am trying to write few Russian characters into a XML file using XStream. I am using unicode encoding "UTF8". Most of the Russian characters are getting written correctly, but not all.

Here is the code I am using:-

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class Test {

public static void main(String[] args) {

String str = null;

try {

str = Util.getRussianChar();

XStream xstream = new XStream(new DomDriver());
xstream.alias("test", A.class);

A a = new A();
a.setTest(str);


File file = new File("d:\\Russian1.xml");
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter
(new FileOutputStream(file),"UTF8"));
bufferedWriter.write("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
//bufferedWriter.write("<?xml version=\"1.0\" encoding=\"UTF-8?>");
bufferedWriter.newLine();
xstream.toXML(a, bufferedWriter);


} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

The value in String "str" is "Сибирь/Дальний восток". Here is my code of A.java :-

public class A {

String test;

/**
* @return the test
*/
public String getTest() {
return test;
}

/**
* @param test the test to set
*/
public void setTest(String test) {
this.test = test;
}

}

And here is the Russian1.xml :-

<?xml version="1.0" encoding="ISO-8859-1"?>

<test>
<test>Сибирь/Дальний во�?ток</test>
</test>

As can be seen above in the XML file, few Russian characters are not written correctly into the file.

Any idea what has gone wrong?

Regards,
Guru
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42919
    
  68
If it's UTF-8 you're writing to the file, then you had better specify that as the encoding in the XML file, hadn't you? You're specifying ISO-8859-1 (which does not include any Russian characters, AFAIK). Also note that it's called "UTF-8", not "UTF8".
Guru Rao
Greenhorn

Joined: Apr 24, 2009
Posts: 11
Yes, I have already tried those options, but the result is same.
ravi pardeshi
Greenhorn

Joined: Jan 13, 2009
Posts: 23


add this one in your web.xml file.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19784
    
  20

Thanks for your input, but what makes you think this is about servlets or JSP files?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
ravi pardeshi
Greenhorn

Joined: Jan 13, 2009
Posts: 23
i had this problem for Chinese characters. i solved that one using that filter.


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19784
    
  20

I'm sure it did, but it won't work with normal Java applications, now will it?
ravi pardeshi
Greenhorn

Joined: Jan 13, 2009
Posts: 23
ohh sory ...

i forgot that one.

he is doing for standalone application.
no idea about this.
Guru Rao
Greenhorn

Joined: Apr 24, 2009
Posts: 11
The issue is fixed.
Sorry guys, the problem was not in the program posted by me. The issue was with the Utility which retrieved the Russian characters, that is "str = Util.getRussianChar(); "

Anyways, thanks for all your inputs.

Regards,
Guru
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19784
    
  20

Don't you just hate it when that happens? You spend so long trying to find out what's wrong in your code, only to find some third party code has a bug...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Writing Russian characters to XML file