简体   繁体   English

如何从包含嵌套/复杂对象的 Map 创建 DOM 元素列表

[英]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方法能够处理任何复杂/嵌套的值,例如MapString并递归地创建一个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方法基于MapStringList构建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.

相关问题 如何在 java 中创建嵌套列表对象的 Map? - How can I create a Map of my nested List objects in java? 如何从包含列表的地图中检索特定值? - How to retrieve paricular value from map which contains list? 将 map 映射到包含名称和值参数的对象列表 - Mapping map to a list of objects which contains a name and a value parameter 如何在Android中将包含对象列表的对象列表从一个活动传递到另一个活动 - How to pass List of objects which again contains List of objects from one activity to another in Android 如何从 map 中的列表中获取元素数? - How do I get the number of elements from a list which is in a map? 如何对还包含对象列表的对象列表执行 DFS - How to do a DFS on a list of objects which also contains a list of objects 如何将包含对象列表的对象列表显示为表格? - How to display a list of objects which contains a list of objects as a table? 如何创建一个 Java 列表/映射/集合,我可以在其中修改值但不能添加或删除元素? - How can I create a Java List/Map/Collection in which I can modify the values but not add nor remove elements from it? 如何将嵌套对象列表减少为 Map - How to reduce a list of nested objects into a Map 如何对包含对象列表的对象列表进行分页 - How to paginate a list of objects which contains a list of object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM