33 lines
848 B
TypeScript
33 lines
848 B
TypeScript
import { readFileSync, writeFileSync } from 'fs';
|
|
import { parseStringPromise } from 'xml2js';
|
|
|
|
const xmlOptions = {
|
|
explicitArray: false,
|
|
explicitRoot: false,
|
|
attrkey: '@',
|
|
charkey: '#',
|
|
mergeAttrs: false,
|
|
};
|
|
|
|
type Converts = (xmlPath: string, jsonPath: string) => Promise<void>;
|
|
|
|
const convertXmlToJson: Converts = async (xmlPath, jsonPath) => {
|
|
const xmlData = readFileSync(xmlPath, { encoding: 'utf8' });
|
|
|
|
const result = await parseStringPromise(xmlData, xmlOptions);
|
|
console.dir(result);
|
|
const jsonString = JSON.stringify(result, null, 2);
|
|
|
|
|
|
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);
|
|
});
|
|
|