Skip to main content
XML File Using Nodejs xmlbuilder2

How To Create XML File Using Nodejs xmlbuilder2

This tutorial help to create an XML file using nodejs. We’ll use the xmlbuilder2 npm package to create a new file using nodejs.

xmlbuilder2 is a wrapper around DOM nodes which adds chainable functions to make it easier to create and work with XML documents.

How To Create A New XML File

Let’s create an XML file with nodes and attributes using xmlbuilder2.We’ll cover following functionality into this tutorial:

  • Install & import package
  • Created a root element
  • Generate an XML file
  • write and save in a XML file

The target XML file

Create an XML file that has the following content. We’ll create a main.js file and stored into the c:/workspace/create_xml/ folder.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<javascript async="false" continueonerror="false" enabled="true" timelimit="200" name="post-request">
  <displayname>post-request</displayname>
  <properties>
  <resourceurl>jsc://post-request.js</resourceurl>
</properties></javascript>

How To Install xmlbuilder2

You can install the npm packages by running the following command:

npm install xmlbuilder2
npm install fs

if you want to save into package.json

npm install xmlbuilder2 --save
npm install fs --save

How To Install xmlbuilder2

At the top of the main.js file, and added below code into this file to import packages:

const { create } = require('xmlbuilder2');
const fs = require('fs');

Generate Xml File

We’ll generate xml file using following script:

let req_name = post-request;                    
const root = create({ version: '1.0', encoding: "UTF-8", standalone: "yes" })                        
            .ele('Javascript', { async: 'false', continueOnError: "false", enabled: "true", timeLimit: "200", name:req_name})                        
			 .ele('DisplayName').txt(req_name).up()                        
			 .ele('Properties').up()                        
			 .ele('ResourceURL').txt('jsc://'+req_name+'.js').up()                    
			 .up();                    
const xml = root.end({ prettyPrint: true });
console.log(xml);

Write and Save XML File

Let’s write a file using fs module writeFileSync method and saved into the current directory.

let full_file_name = "./" + req_name + ".xml";
fs.writeFileSync(full_file_name, xml, function(err) {
    if (err) throw err;
});

The full Source:

The following is the full source code to create an XML file and save it into the current directory path.

const { create } = require('xmlbuilder2');
const fs = require('fs');

let req_name = 'post-request';                    
const root = create({ version: '1.0', encoding: "UTF-8", standalone: "yes" })                        
            .ele('Javascript', { async: 'false', continueOnError: "false", enabled: "true", timeLimit: "200", name:req_name})                        
			 .ele('DisplayName').txt(req_name).up()                        
			 .ele('Properties').up()                        
			 .ele('ResourceURL').txt('jsc://'+req_name+'.js').up()                    
			 .up();                    
const xml = root.end({ prettyPrint: true });
console.log(xml);
let full_file_name = "./" + req_name + ".xml";
fs.writeFileSync(full_file_name, xml, function(err) {
    if (err) throw err;
});

Output:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<javascript async="false" continueonerror="false" enabled="true" timelimit="200" name="post-request">
  <displayname>post-request</displayname>
  <properties>
  <resourceurl>jsc://post-request.js</resourceurl>
</properties></javascript>

The create() function generates and returns a blank XML document node, while the ele function generates and returns an element node, and the up function returns its parent element node. You can think of up as the element node’s ending tag. The doc function returns the XML document’s document node. The end function finally converts the XML document to its string representation.

Leave a Reply

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