用SimpleXML-JAVA将HashMap解析为xml



我需要将这些值放在一个pat中,作为Ex:的XML文件的值

Map<String, String> props = new HashMap<>();
props.put("role", "Admin");
props.put("externalId", "2ew1Q");
props.put("Property", "internal");
props.put("Execution", "internal");

我的预期输出应该是:

<role>Admin</role>
<externalId>2ew1Q</externalId>
<Property>internal</Property>
<Execution>internal</Execution>

但我得到的不是

<entry string="role">Admin</entry>
<entry string="Execution">internal</entry>
<entry string="externalId">2ew1Q</entry>
<entry string="Property">internal</entry>

我必须使用SimpleXML来完成,这是我的代码:

@Root
public class Data {

@ElementMap(entry = "property", key = "key", attribute = true, inline = true)
private Map<String, String> customProps;

public Map<String, String> getData() {
return customProps;
}

public void setData(Map<String, String> data) {
this.customProps = data;
}
}

public static void main(String[] args) throws Exception {
Map<String, String> props = new HashMap<>();
props.put("role", "Admin");
props.put("externalId", "2ew1Q");
props.put("Property", "internal");
props.put("Execution", "internal");

Data customProps = new Data();
customProps.setData(props);
Serializer serializer = new Persister();
File result = new File("example.xml");
serializer.write(customProps, result);
}

尝试:@ElementMap(entry = "property", key = "key", attribute = false, inline = true)

最新更新