简体   繁体   中英

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. 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 . I was able to complete it halfway through it and after that, I am unable to understand how to proceed in the recursive approach.

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 .

My input Map<String, Object> can be anything like (can be more nested/complex or simple):

{google:first={google:second={google:third=Value1, google:forth={google:fifth=Value2}}}}

These values are provided as Map<String, Object> to my method. I want to identify what kind of values are incoming based on which I need to create the List<Object> . These Objects are DOM elements that I am using later to create my XML file. I am stuck at some point and unable to get desired output.

Following is the code I have so far where I am using the recursive method to build the 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.

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

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;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM