我在本地 .xml 文件中有一个 xml 响应,我需要在 React 中将其转换为 JSON
I have an xml response in a local .xml file which i need to convert to JSON in React
我正在使用 create-react-app 构建一个简单的 React 应用程序,我需要从中读取数据 xml 并显示在 UI 中。我已经尝试了几乎所有 XML 转换包,例如 xml2js,xmltojson stream,xml-js,xml2json 但失败了。 xml2js 我已经尝试将小 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)
我的 XML 长 2784 行,结构如下:
<response>
<header>
..
.
.
.
</header>
<branches>
..
..
</branches>
<Products>
<product1>
..
..
</product1>
<product2>
...
</product2>
...
...
...
</Products>
</response>
我可以将 xml 作为本地文件或本地变量读取,但不知何故我需要访问所有产品字段详细信息以显示在 UI。
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)
当我替换 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);
我正在使用 create-react-app 构建一个简单的 React 应用程序,我需要从中读取数据 xml 并显示在 UI 中。我已经尝试了几乎所有 XML 转换包,例如 xml2js,xmltojson stream,xml-js,xml2json 但失败了。 xml2js 我已经尝试将小 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)
我的 XML 长 2784 行,结构如下:
<response>
<header>
..
.
.
.
</header>
<branches>
..
..
</branches>
<Products>
<product1>
..
..
</product1>
<product2>
...
</product2>
...
...
...
</Products>
</response>
我可以将 xml 作为本地文件或本地变量读取,但不知何故我需要访问所有产品字段详细信息以显示在 UI。
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)
当我替换 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);