У меня есть xml ответ в локальном. xml файле, который мне нужно преобразовать в JSON в React - PullRequest
0 голосов
/ 25 февраля 2020

Я создаю простое приложение реакции, используя create-Reaction-app, где мне нужно прочитать данные из этого xml и отобразить в пользовательском интерфейсе. Я перепробовал почти все XML конвертирующие пакеты, такие как xml2 js, xmlto json stream, xml - js ,, xml2 json, но не смог. xml2 js я пробовал и конвертировал маленькие xml в json, но когда я пробовал свой xml, он не работал с ошибкой:

Error: Unquoted attribute value
Line: 0
Column: 40401
Char: &
    at error (sax.js:651)
    at strictFail (sax.js:677)
    at SAXParser.write (sax.js:1367)
    at Parser.exports.Parser.Parser.parseString (parser.js:323)
    at Parser.parseString (parser.js:5)
    at Object.<anonymous> (Sal.js:22)
    at __webpack_require__ (bootstrap ac9a14d02bd3405bc94c:555)
    at fn (bootstrap ac9a14d02bd3405bc94c:86)
    at Object.<anonymous> (App.js:6)
    at __webpack_require__ (bootstrap ac9a14d02bd3405bc94c:555)

My XML имеет длину 2784 строки и был структурирован как показано ниже:


    <response>
    <header>
    ..
    .
    .
    .
    </header>
    <branches>
    ..
    ..
    </branches>
    <Products>
    <product1>
    ..
    ..
    </product1>
    <product2>
    ...
    </product2>
    ...
    ...
    ...
    </Products>
    </response>

Я в порядке, чтобы прочитать xml как локальный файл или локальную переменную, но каким-то образом мне нужен доступ ко всем деталям поля Products для отображения в пользовательском интерфейсе.

My code
var convert = require('xml-js');
import fs from 'fs'

var xml2 = fs.readFileSync('./xmlText.xml', 'utf8');
var options = {ignoreComment: true, alwaysChildren: true};
var result = convert.xml2js(xml2, options); // or convert.xml2json(xml, options)
console.log(result);

Uncaught TypeError: _fs2.default.readFileSync is not a function
    at Object.<anonymous> (converter.js:25)
    at __webpack_require__ (bootstrap efc8124703a44966d7ce:555)
    at fn (bootstrap efc8124703a44966d7ce:86)
    at Object.<anonymous> (App.js:6)
    at __webpack_require__ (bootstrap efc8124703a44966d7ce:555)
    at fn (bootstrap efc8124703a44966d7ce:86)
    at Object.<anonymous> (index.js:3)
    at __webpack_require__ (bootstrap efc8124703a44966d7ce:555)
    at fn (bootstrap efc8124703a44966d7ce:86)
    at Object.<anonymous> (bootstrap efc8124703a44966d7ce:578)


if i declare xml as a variable i get below error:
sax.js:651 Uncaught Error: Invalid character in entity name
Line: 0
Column: 54128
Char:  
    at error (sax.js:651)
    at strictFail (sax.js:677)
    at SAXParser.write (sax.js:1491)
    at Object.module.exports [as xml2js] (xml2js.js:346)
    at Object.<anonymous> (converter.js:27)
    at __webpack_require__ (bootstrap 3a3dffa4ca814efe6ba5:555)
    at fn (bootstrap 3a3dffa4ca814efe6ba5:86)
    at Object.<anonymous> (App.js:6)
    at __webpack_require__ (bootstrap 3a3dffa4ca814efe6ba5:555)
    at fn (bootstrap 3a3dffa4ca814efe6ba5:86)

1 Ответ

0 голосов
/ 26 февраля 2020

Я достиг этого, когда заменил символы 'и & в своем xml.

var convert = require('xml-js');


var xml = '<myxml></myxml>'
var options = {ignoreComment: true, alwaysChildren: true};
var result = convert.xml2js(xml2, options); // or convert.xml2json(xml, options)
console.log(result);
...