Search code examples
utf-8character-encodingglassfish-3

java XML reading from url connection on Glassfish encoding issue?


I try to read data from xml url using URLConnection and DocumentBuilder objects and persist on MySQL DB. with my Hibernate web application. xml element datas contain Turkish characters. Actually i achieved the issue on my local environment(Windows7). So it persists succesfully on my local MYSQL db. But if i try to run on my AWS environment seems to be problem with Turkish characters(ı,İ,ş,ç,ğ,ü,ö). MYSQL collation is utf8_unicode_ci. Also source xml on url contains utf-8 encoding as well. Also, i could not find the solution with my System.out s trials. What might be the problem?Thx Glassfish 3.1.2 javaee5

UPDATE: I already have checked jre charset via Charset.defaultCharset(). It is UTF-8

        URL url = new URL("http://domain/address");
        URLConnection urlConnection = null;
        urlConnection = url.openConnection();
        urlConnection.setRequestProperty("Accept-Charset", "UTF-8");
        urlConnection.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
        InputStream inputStream = urlConnection.getInputStream();

        db = dbf.newDocumentBuilder();
        InputSource source = new InputSource(new InputStreamReader(inputStream, "UTF-8"));
        source.setEncoding("UTF-8");
        dom = db.parse(source);
        NodeList nodeList = dom.getElementsByTagName("Product");

        Product product = null;
        for (int temp = 0; temp < nodeList.getLength(); temp++)
        {
            Node nNode = nodeList.item(temp);
            if (nNode.getNodeType() == Node.ELEMENT_NODE)
            {
                Element productNode = (Element)nNode;
                product = new Product();
                String name=productNode.getElementsByTagName("Name").item(0).getTextContent();
                System.out.println("before encoding: " + name);
                System.out.println("encoding_utf-8: " + new String (name.getBytes ("UTF-8"), "UTF-8"));
                System.out.println("encoding_iso-8859-9: " + new String (name.getBytes ("UTF-8"), "ISO-8859-9"));
                product.setBarcode(productNode.getElementsByTagName("Name").item(0).getTextContent());
...

Solution

  • After spending many hours for that, finally solved. That was related with jdbc connection resource. I added 3 lines with bold on my domain.xml file such as below. Hope that helps to someone

        <jdbc-resource pool-name="MYQSL_accmeepool" description="" jndi-name="jdbc/accmee"></jdbc-resource>
        <jdbc-connection-pool driver-classname="" datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" res-type="javax.sql.XADataSource"
                              description="" name="MYQSL_accmeepool">
            <property name="user" value="root"></property>
            <property name="password" value="admin"></property>
            <property name="serverName" value="localhost"></property>
            <property name="databaseName" value="accmee"></property>
            <property name="portNumber" value="3306"></property>
            **<property name="useUnicode" value="true"/>
            <property name="characterEncoding" value="utf8"/>
            <property name="characterSetResults" value="utf8"/>**
        </jdbc-connection-pool>