0

I am just starting coding xml and pls help me on below xml. Parsing to json format xml

<Booking> <BookingId>123</BookingId>
 <BookingDate>17Jan18</BookingDate>
<BookingStatus>Active</BookingStatus> <BookingName>  
 <BookingNameId>ABC </BookingNameId>   <BookingId>111</BookingId>`$`  
 <ContactTelNum>0911</ContactTelNum> </BookingName> </Booking>

I want the output to be like below :

 "Booking":{ "BookingId": "123",    "BookingDate": "17Jan18", 
 "BookingStatus": "Active",    "BookingName": {
       "BookingNameId": "ABC",
       "BookingId": "111",
       "ContactTelNum": "0911"    } }
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
bebi
  • 1
  • 1
  • 1
    See for example: https://stackoverflow.com/questions/39493173/how-to-convert-xml-to-json-using-only-jackson – lexicore Mar 07 '18 at 14:09
  • 1
    Also you can check that: https://stackoverflow.com/questions/1823264/quickest-way-to-convert-xml-to-json-in-java – Mert Öksüz Mar 07 '18 at 14:14
  • thanks ...i have the codes but it doesn't work as i intended as you can see on the output ...so if you could help me on the recursive loop node code – bebi Mar 07 '18 at 14:34

1 Answers1

0

Using JSON.org

String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
             "<errors>\n" +
             "  <error>\n" +
             "    <status>400</status>\n" +
             "    <message>The field 'quantity' is invalid.</message>\n" +
             "    <details>\n" +
             "      <invalid_reason>The quantity specified is greater than the quantity of the product that is available to ship.</invalid_reason>\n" +
             "      <available_quantity>0</available_quantity>\n" +
             "      <order_product_id>12525</order_product_id>\n" +
             "    </details>\n" +
             "  </error>\n" +
             "</errors>";

String json = XML.toJSONObject(xml).toString();

Dependencies required

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20160810</version>
</dependency>
King
  • 1,885
  • 3
  • 27
  • 84