От Nodejs Xml до Json xml2js - PullRequest
0 голосов
/ 13 июля 2020

https://ctxt.io/2/AADAMQsCEQ Вот мой xml файл

    const fs = require("fs");
    const util = require("util");
    const xml2js = require('xml2js');
    
    
    var xml = fs.readFileSync("random.xml", "utf-8")
    
    function testXmlParse(xml) {
        const parser = new xml2js.Parser({ explicitArray: false, explicitRoot : false })
        parser.parseString(xml, (err, result) => {
            if (err) {
                console.error("An error occurred: ", err);
            } else {
                var lastJson = JSON.stringify(result, null, 2)
                console.log(lastJson);
            }
        })
    }
    
    testXmlParse(xml);

Я использую этот код

https://ctxt.io/2/AADAkWZcEg Вот мой результат но я не хочу видеть root и теги элементов, как я могу исправить?

https://ctxt.io/2/AADAoalVFQ Я хочу, чтобы этот результат был похож на этот результат

1 Ответ

0 голосов
/ 13 июля 2020

Здесь ваш код работает правильно. Смотрите мой файл результатов. Я также изменил явный ключ Root на true при создании парсера, и он также изменился правильно.

{
  "BIREYSEL": {
    "bkBildirimdeBulunanFinansKurulusuSayisi": "2",
    "bkEnEskTakibeAlinmaTarihi": "",
    "bkExclusionCode": "",
    "bkGecikmedekiToplamHesapSayisi": "0",
    "bkGeciktirdigiBakiyeToplami": "0",
    "bkHesapDokumuBeans": {
      "bkHesap": {
        "element": [
          {
            "bkAcilisTarihi": "20190716",
            "bkDovizKodu": "TL",
            "bkEnSonGuncellemeTarihi": "20190915",
            "bkGecikmedekiBakiye": "0",
            "bkHesaptakiKonumu": "1",
            "bkKapanisTarihi": {
              "element": ""
            },
            "bkKayitReferansNo": "7027461580078B",
            "bkKrediKartiTaksitliBakiye": "000000263",
            "bkKrediTuru": "23",
            "bkKrediTutariLimiti": "12250",
            "bkKurumRumuzu": "A",
            "bkLimitKullanimOrani": "0.09",
            "bkOdemePerformansiTarihcesi": "000",
            "bkSiraNo": "1",
            "bkTakibeAlinmaBakiyesi": "0",
            "bkTakipTarihi": "",
            "bkToplamBakiye": "1026",
            "bkToplamGeciktirilmisOdemeSayisi": "00",
            "bkTuketiciKredisiTaksitSayisi": "",
            "bkTuketiciKredisiTaksitTutari": ""
          },
          {
            "bkAcilisTarihi": "20110914",
            "bkDovizKodu": "TL",
            "bkEnSonGuncellemeTarihi": "20190913",
            "bkGecikmedekiBakiye": "0",
            "bkHesaptakiKonumu": "1",
            "bkKapanisTarihi": {
              "element": ""
            },
            "bkKayitReferansNo": "5670737591250I",
            "bkKrediKartiTaksitliBakiye": "000000000",
            "bkKrediTuru": "23",
            "bkKrediTutariLimiti": "3500",
            "bkKurumRumuzu": "B",
            "bkLimitKullanimOrani": "0.00",
            "bkOdemePerformansiTarihcesi": "000000000000000000",
            "bkSiraNo": "2",
            "bkTakibeAlinmaBakiyesi": "0",
            "bkTakipTarihi": "",
            "bkToplamBakiye": "0",
            "bkToplamGeciktirilmisOdemeSayisi": {
              "element": ""
            },
            "bkTuketiciKredisiTaksitSayisi": "",
            "bkTuketiciKredisiTaksitTutari": ""
          }
        ]
      }
    },
    "bkKrediNotu": "1369",
    "bkKrediNotuSebepKodu1": "12",
    "bkKrediNotuSebepKodu2": "06",
    "bkKrediNotuSebepKodu3": "09",
    "bkKrediNotuSebepKodu4": "17",
    "bkMevcutEnUzunGecikmeSuresi": "0",
    "bkSonKrediKullandirimTarihi": "20190716",
    "bkSorguNo": "72626513",
    "bkTakibiDevamEdenKrediSayisi": "0",
    "bkTakipBildirimdeBulunanFinansKurulusuSayisi": "0",
    "bkToplamKrediliHesapSayisi": "2",
    "bkToplamLimit": "15750",
    "bkToplamRisk": "1026",
    "bkToplamTakibeAlinmisKrediSayisi": "0",
    "bkToplamTakipBakiyesi": "0",
    "bkWorstPaymetStatusEver": "0",
    "krsSonucu": "000",
    "krsVeriBulunduMu": "true"
  },
  "TICARI": {
    "krmSonucu": "1",
    "krmVeriBulunduMu": "false",
    "tkBildirimdeBulunanFinansKurulusuSayisi": "0",
    "tkEnEskiTakipTarihi": "",
    "tkEnGuncelLimitTahsisTarihi": "",
    "tkEnYakinTakipTarihi": "",
    "tkGecikmedekiHesapSayisi": "0",
    "tkGecikmisBakiyeToplami": "0",
    "tkGuncelKrediBakiyesiToplami": "0",
    "tkIlkKrediKullandirimTarihi": "",
    "tkLimitRiskBeans": "",
    "tkSonKrediKullandirimTarihi": "",
    "tkTakFaktoringBildirimDonemi": "",
    "tkTakFaktoringCalistigiBankVeDigerMaliKurulusSayisi": "0",
    "tkTakFaktoringFaizReeskontKomisyon": "0",
    "tkTakFaktoringFaizTahakkukuKomisyon": "0",
    "tkTakFaktoringKrediLimiti": "0",
    "tkTakFaktoring_12_24_Ay_Vadeli": "0",
    "tkTakFaktoring_1_12_Ay_Vadeli": "0",
    "tkTakFaktoring_24_Ay_Vadeli": "0",
    "tkTakLeasingBildirimDonemi": "",
    "tkTakLeasingCalistigiBankVeDigerMaliKurulusSayisi": "0",
    "tkTakLeasingFaizReeskontKomisyon": "0",
    "tkTakLeasingFaizTahakkukuKomisyon": "0",
    "tkTakLeasingKrediLimiti": "0",
    "tkTakLeasing_12_24_Ay_Vadeli": "0",
    "tkTakLeasing_1_12_Ay_Vadeli": "0",
    "tkTakLeasing_24_Ay_Vadeli": "0",
    "tkTakibeAlindigiTarihtekiRiskTutarlariToplami": "0",
    "tkTakipHesabiBildirimdeBulunanFinansKurulusuSayisi": "0",
    "tkTokFaktoringBildirimDonemi": "",
    "tkTokFaktoringCalistigiBankVeDigerMaliKurulusSayisi": "0",
    "tkTokFaktoringFaizReeskontKomisyon": "0",
    "tkTokFaktoringFaizTahakkukuKomisyon": "0",
    "tkTokFaktoringKrediLimiti": "0",
    "tkTokFaktoring_12_24_Ay_Vadeli": "0",
    "tkTokFaktoring_1_12_Ay_Vadeli": "0",
    "tkTokFaktoring_24_Ay_Vadeli": "0",
    "tkTokLeasingBildirimDonemi": "",
    "tkTokLeasingCalistigiBankVeDigerMaliKurulusSayisi": "0",
    "tkTokLeasingFaizReeskontKomisyon": "0",
    "tkTokLeasingFaizTahakkukuKomisyon": "0",
    "tkTokLeasingKrediLimiti": "0",
    "tkTokLeasing_12_24_Ay_Vadeli": "0",
    "tkTokLeasing_1_12_Ay_Vadeli": "0",
    "tkTokLeasing_24_Ay_Vadeli": "0",
    "tkToplamDigerLimit": "",
    "tkToplamDigerRisk": "0",
    "tkToplamGayriNakdiLimit": "0",
    "tkToplamGayriNakdiRisk": "0",
    "tkToplamLimit": "0",
    "tkToplamNakdiLimit": "0",
    "tkToplamNakdiRisk": "0",
    "tkToplamRisk": "0"
  },
  "donusDegerleri": {
    "hataKodu": "0000",
    "islemSonucu": "0"
  },
  "ersRaporTuru": "D",
  "raporTur": "FD",
  "referansNo": "603A796EB0",
  "varlikTuru": "G"
}

