How to Loop through XML in JavaScript ? Last Updated : 16 Apr, 2024 Comments Improve Suggest changes Like Article Like Report In JavaScript, looping over XML data is important for processing and extracting information from structured XML documents. Below is a list of methods utilized to loop through XML. Table of Content Using for loop with DOMParser and getElementsByTagNameUsing Array.from with DOMParser and childNodesUsing for loop with DOMParser and getElementsByTagNameIn this approach, we are using a DOMParser from the xmldom module to parse the XML data into a document object. Then, we use getElementsByTagName to select all elements with the tag name 'language' and iterate through them using a for loop, accessing child elements within each 'language' element to retrieve and display their content. Run the below command to install xmldom before running the code: npm install xmldomExample: The below example uses for loop with DOMParser and getElementsByTagName To Loop Through XML in JavaScript. JavaScript let DOMParser = require('xmldom').DOMParser; const xmlData = ` <languages> <language> <name>JavaScript</name> <type>Programming</type> </language> <language> <name>Python</name> <type>Programming</type> </language> <language> <name>Java</name> <type>Programming</type> </language> </languages> `; const parser = new DOMParser(); const xmlDoc = parser. parseFromString(xmlData, 'text/xml'); const lNodes = xmlDoc. getElementsByTagName('language'); for (let i = 0; i < lNodes.length; i++) { const name = lNodes[i]. getElementsByTagName('name')[0].textContent; const type = lNodes[i]. getElementsByTagName('type')[0].textContent; console.log(`Name: ${name}, Type: ${type}`); } Output: Name: JavaScript, Type: ProgrammingName: Python, Type: ProgrammingName: Java, Type: ProgrammingUsing Array.from with DOMParser and childNodesIn this approach, we are using Array.from to convert the NodeList into an array, allowing us to use forEach for iteration. Within the forEach loop, we check if each node is an element node with the name 'language' before extracting and displaying the content of its child elements ('name' and 'type'). Run the below command to install xmldom before running the code: npm install xmldomExample: The below example uses Array.from with DOMParser and childNodes To Loop Through XML in JavaScript. JavaScript const { DOMParser } = require('xmldom'); const xmlData = ` <languages> <language> <name>JavaScript</name> <type>Programming</type> </language> <language> <name>Python</name> <type>Programming</type> </language> <language> <name>Java</name> <type>Programming</type> </language> </languages> `; const parser = new DOMParser(); const xmlDoc = parser. parseFromString(xmlData, 'text/xml'); const lNodes = xmlDoc. documentElement.childNodes; Array.from(lNodes).forEach(node => { if (node.nodeType === node.ELEMENT_NODE && node.nodeName === 'language') { const name = node. getElementsByTagName('name')[0].textContent; const type = node. getElementsByTagName('type')[0].textContent; console.log(`Name: ${name}, Type: ${type}`); } }); Output: Name: JavaScript, Type: ProgrammingName: Python, Type: ProgrammingName: Java, Type: Programming Comment More infoAdvertise with us Next Article How to Loop through XML in JavaScript ? G gauravgandal Follow Improve Article Tags : HTML HTML and XML Similar Reads 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 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 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 Loop through JSON in EJS ? EJS stands for Embedded JavaScript. It is a template engine designed to be used with server-side JavaScript environments like NodeJS and It is used to generate dynamic content in a web page. In this article, we will learn about How to Loop through JSON in EJS with different Approaches. Approaches to 4 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 Create XML in JavaScript ? 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 2 min read Convert XML to JSON using JavaScript Convert XML to JSON effortlessly using JavaScript, enabling seamless integration and manipulation of XML data within your applications. JavaScript libraries like xml-js and xmldom Library simplify the conversion process, ensuring compatibility and efficiency. Below are the approaches to convert XML 2 min read How to Convert HTML to JSON in JavaScript ? Converting HTML to JSON is important for structured data extraction and integration with JavaScript applications. Here, we will learn different approaches to converting HTML to JSON in JavaScript. Below are the approaches to convert html to JSON in JavaScript: Table of Content Using html-to-json Lib 2 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 Validate XML against XSD in JavaScript ? XML (Extensible Markup Language) is a widely used format for storing and exchanging structured data. XSD (XML Schema Definition) is a schema language used to define the structure, content, and data types of XML documents. Validating XML against XSD ensures that the XML document conforms to the speci 4 min read Like