将linkedhashmap转换为org.w3c.dom.Element

时间:2019-12-09 10:32:03

标签: java xml dom linkedhashmap

我需要创建一个给定任何LinkedHashMap的方法,该方法必须将其转换为Document的XML / Dom元素,如:

@Override
    public Element marshal(Object linkedHashMap) {

       Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
       final Element element = doc.createElement("root");

       //For each attribute and object of the linked hashmap
       // I must iterate recursively in order to get all the objects and atributes of the LinkedHashMap and append them to the root node element:

       //element.appendChild(...));

      return element; // here the element must be already populated with all the attributes of the linked hashmap and its values.
    }

我不知道该如何实现,如何遍历LinkedHashMap的属性以将它们映射到Element?

我需要这样的东西,但是它必须遍历linkedhashmap的所有级别和子级别(嵌套的linkedhashmap对象):

 private void marshalMapElements(ArrayList<LinkedHashMap> linkedHashMaps) {
        Document doc = getDocument();
        Element root = doc.createElement("root");
        for (Map<String, Object> element : linkedHashMaps) {
                Element e = doc.createElement(element.getKey());
                e.setTextContent(element.getValue());
                root.appendChild(e);
            }
        }
    }

0 个答案:

没有答案