### Question:
CollectionConverter { public static Collection toCollection(String input, Class<?> collectionClass, Class<?> genericClass, String mediaType) { try { if (MediaType.APPLICATION_XML.equals(mediaType)) { Collection c = (Collection) collectionClass.newInstance(); if (input.startsWith("<?xml")) { input = input.substring(input.indexOf(">") + 1, input.length()); } input = input.replaceAll("<" + genericClass.getSimpleName().toLowerCase() + "s>", ""); while (!input.equals("")) { int i = input.indexOf("</" + genericClass.getSimpleName().toLowerCase() + ">"); String s = input.substring(0, i + 3 + genericClass.getSimpleName().length()); input = input.substring(i + 3 + genericClass.getSimpleName().length(), input.length()); Object o = JAXBUtils.toObject(StreamUtils.toInputStream(s), genericClass, mediaType); c.add(o); } return c; } else if (MediaType.APPLICATION_JSON.equals(mediaType)) { return JAXBUtils.mapper.readValue(input, JAXBUtils.mapper.getTypeFactory() .constructCollectionType((Class<? extends Collection>) collectionClass, genericClass)); } return null; } catch (InstantiationException e) { log.error("Error during translation, Caused by:" + e.getMessage() + ", returning null"); return null; } catch (IllegalAccessException e) { log.error("Error during translation, Caused by:" + e.getMessage() + ", returning null"); return null; } catch (JsonParseException e) { log.error("Error during translation, Caused by:" + e.getMessage() + ", returning null"); return null; } catch (JsonMappingException e) { log.error("Error during translation, Caused by:" + e.getMessage() + ", returning null"); return null; } catch (IOException e) { log.error("Error during translation, Caused by:" + e.getMessage() + ", returning null"); return null; } }  static String toString(Collection<?> input, Class<?> genericClass, String mediaType); static Collection toCollection(String input, Class<?> collectionClass, Class<?> genericClass,
            String mediaType);  }### Answer:
@Test public void testToCollection() { String s = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><books><book><author>Saurabh</author><isbn>1111111111111</isbn><publication>Willey</publication></book><book><author>Vivek</author><isbn>2222222222222</isbn><publication>OReilly</publication></book><books>"; Collection c = CollectionConverter.toCollection(s, ArrayList.class, Book.class, MediaType.APPLICATION_XML); Assert.assertNotNull(c); Assert.assertFalse(c.isEmpty()); Assert.assertEquals(2, c.size()); s = "[{\"isbn\":\"1111111111111\",\"author\":\"Amresh\", \"publication\":\"Willey\"},{\"isbn\":\"2222222222222\",\"author\":\"Vivek\", \"publication\":\"Oreilly\"}]"; c = CollectionConverter.toCollection(s, ArrayList.class, Book.class, MediaType.APPLICATION_JSON); Assert.assertNotNull(c); Assert.assertFalse(c.isEmpty()); Assert.assertEquals(2, c.size()); }