[英]How to create a LIST of DOM elements from Map which contains nested/complex objects
I have a Map<String, Object>
field which can contain complex types.我有一个可以包含复杂类型的
Map<String, Object>
字段。 The value (Object)
can contain Map, String or ArrayList
my goal is to write a method that can recursively loop over the Map
and create a nested DOM
elements and write into List
. value (Object)
可以包含Map, String or ArrayList
我的目标是编写一个可以递归循环Map
并创建嵌套DOM
元素并写入List
的方法。 I was able to complete it halfway through it and after that, I am unable to understand how to proceed in the recursive
approach.我能够在中途完成它,之后,我无法理解如何以
recursive
方法进行。
Basically, I want my Marshalling
method to handle any complex/nested values such as Map
and String
and create a DOM Element
recursively and store it in List
.基本上,我希望我的
Marshalling
方法能够处理任何复杂/嵌套的值,例如Map
和String
并递归地创建一个DOM Element
并将其存储在List
中。
My input Map<String, Object>
can be anything like (can be more nested/complex or simple):我的输入
Map<String, Object>
可以是任何类似的东西(可以更嵌套/复杂或简单):
{google:first={google:second={google:third=Value1, google:forth={google:fifth=Value2}}}}
These values are provided as Map<String, Object>
to my method.这些值作为
Map<String, Object>
提供给我的方法。 I want to identify what kind of values are incoming based on which I need to create the List<Object>
.我想根据我需要创建
List<Object>
来确定传入的值类型。 These Objects
are DOM
elements that I am using later to create my XML
file.这些
Objects
是我稍后用来创建XML
文件的DOM
元素。 I am stuck at some point and unable to get desired output.我被困在某个时刻,无法获得所需的 output。
Following is the code I have so far where I am using the recursive
method to build the Element
.以下是我目前使用
recursive
方法构建Element
的代码。 I want the Marshalling
method to build the Complex
element based on Map
, String
, and List
so it can handle any type of complex UserExtensions.
我希望
Marshalling
方法基于Map
、 String
和List
构建Complex
元素,以便它可以处理任何类型的复杂UserExtensions.
public class Main {
public static void main(String[] args) throws JsonProcessingException, ParserConfigurationException {
Map<String, Object> userExtensions = new HashMap<>();
Map<String, Object> complex1 = new HashMap<>();
Map<String, Object> complex2 = new HashMap<>();
Map<String, Object> complex3 = new HashMap<>();
complex3.put("google:fifth", "Value2");
complex2.put("google:fourth", complex3);
complex2.put("google:third", "Value1");
complex1.put("google;second", complex2);
userExtensions.put("google:first", complex1);
List<Object> result = Marshalling(userExtensions);
}
private static List<Object> Marshalling(Map<String, Object> userExtensions) throws ParserConfigurationException {
List<Object> tempElement = new ArrayList<>();
javax.xml.parsers.DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();
org.w3c.dom.Document doc = db.newDocument();
for (Map.Entry<String, Object> property : userExtensions.entrySet()) {
if (property.getValue() instanceof Map) {
Element complexElement = doc.createElement(property.getKey());
System.out.println(" Element : " + complexElement);
Marshalling((Map<String, Object>) property.getValue()).forEach(innerChildren -> {
if (innerChildren instanceof Element) {
if (((Element) innerChildren).getTextContent() != null) {
org.w3c.dom.Element newNode = doc.createElement(((Element) innerChildren).getNodeName());
newNode.setTextContent(((Element) innerChildren).getTextContent());
complexElement.appendChild(newNode);
}
}
});
tempElement.add(complexElement);
} else {
Element simpleElement = doc.createElement(property.getKey());
simpleElement.setTextContent(((String) property.getValue()));
System.out.println(tempElement.size());
tempElement.add(simpleElement);
}
}
return tempElement;
}
}
I was referring to the following answer a bit: https://stackoverflow.com/a/25359634/7584240我指的是以下答案: https://stackoverflow.com/a/25359634/7584240
I hope I have provided all the needed explanation.我希望我已经提供了所有需要的解释。 Any help or suggestion would be really helpful for me.
任何帮助或建议都会对我很有帮助。 Thanks in advance.
提前致谢。
I tried a lot of things and did some research, I was able to get it, posting the answer here as it can be useful to someone in the future:我尝试了很多东西并做了一些研究,我得到了它,在这里发布答案,因为它对将来的某人有用:
public List<Object> Marshalling(Map<String, Object> userExtensions) throws ParserConfigurationException {
if (userExtensions == null) {
return null;
}
List<Object> tempElement = new ArrayList<>();
for (Map.Entry<String, Object> property : userExtensions.entrySet()) {
Element root = document.createElement(property.getKey());
if (property.getValue() instanceof Map) {
List<Object> mapElements = Marshalling((Map<String, Object>) property.getValue());
mapElements.forEach(innerChildren -> {
if (innerChildren instanceof Element) {
if (((Element) innerChildren).getTextContent() != null) {
root.appendChild(document.appendChild((Element) innerChildren));
}
}
});
tempElement.add(root);
} else if (property.getValue() instanceof String) {
root.setTextContent(((String) property.getValue()));
tempElement.add(root);
} else if (property.getValue() instanceof ArrayList) {
for (Object dupItems : (ArrayList<Object>) property.getValue()) {
if (dupItems instanceof Map) {
Element arrayMap = document.createElement(property.getKey());
List<Object> arrayMapElements = Marshalling((Map<String, Object>) dupItems);
arrayMapElements.forEach(mapChildren -> {
if (mapChildren instanceof Element) {
if (((Element) mapChildren).getTextContent() != null) {
arrayMap.appendChild(document.appendChild((Element) mapChildren));
}
}
});
tempElement.add(arrayMap);
} else if (dupItems instanceof String) {
Element arrayString = document.createElement(property.getKey());
arrayString.setTextContent((String) dupItems);
tempElement.add(arrayString);
}
}
}
}
return tempElement;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.