Require Maven dependency in project pom.xml file:
<!-- Add this Maven dependecy in your pom file --> <dependencies> <!-- https://mvnrepository.com/artifact/com.github.javadev/underscore --> <dependency> <groupId>com.github.javadev</groupId> <artifactId>underscore</artifactId> <version>1.69</version> </dependency> </dependencies>
Java code with explainer comments:
//1. require import statements import com.github.underscore.lodash.U; import java.util.LinkedHashMap; import java.util.Map; //... //2. create Map object name 'map' Map<String, Object> map = new LinkedHashMap<>(); //3. enter "key"(String), "value"(Object) pair map.put("id", "1"); map.put("name", "jorge"); //4. display map value in console System.out.println("map: " + map); //5. convert map to xml and assign to String 'xml' variable String xml = U.toXml(map); //6. display xml1 value in console System.out.println("xml: " + xml);
console output:
map: {id=1, name=jorge} xml1: <?xml version="1.0" encoding="UTF-8"?> <root> <id>1</id> <name>jorge</name> </root>