Search code examples
ruby-on-railsxmlutf-8xsdutf-16

Can we change XML encoding from utf-8 to utf -16?


I have written a code for generating XML with UTF-8 encoding.I always validate the XML with XSD file. In the same code i need UTF-16 encoding. Because one of my XSD file is of UTF-16 encoding. But in my existing code it is not accepted. it gives following error.

FAILED: Fatal error: Document labelled UTF-16 but has UTF-8 content at filepath/standard.xsd:1.

and at line 1 of XSD this tag is defined <?xml version="1.0" encoding="utf-16"?>

How can i validate it with utf-8 encoding?

Is there any way to change UTF-16 to UTF-8 encoding. Thanks in advance.


Solution

  • You can change the encoding from utf16 to utf-8 with Iconv

    Call iconv from Ruby 1.8.7 through system to convert a file from utf-16 to utf-8

    When you write the new file you can replace the first line with a new header like

    <?xml version="1.0" encoding="utf-8" ?>
    

    Ruby - Open file, find and replace multiple lines

    If you need it in the other way then change the endoding in the function.