How to Parse XML using Kotlin in 2021

Introduction:

In 2021, Lots of APIs are written using JSON. But there are some API in some domains like healthcare, traveling and web streaming that return XML. XML is a terrible serialization format, it wasn’t initially, but has been misuse…


This content originally appeared on DEV Community and was authored by Sheeraz Ahmed Memon

Introduction:

In 2021, Lots of APIs are written using JSON. But there are some API in some domains like healthcare, traveling and web streaming that return XML. XML is a terrible serialization format, it wasn’t initially, but has been misused for decades and abused by many a software development engineer out in the wild.

When you want to interact with one of these XML, you might cry or shake your fist initially.

But no more.

We can now use Simple XML library in Kotlin that does the heavy load, parses XML and gives the POJO (Plain old java object) magically.

Steps:

So here is the XML we will use in this example, and parse it

  1. First add Simple XML library in Gradle.
  1. Now let's create POJO’s
  1. Now that POJO’s are created, we can parse it using the following code

val serializer: Serializer = Persister() val dataFetch = serializer.read(DataFetch::class.java, xmlToParse)

  1. Easy, right, here is a test to check the parsing.


This content originally appeared on DEV Community and was authored by Sheeraz Ahmed Memon


Print Share Comment Cite Upload Translate Updates
APA

Sheeraz Ahmed Memon | Sciencx (2021-12-04T13:56:53+00:00) How to Parse XML using Kotlin in 2021. Retrieved from https://www.scien.cx/2021/12/04/how-to-parse-xml-using-kotlin-in-2021/

MLA
" » How to Parse XML using Kotlin in 2021." Sheeraz Ahmed Memon | Sciencx - Saturday December 4, 2021, https://www.scien.cx/2021/12/04/how-to-parse-xml-using-kotlin-in-2021/
HARVARD
Sheeraz Ahmed Memon | Sciencx Saturday December 4, 2021 » How to Parse XML using Kotlin in 2021., viewed ,<https://www.scien.cx/2021/12/04/how-to-parse-xml-using-kotlin-in-2021/>
VANCOUVER
Sheeraz Ahmed Memon | Sciencx - » How to Parse XML using Kotlin in 2021. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/04/how-to-parse-xml-using-kotlin-in-2021/
CHICAGO
" » How to Parse XML using Kotlin in 2021." Sheeraz Ahmed Memon | Sciencx - Accessed . https://www.scien.cx/2021/12/04/how-to-parse-xml-using-kotlin-in-2021/
IEEE
" » How to Parse XML using Kotlin in 2021." Sheeraz Ahmed Memon | Sciencx [Online]. Available: https://www.scien.cx/2021/12/04/how-to-parse-xml-using-kotlin-in-2021/. [Accessed: ]
rf:citation
» How to Parse XML using Kotlin in 2021 | Sheeraz Ahmed Memon | Sciencx | https://www.scien.cx/2021/12/04/how-to-parse-xml-using-kotlin-in-2021/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.