How to Create XML in JavaScript ? Last Updated : 17 Apr, 2024 Comments Improve Suggest changes Like Article Like Report In JavaScript, XML documents can be created using various approaches. You can define elements, attributes, and content to structure the XML data, and then serialize it into a string for use or storage. There are several approaches to creating XML in JavaScript which are as follows: Table of Content Using DOMParser and XMLSerializerUsing xmlbuilder2 LibraryUsing DOMParser and XMLSerializerIn this approach, we are using the DOMParser and XMLSerializer from the 'xmldom' package to create an XML document in JavaScript. First, we parse an empty XML string to initialize the document, then we create elements like 'Language' and 'Topic' and append them to the root element. Finally, we serialize the document to a string and use the 'pretty-data' package's pd.xml function to format the XML output. Installtion Syntax:npm install xmldom pretty-dataExample: The below example uses DOMParser and XMLSerializer to Create XML in JavaScript. JavaScript const { DOMParser, XMLSerializer } = require('xmldom'); const { pd } = require('pretty-data'); let xmlDoc = new DOMParser() .parseFromString('<GeeksforGeeks></GeeksforGeeks>', 'text/xml'); let rootEle = xmlDoc .documentElement; let lElement = xmlDoc .createElement('Language'); lElement.textContent = 'JavaScript'; rootEle.appendChild(lElement); let childEle = xmlDoc .createElement('Topic'); childEle.textContent = 'Creating XML in JavaScript'; rootEle.appendChild(childEle); let xmlString = new XMLSerializer() .serializeToString(xmlDoc); let res = pd .xml(xmlString); console.log(res); Output: <GeeksforGeeks> <Language>JavaScript</Language> <Topic>Creating XML in JavaScript</Topic></GeeksforGeeks>Using xmlbuilder2 LibraryIn this approach, we are using the xmlbuilder2 library to create an XML document in JavaScript. We begin by specifying the encoding and version, then add elements like 'GeeksforGeeks' and 'Language' with their respective content, ending with pretty-printing the XML output using pretty-data. Installation Syntax:npm install xmlbuilder2 pretty-dataExample: The below example uses xmlbuilder2 to Create XML in JavaScript. JavaScript const { create } = require('xmlbuilder2'); const { pd } = require('pretty-data'); const xml = create({ encoding: 'UTF-8', version: '1.0' }) .ele('GeeksforGeeks') .ele('Language', 'JavaScript') .up() .ele('Topic', 'Creating_XML_in_JavaScript') .end({ prettyPrint: true }); let res = pd.xml(xml); console.log(res); Output: <?xml version="1.0" encoding="UTF-8"?><GeeksforGeeks> <JavaScript xmlns="Language"/> <Creating_XML_in_JavaScript xmlns="Topic"/> </GeeksforGeeks> Comment More infoAdvertise with us Next Article How to Create XML in JavaScript ? A anjalibo6rb0 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to Create a Link in JavaScript ? In JavaScript, a link typically refers to creating HTML hyperlinks (<a> tags) dynamically using JavaScript. You can link using a document.createElement('a'), setting attributes like href and textContent, and then appending it to the document with appendChild().Here are some common approaches t 4 min read How to Convert XML to JSON in JavaScript? To convert XML to JSON in JavaScript, various methods and libraries and be used. Here, we use xml-js library that provides xml2json function to convert XML to JSON data. It takes XML data as input and gives the JSON objects as output. We can also use the DOMParser from the xmldom package to convert 2 min read How to Parse XML in JavaScript? Parsing XML data is important because it allows JavaScript applications to extract structured information from XML documents. We will explore two different approaches to Parse XML in JavaScript. Below are the approaches to parsing XML in JavaScript: Table of Content Using DOM ParserUsing xml2js Libr 2 min read How to Create a New Line in JavaScript ? A new line can be made in JavaScript using the below-discussed methods.Using Escape Sequence `\n`The \n escape sequence represents a newline character in JavaScript strings and it can used to render a new line in JavaScript.JavaScriptconsole.log("GfG"); console.log("\n"); // New line console.log("Co 2 min read How to Access XML Data via JavaScript ? XML stands for Extensible Markup Language. It is a popular format for storing and exchanging data on the web. It provides a structured way to represent data that is both human-readable and machine-readable. There are various approaches to accessing XML data using JavaScript which are as follows: Tab 2 min read JavaScript- Read CDATA in XML CDATA sections in XML are used to include text that shouldn't be parsed by the XML parser, such as special characters or reserved words. You can read CDATA content in JavaScript by parsing the XML and accessing the content. Parse XML and Read CDATAIn this method, we parse an XML string using the DOM 2 min read How to Validate XML in JavaScript ? Validation of XML is important for ensuring data integrity and adherence to XML standards in JavaScript. There are various approaches available in JavaScript using which validation of the XML can be done which are described as follows: Table of Content Using DOM ParserUsing Tag MatchingUsing DOM Par 2 min read How to Load XML from JavaScript ? Loading XML data into JavaScript is a common task, whether it's for parsing user input or fetching data from a server. The below-listed approaches can be used to load XML from JavaScript. Table of Content Parsing XML String DirectlyFetching XML Data from an External SourceParsing XML in Node.js usin 4 min read How to Fetch XML with Fetch API in JavaScript ? The JavaScript fetch() method retrieves resources from a server and produces a Promise. We will see how to fetch XML data with JavaScript's Fetch API, parse responses into XML documents, and utilize DOM manipulation for streamlined data extraction with different methods. These are the following meth 3 min read How to create XML Dynamically using JavaScript? XML stands for Extensible Markup Language. It is a popular format for storing and exchanging data on the web. It provides a structured way to represent data that is both human-readable and machine-readable. There are various approaches to create XML Dynamically using JavaScript: Table of Content Usi 2 min read Like