Unable to parse chinese and french characters in XML with groovy and HTTP request

1.1k views Asked by At

Below is the code to read a XML from a simple XML file on disk. It contains the Chinese and french characters. It works perfectly fine and displays the characters correctly.


Plugin available on Github:- https://github.com/Citytechinc/cq-groovy-console.git/tags/7.0.1


Code Snippet (Written in Groovy Console Plugin with AEM 6)

 String file = "C:\\data\\create.xml";
     String line = "";
     StringBuilder sb = new StringBuilder();
     BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF-8"));
     while ((line = br.readLine()) !=null) {
         sb.append(line);
     }
     def xmlf = new XmlSlurper().parseText(sb.toString())
        .declareNamespace(gContact:'http://schemas.google.com/contact/2008',
            gd:'http://schemas.google.com/g/2005')

     println xmlf.title 

XML

<?xml version="1.0"?>
<entry xmlns:atom='http://www.w3.org/2005/Atom'
  xmlns:gd='http://schemas.google.com/g/2005'>
<category scheme='http://schemas.google.com/g/2005#kind'
  term='http://schemas.google.com/contact/2008#contact' />
<title type='text'>Elizabeth Bennet 反常的矛盾 degrés la réalité pourrions réessayer</title>

</entry>

Response:-

Elizabeth Bennet 反常的矛盾 degrés la réalité pourrions réessayer

But now i mock the same xml with SOAP UI Rest request and consume it using HTTPClient it does not work for chinese characters. I have tried with both UTF-8 an ISO-8859-1. Also using getResponseBodyAsStream and AsString does not work. Please Help. Below is the code and current response.

Code Snippet (Written in Groovy Console Plugin ver 7.01 with AEM 6)

 HttpClient client = new HttpClient()
    String importUrl = "http://localhost:8080/xml/node"
    HttpMethod method = new GetMethod(importUrl)
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
         new DefaultHttpMethodRetryHandler(3, false));
    int statusCode = client.executeMethod(method);
    byte[] responseBody = method.getResponseBody()
    println new String(responseBody)

Current Response

<?xml version="1.0" encoding="ISO-8859-1"?>
<entry xmlns:atom='http://www.w3.org/2005/Atom'
  xmlns:gd='http://schemas.google.com/g/2005'>
<category scheme='http://schemas.google.com/g/2005#kind'
  term='http://schemas.google.com/contact/2008#contact' />
<title type='text'>Elizabeth Bennet ????? degrés la réalité pourrions réessayer</title>

</entry>
0

There are 0 answers