This sample project maps XML data set to a JSON data set. (The input and output formats are specified at the end of the article)
1. In Eclipse with ESB tooling, create a new ESB Solution Project(If it is not already suggested start typing to filter or check under WSO2->Message Mediation->Project types). Make sure to uncheck Create Connector Exporter Project.
2. Once you create the project, you will see that two additional projects, the CAP and Registry are created.
3. Now we will create a new REST API. Right click on the project and select New->REST API
4. Give a name to the API and a context (eg. PeopleMapping and /peopleconvert respectively).
5. In the XML, add a DataMapper and a Response. We will configure the DataMapper to suit our requirements and check the Response using Postman, an API tester. In the properties, set the POST to true.
6. To start configuring the DataMapper, double click on it, specify a name and press OK.
7. To configure the input, right click on input-> Load input-> Select XML and point to your input XML file. Similarly, to configure the output, right click->Load output->Select JSON and point to the output json file.
8. Once loaded, the attributes will be visible, simply drag and drop to map to corresponding output set. Fields can be edited by right clicking on the field.
There are many operators which can be used to get the desired mapping.
9. To deploy the API, right click on the Composite Application->Export->Select WSO2 Platform distribution->Select Composite Application Archive
10. Once the CAR file is created, start the WSO2 ESB Server and in the management console add the application by uploading the .car file.
Once the application is uploaded, the API will be displayed in the deployed APIs. (eg. PeopleMapping)
To test it in Postman, copy the invocation URL. Copy the input file content in the body and specify as XML(application/xml).
When the message is sent, the required response will be obtained.
Input XML:-
<?xml version=”1.0" encoding=”UTF-8"?> <CATALOG> <CD> <TITLE>Empire Burlesque</TITLE> <ARTIST>Bob Dylan</ARTIST> <COUNTRY>USA</COUNTRY> <COMPANY>Columbia</COMPANY> <PRICE>10.90</PRICE> <YEAR>1985</YEAR> </CD> <CD> <TITLE>Hide your heart</TITLE> <ARTIST>Bonnie Tyler</ARTIST> <COUNTRY>UK</COUNTRY> <COMPANY>CBS Records</COMPANY> <PRICE>9.90</PRICE> <YEAR>1988</YEAR> </CD> </CATALOG>
Output JSON format:-
{“artist”: [{ “name”: “sample_artist_name”, “location”: “artist_location”, “CD”:[{ “title”:”saple_title”, “price”:”sample_title” }] }] }
0 comments:
Post a Comment