const fs = require('fs'); const xml2js = require('xml2js'); const {EPub} = require('epub2'); // 读取 toc.ncx 文件 fs.readFile('./test2_副本/OEBPS/toc.ncx', 'utf8', (err, data) => { if (err) { console.error("Error reading the file:", err); return; } // 使用 xml2js 解析 XML 数据 const parser = new xml2js.Parser({ explicitArray: false, mergeAttrs: true }); parser.parseString(data, (err, result) => { if (err) { console.error("Error parsing the XML:", err); return; } console.log(20, result); // 转换结果为 JSON 格式 const jsonResult = JSON.stringify(result, null, 2); // 输出 JSON 数据 console.log(jsonResult); // 如果需要将 JSON 保存为文件,可以使用 fs.writeFile() fs.writeFile('output.json', jsonResult, (err) => { if (err) { console.error("Error writing JSON file:", err); } else { console.log("JSON data saved to output.json"); } }); }); }); // 读取 OEBPS/Text/chapter396.html 文件 // fs.readFile('./OEBPS/Text/chapter396.html', 'utf8', (err, data) => { // if (err) { // console.error("Error reading the file:", err); // return; // } // console.log(4545, data); // // 使用 xml2js 解析 XML 数据 // /* const parser = new xml2js.Parser({ explicitArray: false, mergeAttrs: true }); // parser.parseString(data, (err, result) => { // if (err) { // console.error("Error parsing the XML:", err); // return; // } // // 转换结果为 JSON 格式 // const jsonResult = JSON.stringify(result, null, 2); // // 输出 JSON 数据 // console.log(jsonResult); // // 如果需要将 JSON 保存为文件,可以使用 fs.writeFile() // fs.writeFile('output.json', jsonResult, (err) => { // if (err) { // console.error("Error writing JSON file:", err); // } else { // console.log("JSON data saved to output.json"); // } // }); // }); */ // }); // (async () => { // const epub = await EPub.createAsync('/Users/honghaitao/code/chm_py/test.epub', null,''); // let imgs = epub.listImage(); // console.log(imgs); // })()