NextJs - How to read XML file data completely?

Viewed 21

In Nextjs, app I am using nextjs-sitemap-generator to generate a sitemap.xml.

I am trying to read this file using fs like,

const fs = require('fs');
var file = 'test/sitemap.xml';
let content = fs.readFileSync(file, 'utf8');
console.log("---content--- ", content);

Here, I am not getting whole file data just getting header part like,

<?xml version="1.0" encoding="UTF-8"?>
  <urlset xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 
  http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" 
  xmlns:xhtml="http://www.w3.org/1999/xhtml">

How can I get XML file data completely using fs or any other package?

I have tried by referring Reading XML file in Node.js but I got this error

Error: There are errors in your xml file: not well-formed (invalid token)Error: There are errors in your xml file: not well-formed (invalid token)

My xml looks like,

<?xml version="1.0" encoding="UTF-8"?>
  <urlset xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 
  http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" 
  xmlns:xhtml="http://www.w3.org/1999/xhtml">
  
  <url>
    <loc>http://localhost/about-us</loc>
    <lastmod>2022-09-12</lastmod>
  </url>
   ...
</urlset>
0 Answers
Related