我想创建一个方法:给定任何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的所有级别和子级别(嵌套对象):
Element root = doc.createElement(rootName);
doc.appendChild(root);
for (Map.Entry<String,String> element : map.entrySet() ) {
Element e = doc.createElement(element.getKey());
e.setTextContent(element.getValue());
root.appendChild(e);
}