Java 将linkedhashmap转换为org.w3c.dom.Element
我需要创建一个方法,给定任何LinkedHashMap,该方法必须将其转换为文档的XML/Dom元素,如:Java 将linkedhashmap转换为org.w3c.dom.Element,java,xml,dom,linkedhashmap,Java,Xml,Dom,Linkedhashmap,我需要创建一个方法,给定任何LinkedHashMap,该方法必须将其转换为文档的XML/Dom元素,如: @Override public Element marshal(Object linkedHashMap) { Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); final Element element = doc.crea
@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的属性以将它们映射到元素
我需要这样的东西,但它必须遍历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);
}
}
}
private void MarshallMapElements(ArrayList linkedHashMaps){
Document doc=getDocument();
元素根=doc.createElement(“根”);
for(映射元素:linkedHashMaps){
元素e=doc.createElement(Element.getKey());
e、 setTextContent(element.getValue());
根。子(e);
}
}
}
您确定自己要这样做吗?将对象映射到XML之前已经做了无数次了,那么为什么不直接获得一个库并节省时间呢?因为我找不到任何将对象映射/封送到org.w3c.dom.Element对象的库。您是映射到XML还是映射到org.w3c.dom.Element
对象?因为它们是两个不同的东西。我映射到org.w3c.dom.Element。我更改了标题:)