I want to convert JSON response to a SOAP-based XML Response (using underscore-java).
import com.github.underscore.lodash.U;
public class JsonToXml {
public static void main(String[] args) {
String json = "[{\"id\":\"1\",\"name\":\"Bratislava\",\"population\":\"432000\"},{\"id\":\"2\",\"name\":\"Budapest\",\"population\":\"1759000\"},{\"id\":\"3\",\"name\":\"Prague\",\"population\":\"1280000\"},{\"id\":\"4\",\"name\":\"Warsaw\",\"population\":\"1748000\"},{\"id\":\"5\",\"name\":\"Los Angeles\",\"population\":\"3971000\"},{\"id\":\"6\",\"name\":\"New York\",\"population\":\"8550000\"},{\"id\":\"7\",\"name\":\"Edinburgh\",\"population\":\"464000\"},{\"id\":\"8\",\"name\":\"Berlin\",\"population\":\"3671000\"}]";
String jsonToXml = U.jsonToXml(json);
System.out.println(jsonToXml);
}
}
How can we add the below in response to XML while converting JSON to XML?
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header/>
<soapenv:Body>
Code:
String xml ="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
+ " <soapenv:Header/>"
+ " <soapenv:Body><Cities><City></City></Cities>"
+ " <OperationResponse> <Type>SUCCESS</Type> <Code>0</Code> <ResponseDetails> <ResponseDetail> <Message>Operation completed successfully</Message> </ResponseDetail> </ResponseDetails> </OperationResponse>"
+ " </soapenv:Body>"
+ "</soapenv:Envelope>";
Map<String, Object> map = (Map<String, Object>) U.fromXml(xml);
String json = "[{\"id\":\"1\",\"name\":\"Bratislava\",\"population\":\"432000\"},{\"id\":\"2\",\"name\":\"Budapest\",\"population\":\"1759000\"},{\"id\":\"3\",\"name\":\"Prague\",\"population\":\"1280000\"},{\"id\":\"4\",\"name\":\"Warsaw\",\"population\":\"1748000\"},{\"id\":\"5\",\"name\":\"Los Angeles\",\"population\":\"3971000\"},{\"id\":\"6\",\"name\":\"New York\",\"population\":\"8550000\"},{\"id\":\"7\",\"name\":\"Edinburgh\",\"population\":\"464000\"},{\"id\":\"8\",\"name\":\"Berlin\",\"population\":\"3671000\"}]";
List<Object> list = (List<Object>) U.fromJson(json);
U.set(map, "soapenv:Envelope.soapenv:Body.Cities.City", list);
System.out.println(U.toXml(map));
Output:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header/>
<soapenv:Body>
<Cities>
<City>
<id>1</id>
<name>Bratislava</name>
<population>432000</population>
</City>
<City>
<id>2</id>
<name>Budapest</name>
<population>1759000</population>
</City>
<City>
<id>3</id>
<name>Prague</name>
<population>1280000</population>
</City>
<City>
<id>4</id>
<name>Warsaw</name>
<population>1748000</population>
</City>
<City>
<id>5</id>
<name>Los Angeles</name>
<population>3971000</population>
</City>
<City>
<id>6</id>
<name>New York</name>
<population>8550000</population>
</City>
<City>
<id>7</id>
<name>Edinburgh</name>
<population>464000</population>
</City>
<City>
<id>8</id>
<name>Berlin</name>
<population>3671000</population>
</City>
</Cities>
<OperationResponse>
<Type>SUCCESS</Type>
<Code>0</Code>
<ResponseDetails>
<ResponseDetail>
<Message>Operation completed successfully</Message>
</ResponseDetail>
</ResponseDetails>
</OperationResponse>
</soapenv:Body>
</soapenv:Envelope>