5

How to Parse XML using Kotlin in 2021

 2 years ago
source link: https://medium.com/codechai/how-to-parse-xml-using-kotlin-in-2021-21f2fffbdf22
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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.

2. Now let's create POJO’s

3. 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)

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK