@Test public void should_map_element_to_hashMap_free_tags() throws Exception { Content content = (Content) unmarshaller.unmarshal(getClass().getResourceAsStream( "/element_with_complex_free_data.xml")); Map<String, Object> map = elementMapper.toMap(content.elements); assertThat(map).hasSize(7); assertThat(map.get("DataInTestTab0")).isNotNull(); assertThat(map.get("Description")).isNotNull(); List<Map> dataInTestTab0 = (List<Map>) map.get("DataInTestTab0"); assertThat(dataInTestTab0).hasSize(1); assertThat( dataInTestTab0.toString() .equals("[{DataInTestTab1=[{DataInTestTab2=[{DataInTestTab3=[f, g, h, i, j]}]}]}]")); assertThat((List<String>) map.get("DataInTestNum")).containsExactlyInAnyOrder("1", "20"); }
public Map<String, Object> toMap(List<Object> elements) { Map<String, List<Object>> collect = elements.stream() .filter(item -> item instanceof Element) .map(item -> (Element) item) .map(item -> new SimpleImmutableEntry<>(item.getLocalName(), elementToMap(item))) .collect(Collectors.toMap( SimpleImmutableEntry::getKey, SimpleImmutableEntry::getValue, (o, o2) -> Stream.concat(o.stream(), o2.stream()).collect(Collectors.toList()))); return (Map) collect; }
ElementMapper { public Map<String, Object> toMap(List<Object> elements) { Map<String, List<Object>> collect = elements.stream() .filter(item -> item instanceof Element) .map(item -> (Element) item) .map(item -> new SimpleImmutableEntry<>(item.getLocalName(), elementToMap(item))) .collect(Collectors.toMap( SimpleImmutableEntry::getKey, SimpleImmutableEntry::getValue, (o, o2) -> Stream.concat(o.stream(), o2.stream()).collect(Collectors.toList()))); return (Map) collect; } }
ElementMapper { public Map<String, Object> toMap(List<Object> elements) { Map<String, List<Object>> collect = elements.stream() .filter(item -> item instanceof Element) .map(item -> (Element) item) .map(item -> new SimpleImmutableEntry<>(item.getLocalName(), elementToMap(item))) .collect(Collectors.toMap( SimpleImmutableEntry::getKey, SimpleImmutableEntry::getValue, (o, o2) -> Stream.concat(o.stream(), o2.stream()).collect(Collectors.toList()))); return (Map) collect; }  }
ElementMapper { public Map<String, Object> toMap(List<Object> elements) { Map<String, List<Object>> collect = elements.stream() .filter(item -> item instanceof Element) .map(item -> (Element) item) .map(item -> new SimpleImmutableEntry<>(item.getLocalName(), elementToMap(item))) .collect(Collectors.toMap( SimpleImmutableEntry::getKey, SimpleImmutableEntry::getValue, (o, o2) -> Stream.concat(o.stream(), o2.stream()).collect(Collectors.toList()))); return (Map) collect; }  Map<String, Object> toMap(List<Object> elements); }
ElementMapper { public Map<String, Object> toMap(List<Object> elements) { Map<String, List<Object>> collect = elements.stream() .filter(item -> item instanceof Element) .map(item -> (Element) item) .map(item -> new SimpleImmutableEntry<>(item.getLocalName(), elementToMap(item))) .collect(Collectors.toMap( SimpleImmutableEntry::getKey, SimpleImmutableEntry::getValue, (o, o2) -> Stream.concat(o.stream(), o2.stream()).collect(Collectors.toList()))); return (Map) collect; }  Map<String, Object> toMap(List<Object> elements);  }