IMPORTANT RULES:
- Maintain the original formatting (markdown, HTML tags, links, etc.)
- Keep technical terms and proper nouns as appropriate
- Preserve code blocks and technical syntax exactly
- Maintain the same tone and style
- Only output the translated text, no explanations or comments
Previously, I discussed the WordPress plugin’s usage with PHP, including XML and cURL in the article titled XML2WOO (WordPress Plugin: XML2WOO). In this article, I will reimplement a similar process using Node.js.
Node.js
In simple terms, Node.js can be described as an open-source, server-side runtime environment. For more detailed information, please refer to the article titled What is Node.js?. Let’s now move on to our application.
Node.js and XML
Node.js primarily uses the JSON format for data transmission. However, some applications may require communication in different formats due to various service-specific reasons. One of these formats is, of course, XML. So, how can we perform file operations with XML using Node.js?
As is well known, the Node.js ecosystem is built upon modules. Each package consists of one or more modules designed to perform specific functions. Therefore, we will need to search for the solution among these modules. In the process of searching and downloading modules, we will also need package managers. NPM is one such tool.
When we perform a quick search for XML-related content on NPM, we end up with approximately 3,000 results. By filtering the results based on popularity and quality assessments, we can narrow down our selection further1.
The package we will use in our example is xml2js2. Its function is to convert XML files into JavaScript objects.
First, we need an XML source. In the PHP example, we based our work on an XML file created by Ideasoft web sites. In this article, I’ll use an example from the haydigiytoptan website, one of the XML sources I frequently encounter, where attributes are defined as attribute values.
The structure of our XML file is shown below. Let’s call the file text.xml.
<?xml version="1.0" encoding="utf-8"?>
<Products>
<Product Id="" ModelCode="" Sku="" Gtin="" Name="" ShortDescription="" FullDescription="" Currency="" StockQuantity="" OldPrice="" Price="" Weight="" Price1="" Price2="" Price3="" Price4="" Price5="" Tax="">
<Categories>
<Category Path="" DisplayOrder="" />
</Categories>
<Manufacturers>
<Manufacturer Name="" DisplayOrder="" /></Manufacturers>
<Pictures>
<Picture Path="" />
</Pictures>
<Combinations>
<Combination Id="" Sku="" Gtin="" StockQuantity="">
<Attributes>
<Attribute Name="" Value="" />
</Attributes>
</Combination>
</Combinations>
<Specifications>
<Specification Name="" Value="" DisplayOrder="" />
</Specifications>
<SameProducts>
<SameProduct Id="" DisplayOrder="" />
</SameProducts>
</Product>
</Products>
XML – JSON Operations with Node.js
I’ll perform these operations on a server. For this purpose, I use the DigitalOcean command-line application and create a Node.js droplet using the doctl tool.
doctl compute ssh-key list
doctl compute droplet create nodexml --size s-1vcpu-1gb --image sharklabs-nodejsquickstart-18-04 --region sfo2 --ssh-keys <ssh-key-id|name|fingerprint>
doctl compute droplet list --format "ID,Name,PublicIPv4"
After the completion of the installation, I can proceed with SSH to perform the package installation and coding phase using npm or yarn.
At this stage, you can continue via doctl or directly initiate an SSH request.
doctl compute ssh <droplet-id>
# or
ssh kullanici-adi@ip-adresi
All the steps up to this point were the sections you need to follow when working on a remote server. However, if you intend to perform operations on localhost and/or have already established an SSH connection, you should verify your node.js installation.
node -v
Once you obtain the relevant version information, we can proceed with the module installation. Otherwise, you will need to download and/or configure node.js. I recommend reviewing my article titled What is Node.js?.
Once our installation is complete, we can create our project (I chose the name xml2json, though you may use a different name). Then, we can download the xml2js package.
mkdir xml2json && cd xml2json
npm init -y
npm install xml2js
# or
yarn add xml2js express
Now we can proceed to examine the details of our project3. Let’s start with the most basic operation and read an XML file that we currently have.
const xml2js = require('xml2js');
const fs = require('fs');
const express = require('express');
const parser = new xml2js.Parser();
const app = express();
const PORT = process.env.PORT || 3000;
const XML_file = './test.xml';
let data = fs.readFileSync(XML_file, 'utf8');
let JSON_result = '';
parser.parseString(data, (err, result) => {
if (!err) {
console.log('JSON is ready.');
JSON_result = result;
} else {
console.log(err);
}
});
The above code, in the simplest terms, first assigns the file content to the data variable using fs.readFileSync. Then, if no error occurs, it passes the content of data to JSON_result via parser.parseString.
In the next step, we send the JSON output in response to a GET request received through Express.js via URL.
Now, let’s implement the same process for an actual XML file obtained through a URL. This time, we also need to include the http4 5 / https6 7 package in our modules.
const xml2js = require('xml2js');
const https = require('https'); // or require("http")
const express = require('express');
const parser = new xml2js.Parser();
const app = express();
const PORT = process.env.PORT || 3000;
const XML_url = 'https://alanadi.com/test.xml';
parser.on('error', (err) => {
console.log('Parser error', err);
});
let data = '';
let JSON_result = '';
// or https.get("https://...);
console.log('Checking url...');
https.get(XML_url, (res) => {
if (res.statusCode >= 200 && res.statusCode < 400) {
console.log('XML is parsing...');
res.on('data', (xmldata) => {
data += xmldata.toString();
});
res.on('end', () => {
//console.log('data', data);
parser.parseString(data, (err, result) => {
if (!err) {
console.log('JSON is ready.');
JSON_result = result;
} else {
console.log(err);
}
});
});
}
});
app.get('/', (req, res) => {
res.format({
json: () => {
res.send(JSON_result.Products);
}
});
});
app.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
});
Unlike the previous code, we are now fetching our file using https.get and assigning its content to the JSON_result variable. This operation completes before any request is processed by the web server. If desired, you can also configure this operation to occur when a specific URI is accessed, for example, via Express.js.
Of course, different endpoints can be defined in the above examples to display the JSON output, the content can be listed as an HTML file, or various controls and interventions can be implemented using middleware. On the other hand, using Express.js, the JSON output can also be sent to targets such as the WooCommerce or PIM systems.
In another example, I will explain how we can replicate the actions performed by the XML2WOO extension using the fast-xml-parser8 module through a web application.
Further Reading
- Yusuf Sezer, Node.js XML Creation and Reading (Oct 28, 2019), yusufsezer.com.tr
- Reading XML in Node.js (Jan 3, 2019), usefulangle.com
- Curtis Larson, How to Read, Write, and Update XML with Node.js (Oct 3, 2018), curtismlarson.com
- Tamas Piros, res.json() vs res.send() vs res.end() in Express (Aug 13, 2018), fullstacktraining.com
- Rob Kendal, How to build a RESTful Node.js API server using JSON files, robkendal.co.uk
- Atta-Ur-Rehman Shah, How to convert XML to JSON in Node.js (Apr 15, 2020), attacomsian.com
- Abuzer Asif, NodeJS XML to JSON using XML2JS (Apr 22, 2020), embarkcode.com