I use the below function to retrieve the web service response:

```
private String getSoapResponse (String url, String host, String encoding, String soapAction, String soapRequest) throws MalformedURLException, IOException, Exception {         
    URL wsUrl = new URL(url);     
    URLConnection connection = wsUrl.openConnection();     
    HttpURLConnection httpConn = (HttpURLConnection)connection;     
    ByteArrayOutputStream bout = new ByteArrayOutputStream(); 

    byte[] buffer = new byte[soapRequest.length()];     
    buffer = soapRequest.getBytes();     
    bout.write(buffer);     
    byte[] b = bout.toByteArray();          

    httpConn.setRequestMethod("POST");
    httpConn.setRequestProperty("Host", host);

    if (encoding == null || encoding == "")
        encoding = UTF8;

    httpConn.setRequestProperty("Content-Type", "text/xml; charset=" + encoding);
    httpConn.setRequestProperty("Content-Length", String.valueOf(b.length));
    httpConn.setRequestProperty("SOAPAction", soapAction);

    httpConn.setDoOutput(true);
    httpConn.setDoInput(true);

    OutputStream out = httpConn.getOutputStream();
    out.write(b); 
    out.close();

    InputStreamReader is = new InputStreamReader(httpConn.getInputStream());
    StringBuilder sb = new StringBuilder();
    BufferedReader br = new BufferedReader(is);
    String read = br.readLine();

    while(read != null) {
        sb.append(read);
        read = br.readLine();
    }

    String response = decodeHtmlEntityCharacters(sb.toString());    

    return response = decodeHtmlEntityCharacters(response);
}

```

But my problem with this code is it returns lots of special characters and makes the structure of the XML invalid.  

Example response:

```
&lt;PLANT&gt;A565&lt;/PLANT&gt;
          &lt;PLANT&gt;A567&lt;/PLANT&gt;
          &lt;PLANT&gt;A585&lt;/PLANT&gt;
          &lt;PLANT&gt;A921&lt;/PLANT&gt;
          &lt;PLANT&gt;A938&lt;/PLANT&gt;
        &lt;/PLANT_GROUP&gt;
      &lt;/KPI_PLANT_GROUP_KEYWORD&gt;
      &lt;MSU_CUSTOMERS/&gt;
    &lt;/DU&gt;
    &lt;DU&gt; 

```

So to solve this, I use the below method and pass the whole response to replace all the special characters with its corresponding punctuation.

```
private final static Hashtable htmlEntitiesTable = new Hashtable();
static {
    htmlEntitiesTable.put("&amp;","&");
    htmlEntitiesTable.put("&quot;","\"");
    htmlEntitiesTable.put("&lt;","<");
    htmlEntitiesTable.put("&gt;",">");  
}

private String decodeHtmlEntityCharacters(String inputString) throws Exception {
    Enumeration en = htmlEntitiesTable.keys();

    while(en.hasMoreElements()){
        String key = (String)en.nextElement();
        String val = (String)htmlEntitiesTable.get(key);

        inputString = inputString.replaceAll(key, val);
    }

    return inputString;
}

```

But another problem arised. If the response contains this segment `&lt;VALUE&gt;&lt; 0.5 &lt;/VALUE&lt;` and if this will be evaluated by the method, the output would be: 

`<VALUE>< 0.5</VALUE>`

Which makes the structure of the XML invalid again.
The data is correct and valid "< 0.5" but having it within the VALUE elements causes issue on the structure of the XML.

Can you please help how to deal with this? Maybe the way I get or build the response can be improved. Is there any better way to call and get the response from web service?

How can I deal with elements containing "<" or ">"?