elwis elwis - 1 month ago 7
Java Question

Read XML from file with utf-8

I have a mule flow with a spring bean that reads a static XML file encoded in utf-8. However it messes up all the non-english characters.
The bean is defined as follows:

<spring:bean id="LoadFile" name="Bean" class="java.lang.String">
<spring:constructor-arg>
<spring:bean id="Test" name="org.springframework.util.FileCopyUtils" class="org.springframework.util.FileCopyUtils" factory-method="copyToByteArray">
<spring:constructor-arg type="java.io.InputStream" value="classpath:Settings.xml"/>
</spring:bean>
</spring:constructor-arg>
</spring:bean>
</spring:beans>


If I read the same file using my own code like :

InputStream in = this.getClass().getClassLoader()
.getResourceAsStream(name);


It's utf-8 and works. How can I define the springbean to care about encodings?

Regards

Answer

This is because you are creating a String by calling its constructor that takes a byte[], which will interpret the bytes using the default character encoding of your system, which is probably not UTF-8.

Specify the character encoding, by using the right constructor of String. This should work (I haven't tested it):

<spring:bean id="LoadFile" name="Bean" class="java.lang.String">
    <spring:constructor-arg>
        <spring:bean id="Test" name="org.springframework.util.FileCopyUtils" class="org.springframework.util.FileCopyUtils" factory-method="copyToByteArray">
            <spring:constructor-arg type="java.io.InputStream" value="classpath:Settings.xml"/>
        </spring:bean> 
    </spring:constructor-arg>
    <spring:constructor-arg value="UTF-8"/>
</spring:bean> 
Comments