Я также перечислю свой пакет. json для вас просмотр версий моей библиотеки.

{
  "requires": true,
  "lockfileVersion": 1,
  "dependencies": {
    "array-filter": {
      "version": "1.0.0",
      "resolved": "https://registry.npmjs.org/array-filter/-/array-filter-1.0.0.tgz",
      "integrity": "sha1-uveeYubvTCpMC4MSMtr/7CUfnYM="
    },
    "available-typed-arrays": {
      "version": "1.0.2",
      "resolved": "https://registry.npmjs.org/available-typed-arrays/-/available-typed-arrays-1.0.2.tgz",
      "integrity": "sha512-XWX3OX8Onv97LMk/ftVyBibpGwY5a8SmuxZPzeOxqmuEqUCOM9ZE+uIaD1VNJ5QnvU2UQusvmKbuM1FR8QWGfQ==",
      "requires": {
        "array-filter": "^1.0.0"
      }
    },
    "define-properties": {
      "version": "1.1.3",
      "resolved": "https://registry.npmjs.org/define-properties/-/define-properties-1.1.3.tgz",
      "integrity": "sha512-3MqfYKj2lLzdMSf8ZIZE/V+Zuy+BgD6f164e8K2w7dgnpKArBDerGYpM46IYYcjnkdPNMjPk9A6VFB8+3SKlXQ==",
      "requires": {
        "object-keys": "^1.0.12"
      }
    },
    "es-abstract": {
      "version": "1.17.6",
      "resolved": "https://registry.npmjs.org/es-abstract/-/es-abstract-1.17.6.tgz",
      "integrity": "sha512-Fr89bON3WFyUi5EvAeI48QTWX0AyekGgLA8H+c+7fbfCkJwRWRMLd8CQedNEyJuoYYhmtEqY92pgte1FAhBlhw==",
      "requires": {
        "es-to-primitive": "^1.2.1",
        "function-bind": "^1.1.1",
        "has": "^1.0.3",
        "has-symbols": "^1.0.1",
        "is-callable": "^1.2.0",
        "is-regex": "^1.1.0",
        "object-inspect": "^1.7.0",
        "object-keys": "^1.1.1",
        "object.assign": "^4.1.0",
        "string.prototype.trimend": "^1.0.1",
        "string.prototype.trimstart": "^1.0.1"
      }
    },
    "es-to-primitive": {
      "version": "1.2.1",
      "resolved": "https://registry.npmjs.org/es-to-primitive/-/es-to-primitive-1.2.1.tgz",
      "integrity": "sha512-QCOllgZJtaUo9miYBcLChTUaHNjJF3PYs1VidD7AwiEj1kYxKeQTctLAezAOH5ZKRH0g2IgPn6KwB4IT8iRpvA==",
      "requires": {
        "is-callable": "^1.1.4",
        "is-date-object": "^1.0.1",
        "is-symbol": "^1.0.2"
      }
    },
    "foreach": {
      "version": "2.0.5",
      "resolved": "https://registry.npmjs.org/foreach/-/foreach-2.0.5.tgz",
      "integrity": "sha1-C+4AUBiusmDQo6865ljdATbsG5k="
    },
    "fs": {
      "version": "0.0.1-security",
      "resolved": "https://registry.npmjs.org/fs/-/fs-0.0.1-security.tgz",
      "integrity": "sha1-invTcYa23d84E/I4WLV+yq9eQdQ="
    },
    "function-bind": {
      "version": "1.1.1",
      "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz",
      "integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A=="
    },
    "has": {
      "version": "1.0.3",
      "resolved": "https://registry.npmjs.org/has/-/has-1.0.3.tgz",
      "integrity": "sha512-f2dvO0VU6Oej7RkWJGrehjbzMAjFp5/VKPp5tTpWIV4JHHZK1/BxbFRtf/siA2SWTe09caDmVtYYzWEIbBS4zw==",
      "requires": {
        "function-bind": "^1.1.1"
      }
    },
    "has-symbols": {
      "version": "1.0.1",
      "resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.0.1.tgz",
      "integrity": "sha512-PLcsoqu++dmEIZB+6totNFKq/7Do+Z0u4oT0zKOJNl3lYK6vGwwu2hjHs+68OEZbTjiUE9bgOABXbP/GvrS0Kg=="
    },
    "inherits": {
      "version": "2.0.4",
      "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz",
      "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ=="
    },
    "is-arguments": {
      "version": "1.0.4",
      "resolved": "https://registry.npmjs.org/is-arguments/-/is-arguments-1.0.4.tgz",
      "integrity": "sha512-xPh0Rmt8NE65sNzvyUmWgI1tz3mKq74lGA0mL8LYZcoIzKOzDh6HmrYm3d18k60nHerC8A9Km8kYu87zfSFnLA=="
    },
    "is-callable": {
      "version": "1.2.0",
      "resolved": "https://registry.npmjs.org/is-callable/-/is-callable-1.2.0.tgz",
      "integrity": "sha512-pyVD9AaGLxtg6srb2Ng6ynWJqkHU9bEM087AKck0w8QwDarTfNcpIYoU8x8Hv2Icm8u6kFJM18Dag8lyqGkviw=="
    },
    "is-date-object": {
      "version": "1.0.2",
      "resolved": "https://registry.npmjs.org/is-date-object/-/is-date-object-1.0.2.tgz",
      "integrity": "sha512-USlDT524woQ08aoZFzh3/Z6ch9Y/EWXEHQ/AaRN0SkKq4t2Jw2R2339tSXmwuVoY7LLlBCbOIlx2myP/L5zk0g=="
    },
    "is-generator-function": {
      "version": "1.0.7",
      "resolved": "https://registry.npmjs.org/is-generator-function/-/is-generator-function-1.0.7.tgz",
      "integrity": "sha512-YZc5EwyO4f2kWCax7oegfuSr9mFz1ZvieNYBEjmukLxgXfBUbxAWGVF7GZf0zidYtoBl3WvC07YK0wT76a+Rtw=="
    },
    "is-regex": {
      "version": "1.1.0",
      "resolved": "https://registry.npmjs.org/is-regex/-/is-regex-1.1.0.tgz",
      "integrity": "sha512-iI97M8KTWID2la5uYXlkbSDQIg4F6o1sYboZKKTDpnDQMLtUL86zxhgDet3Q2SriaYsyGqZ6Mn2SjbRKeLHdqw==",
      "requires": {
        "has-symbols": "^1.0.1"
      }
    },
    "is-symbol": {
      "version": "1.0.3",
      "resolved": "https://registry.npmjs.org/is-symbol/-/is-symbol-1.0.3.tgz",
      "integrity": "sha512-OwijhaRSgqvhm/0ZdAcXNZt9lYdKFpcRDT5ULUuYXPoT794UNOdU+gpT6Rzo7b4V2HUl/op6GqY894AZwv9faQ==",
      "requires": {
        "has-symbols": "^1.0.1"
      }
    },
    "is-typed-array": {
      "version": "1.1.3",
      "resolved": "https://registry.npmjs.org/is-typed-array/-/is-typed-array-1.1.3.tgz",
      "integrity": "sha512-BSYUBOK/HJibQ30wWkWold5txYwMUXQct9YHAQJr8fSwvZoiglcqB0pd7vEN23+Tsi9IUEjztdOSzl4qLVYGTQ==",
      "requires": {
        "available-typed-arrays": "^1.0.0",
        "es-abstract": "^1.17.4",
        "foreach": "^2.0.5",
        "has-symbols": "^1.0.1"
      }
    },
    "object-inspect": {
      "version": "1.8.0",
      "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.8.0.tgz",
      "integrity": "sha512-jLdtEOB112fORuypAyl/50VRVIBIdVQOSUUGQHzJ4xBSbit81zRarz7GThkEFZy1RceYrWYcPcBFPQwHyAc1gA=="
    },
    "object-keys": {
      "version": "1.1.1",
      "resolved": "https://registry.npmjs.org/object-keys/-/object-keys-1.1.1.tgz",
      "integrity": "sha512-NuAESUOUMrlIXOfHKzD6bpPu3tYt3xvjNdRIQ+FeT0lNb4K8WR70CaDxhuNguS2XG+GjkyMwOzsN5ZktImfhLA=="
    },
    "object.assign": {
      "version": "4.1.0",
      "resolved": "https://registry.npmjs.org/object.assign/-/object.assign-4.1.0.tgz",
      "integrity": "sha512-exHJeq6kBKj58mqGyTQ9DFvrZC/eR6OwxzoM9YRoGBqrXYonaFyGiFMuc9VZrXf7DarreEwMpurG3dd+CNyW5w==",
      "requires": {
        "define-properties": "^1.1.2",
        "function-bind": "^1.1.1",
        "has-symbols": "^1.0.0",
        "object-keys": "^1.0.11"
      }
    },
    "safe-buffer": {
      "version": "5.2.1",
      "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz",
      "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ=="
    },
    "sax": {
      "version": "1.2.4",
      "resolved": "https://registry.npmjs.org/sax/-/sax-1.2.4.tgz",
      "integrity": "sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw=="
    },
    "string.prototype.trimend": {
      "version": "1.0.1",
      "resolved": "https://registry.npmjs.org/string.prototype.trimend/-/string.prototype.trimend-1.0.1.tgz",
      "integrity": "sha512-LRPxFUaTtpqYsTeNKaFOw3R4bxIzWOnbQ837QfBylo8jIxtcbK/A/sMV7Q+OAV/vWo+7s25pOE10KYSjaSO06g==",
      "requires": {
        "define-properties": "^1.1.3",
        "es-abstract": "^1.17.5"
      }
    },
    "string.prototype.trimstart": {
      "version": "1.0.1",
      "resolved": "https://registry.npmjs.org/string.prototype.trimstart/-/string.prototype.trimstart-1.0.1.tgz",
      "integrity": "sha512-XxZn+QpvrBI1FOcg6dIpxUPgWCPuNXvMD72aaRaUQv1eD4e/Qy8i/hFTe0BUmD60p/QA6bh1avmuPTfNjqVWRw==",
      "requires": {
        "define-properties": "^1.1.3",
        "es-abstract": "^1.17.5"
      }
    },
    "util": {
      "version": "0.12.3",
      "resolved": "https://registry.npmjs.org/util/-/util-0.12.3.tgz",
      "integrity": "sha512-I8XkoQwE+fPQEhy9v012V+TSdH2kp9ts29i20TaaDUXsg7x/onePbhFJUExBfv/2ay1ZOp/Vsm3nDlmnFGSAog==",
      "requires": {
        "inherits": "^2.0.3",
        "is-arguments": "^1.0.4",
        "is-generator-function": "^1.0.7",
        "is-typed-array": "^1.1.3",
        "safe-buffer": "^5.1.2",
        "which-typed-array": "^1.1.2"
      }
    },
    "which-typed-array": {
      "version": "1.1.2",
      "resolved": "https://registry.npmjs.org/which-typed-array/-/which-typed-array-1.1.2.tgz",
      "integrity": "sha512-KT6okrd1tE6JdZAy3o2VhMoYPh3+J6EMZLyrxBQsZflI1QCZIxMrIYLkosd8Twf+YfknVIHmYQPgJt238p8dnQ==",
      "requires": {
        "available-typed-arrays": "^1.0.2",
        "es-abstract": "^1.17.5",
        "foreach": "^2.0.5",
        "function-bind": "^1.1.1",
        "has-symbols": "^1.0.1",
        "is-typed-array": "^1.1.3"
      }
    },
    "xml2js": {
      "version": "0.4.23",
      "resolved": "https://registry.npmjs.org/xml2js/-/xml2js-0.4.23.tgz",
      "integrity": "sha512-ySPiMjM0+pLDftHgXY4By0uswI3SPKLDw/i3UXbnO8M/p28zqexCUoPmQFrYD+/1BzhGJSs2i1ERWKJAtiLrug==",
      "requires": {
        "sax": ">=0.6.0",
        "xmlbuilder": "~11.0.0"
      }
    },
    "xmlbuilder": {
      "version": "11.0.1",
      "resolved": "https://registry.npmjs.org/xmlbuilder/-/xmlbuilder-11.0.1.tgz",
      "integrity": "sha512-fDlsI/kFEx7gLvbecc0/ohLG50fugQp8ryHzMTuW9vSa1GJ0XYWKnhsUx7oie3G98+r56aTQIUB4kht42R3JvA=="
    }
  }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...