Parse XML and JSON with JavaScript | Plantpot

Parse XML and JSON with JavaScript

Web Dev
Sponsored links

In this article, I will introduce how to parse XML with JavaScript.

Features of XML format and JSON format

The features of each are as follows.
Common features include nesting, arrays, and other iterations.

XML format


  • Define data using start and end tags
  • Larger number of characters (data volume) than JSON format

XML defines data items by tag name as shown below, and the value enclosed by the start tag and end tag represents the data.

<?xml version="1.0"?>

JSON format


  • Define data by "definition name: value"
  • The number of characters (data volume) is smaller than the XML format

JSON defines data with a pair of definition name and value as follows

Sponsored links

Parse XML and JSON

Converting the object format is called parsing.
Character strings representing XML / JSON can be converted to each format.

The conversion processes are explained below.

Converting an XML string to an XML document

const strXml = '<school> 
                </school >';
const parser = new DOMParser();
let xmlData  = parser.parseFromString(strXml,"text/xml");

Conversion from JSON string to JSON object

const strJson = '{
let jsonData  = JSON.parse(strJson);