JavaScript- Read CDATA in XML Last Updated : 23 Dec, 2024 Comments Improve Suggest changes Like Article Like Report 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 DOMParser and access the content inside the CDATA section. CDATA sections are used to include raw text without being parsed by the XML parser. HTML <!DOCTYPE html> <html lang="en"> <body> <script> const xmlString = ` <note> <to>John</to> <from>Jane</from> <message><![CDATA[Hello, this is a CDATA section!]]></message> </note> `; const parser = new DOMParser(); const xmlDoc = parser.parseFromString(xmlString, "text/xml"); const cdataContent = xmlDoc.getElementsByTagName("message")[0].childNodes[0].nodeValue; console.log(cdataContent); // Output: Hello, this is a CDATA section! </script> </body> </html> Fetch XML File and Read CDATAIn this method, you load an external XML file using the fetch API and then parse the XML to extract the content inside the CDATA section. This is useful when you need to work with XML data hosted remotely. HTML <!DOCTYPE html> <html lang="en"> <body> <script> fetch("example.xml") .then(response => response.text()) .then(data => { const parser = new DOMParser(); const xmlDoc = parser.parseFromString(data, "text/xml"); const cdataContent = xmlDoc.getElementsByTagName("message")[0].childNodes[0].nodeValue; console.log(cdataContent); }) .catch(error => console.error("Error fetching XML:", error)); </script> </body> </html> Use XPath to Locate CDATAIn this method, XPath is used to directly locate and extract the content of the CDATA section. XPath allows you to query XML documents with more precision, making it easier to extract specific elements or text. HTML <!DOCTYPE html> <html lang="en"> <body> <script> const xmlString = ` <note> <to>John</to> <from>Jane</from> <message><![CDATA[Hello, this is a CDATA section!]]></message> </note> `; const parser = new DOMParser(); const xmlDoc = parser.parseFromString(xmlString, "text/xml"); const xpathResult = xmlDoc.evaluate("//message/text()", xmlDoc, null, XPathResult.STRING_TYPE, null); console.log(xpathResult.stringValue); </script> </body> </html> Comment More infoAdvertise with us Next Article JavaScript- Read CDATA in XML A anujpaz9pe Follow Improve Article Tags : JavaScript Web Technologies 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 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 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 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 XML - CDATA Sections CDATA sections are a mechanism in XML for handling character data that might otherwise be misinterpreted by the XML parser. CDATA stands for Character Data. These sections include blocks of text within an XML document that the parser should treat literally, without interpreting any characters as XML 2 min read Like