How to Parse JSON & XML Using the Same Code in Java

How to Parse JSON & XML Using the Same Code in Java

In this article, you’ll learn how to do it in Java by using dataformat-xml — Jackson library extension in such a way that you only need to write your code once.

Supporting multiple formats like XML (Extensible Markup Language) and JSON (JavaScript Object Notation) usually means multiple implementations that deal with serializing and deserializing data. Fortunately, it’s possible to do it using the same code.

In this article, you’ll learn how to do it in Java by using dataformat-xml — Jackson library extension in such a way that you only need to write your code once. We’ll go through the main features including model definition, custom serialization, and deserialization. Finally, I’ll share a tricky use-case where our “one to rule them all code” won’t work and explain why.

Project setup

We’ll use a simple Maven project. In the pom.xml build section, there is a maven-compiler-plugin configured to use Java 11. We’ll also need jackson-dataformat-xml dependency.

<?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>

        <groupId>rauluka</groupId>
        <artifactId>xml-json-one-code</artifactId>
        <version>1.0-SNAPSHOT</version>

        <properties>
            <maven.compiler.release>11</maven.compiler.release>
        </properties>

        <dependencies>
            <dependency>
                <groupId>com.fasterxml.jackson.dataformat</groupId>
                <artifactId>jackson-dataformat-xml</artifactId>
                <version>2.11.2</version>
            </dependency>
        </dependencies>

        <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.1</version>
                </plugin>
            </plugins>
        </pluginManagement>
        </build>
    </project>

Maven pom.xml file (gist by Author).

code coding java technology programming

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How to Install OpenJDK 11 on CentOS 8

What is OpenJDK? OpenJDk or Open Java Development Kit is a free, open-source framework of the Java Platform, Standard Edition (or Java SE).

Functional Programming Techniques in Java with Examples

In this article, I will write about how to write code using Java’s API for functional programming. In the end, we will write our own stream API so we can understand how to implement a functional programming style in Java.

Learn Java Programming | Java Complete Tutorial for Beginners | Simpliv

This program on Java programing from Simpliv’s experts uses Java and an Integrated Development Environment (IDE). Just download this, with their help of course, and open your way to hundreds of lines of source code, and hundreds of lines of comments. That is some Java programming learning!

13 Free/Low-Cost Sites to Supercharge Your Programming Self-Education

Although we still talk about programming as a standalone career, the dominance of technology in our lives makes it clear that coding is much more than a career path. In my opinion, computer science is more than a college major or a high-paid job; it’s a skill, essential for thriving in a modern-day economy. Whether you work in healthcare, marketing, business, or other fields, you will see more coding and have to deal with a growing number of technologies throughout your entire life.

How to Structure Your Programming Code

I remember my first fumble with basic on my ZX Spectrum computer back in the 1980s, ploughing through pages of basic commands and example code without any real idea of how I could write programs myself