first commit
This commit is contained in:
commit
0b7a32423b
36
dist/convert-xml-to-json.js
vendored
Normal file
36
dist/convert-xml-to-json.js
vendored
Normal file
@ -0,0 +1,36 @@
|
||||
"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
|
||||
1
dist/convert-xml-to-json.js.map
vendored
Normal file
1
dist/convert-xml-to-json.js.map
vendored
Normal file
@ -0,0 +1 @@
|
||||
{"version":3,"file":"convert-xml-to-json.js","sourceRoot":"","sources":["../src/convert-xml-to-json.ts"],"names":[],"mappings":";;;;;;;;;;;AAAA,2BAAiD;AACjD,mCAA4C;AAC5C,IAAI,GAAG,GAAG,aAAa,CAAC;AACxB,MAAM,UAAU,GAAG;IACjB,aAAa,EAAE,KAAK;IACpB,YAAY,EAAE,KAAK;IACnB,OAAO,EAAE,GAAG;IACZ,OAAO,EAAE,GAAG;IACZ,UAAU,EAAE,KAAK;CAClB,CAAC;AAIF,MAAM,gBAAgB,GAAa,CAAO,OAAO,EAAE,QAAQ,EAAE,EAAE;IAC7D,kDAAkD;IAClD,MAAM,OAAO,GAAG,IAAA,iBAAY,EAAC,OAAO,EAAE,EAAE,QAAQ,EAAE,MAAM,EAAE,CAAC,CAAC;IAE5D,MAAM,MAAM,GAAG,MAAM,IAAA,2BAAkB,EAAC,OAAO,EAAE,UAAU,CAAC,CAAC;IAC/D,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;IAClB,MAAM,UAAU,GAAG,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC;IAGnD,IAAA,kBAAa,EAAC,QAAQ,EAAE,UAAU,EAAE,EAAE,QAAQ,EAAE,MAAM,EAAE,CAAC,CAAC;IAC1D,OAAO,CAAC,GAAG,CAAC,qBAAqB,QAAQ,EAAE,CAAC,CAAC;AAC/C,CAAC,CAAA,CAAA;AAED,MAAM,OAAO,GAAG,YAAY,CAAC;AAC7B,MAAM,QAAQ,GAAG,aAAa,CAAC;AAE/B,gBAAgB,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE;IAC9C,OAAO,CAAC,KAAK,CAAC,sBAAsB,EAAE,GAAG,CAAC,CAAC;AAC7C,CAAC,CAAC,CAAC"}
|
||||
3465
input2.xml
Normal file
3465
input2.xml
Normal file
File diff suppressed because one or more lines are too long
50679
output.json
Normal file
50679
output.json
Normal file
File diff suppressed because it is too large
Load Diff
32
src/convert-xml-to-json.ts
Normal file
32
src/convert-xml-to-json.ts
Normal file
@ -0,0 +1,32 @@
|
||||
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);
|
||||
});
|
||||
|
||||
BIN
src/input.xml
Normal file
BIN
src/input.xml
Normal file
Binary file not shown.
12
tsconfig.json
Normal file
12
tsconfig.json
Normal file
@ -0,0 +1,12 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"lib": ["es2015"],
|
||||
"module": "commonjs",
|
||||
"outDir": "dist",
|
||||
"sourceMap": true,
|
||||
"strict": true,
|
||||
"target": "es2015",
|
||||
|
||||
},
|
||||
"include": ["src"]
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user