0

I need to put the values inside a pat as the values of my XML file for Ex:

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

My expected output should be:

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

But instead of it, I'm getting

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

I have to do it with Simple XML, and this is my code:

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

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

3 Comments

No, I already try it, but it doesn't work, the attribute = false converts the entry into a parent tag. just like this: <property> <key>role</key> <string>Admin</string> </property> <property> <key>Execution</key> <string>internal</string> </property>.....
Can you not just do: private String role; private String externalId; etc and then set those variables to what you want. They'll then automatically be turned into the nodes
The problem is that those values come from a database, so not always it will be "role" it could come "transferId" or even "asd" (just kidding), so the idea is that whatever is the value in the database the map could take that value and map inside the XML.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.