Read XML Data In Vue JS


A markup language used for defining markup languages, XML is based on Standard Generalized Markup Language (SGML). The main purpose of XML is to produce data formats that are used to encapsulate data for records in databases, transactions, and many other sorts of data.

In this article, we will learn how to read the data from the XML format using  xml2js  package.

Get started:

Step 1: Set up your Vue project.

Goto your desired folder location and run the following command to create a new Vue project.

vue create read-xml

It will take some time. After creating the project, change your directory by

cd read-xml

then running the following command to check project is successfully established or not.

npm run serve

Step 2: Add dependencies.

Install bootstrap for styling…

npm install bootstrap

Install xml2js and timers for read an XML

npm install --save xml2js
npm install timers

Step 3: Add following HTML in the <template> tag.

  <div class="container p-4">
    <table class="table table-bordered table-hover">
        <tr v-for="item in xmlItems" :key="">

Step 4: Add following code in your <script> tag.

import "bootstrap/dist/css/bootstrap.min.css";
import xml2js from 'xml2js';
export default {
  name: 'HelloWorld',
  mounted() {
    this.parseXML(this.xmlData).then((data) => {
      this.xmlItems = data;
  methods: {
    //xml parsing
    parseXML(data) {
      return new Promise(resolve => {
        var k = "";
        var arr = [],
          parser = new xml2js.Parser(
              trim: true,
              explicitArray: true
        parser.parseString(data, function (err, result) {
          var obj = result.Employee;
          for (k in obj.emp) {
            var item = obj.emp[k];
              address: item.address[0],
  data: function () {
    return {
      xmlItems: [],
      //Dummy xml data.
        `<?xml version="1.0" encoding="UTF-8"?>    
          </Employee> `

Finally, save your file and check on the browser.

Submit a Comment

Your email address will not be published. Required fields are marked *


Select Categories