convert_xml_plan_to_json/dist/convert-xml-to-json.js
2026-01-21 18:55:22 +03:00

36 lines
1.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = require("fs");
const xml2js_1 = require("xml2js");
var xml = '<foo></foo>';
const xmlOptions = {
explicitArray: false,
explicitRoot: false,
attrkey: '@',
charkey: '#',
mergeAttrs: false,
};
const convertXmlToJson = (xmlPath, jsonPath) => __awaiter(void 0, void 0, void 0, function* () {
// ✅ use "utf8" the correct BufferEncoding value
const xmlData = (0, fs_1.readFileSync)(xmlPath, { encoding: 'utf8' });
const result = yield (0, xml2js_1.parseStringPromise)(xmlData, xmlOptions);
console.dir(result);
const jsonString = JSON.stringify(result, null, 2);
(0, fs_1.writeFileSync)(jsonPath, jsonString, { encoding: 'utf8' });
console.log(`✅ JSON written to ${jsonPath}`);
});
const xmlFile = 'input2.xml';
const jsonFile = 'output.json';
convertXmlToJson(xmlFile, jsonFile).catch(err => {
console.error('❌ conversion failed:', err);
});
//# sourceMappingURL=convert-xml-to-json.js.map