gpt4 book ai didi

node.js - 在 typescript 中解析 XML

转载 作者:搜寻专家 更新时间:2023-10-30 21:01:12 26 4
gpt4 key购买 nike

使用 Typescript 2.0(tsc 版本 2.0.3)。

我未能成功尝试在 angular2/typescript 应用程序中使用 xml2js Node 库(请参阅 https://www.npmjs.com/package/xml2js)。很明显我对 SystemJS 设置库以供使用的方式不够熟悉。

为了安装 xml2js 库,我执行了以下操作:

npm install --save xml2js
npm install --save @types/xml2js

相关文件如下:

tsconfig.json:

{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false,
"outDir" : "build"
}
}

系统js.config.js

/**
* System configuration for Angular samples
* Adjust as necessary for your application needs.
*/
(function (global) {
System.config({
paths: {
// paths serve as alias
'npm:': 'node_modules/'
},
// map tells the System loader where to look for things
map: {
// our app is within the app folder
app: 'build',
// angular bundles
'@angular/core': 'npm:@angular/core/bundles/core.umd.js',
'@angular/common': 'npm:@angular/common/bundles/common.umd.js',
'@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
'@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
'@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
'@angular/http': 'npm:@angular/http/bundles/http.umd.js',
'@angular/router': 'npm:@angular/router/bundles/router.umd.js',
'@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
'@angular/upgrade': 'npm:@angular/upgrade/bundles/upgrade.umd.js',
// other libraries
'rxjs': 'npm:rxjs',
'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js',
'lodash' : 'npm:lodash',
'xml2js' : 'npm:xml2js'
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
app: {
main: './main.js',
defaultExtension: 'js'
},
rxjs: {
defaultExtension: 'js'
},
lodash: {
main : 'index.js',
defaultExtension: 'js'
},
xml2js: {
defaultExtension: 'js'
}
}
});
})(this);

消费文件:

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/Rx';
import * as _ from 'lodash';
import * as xml2js from 'xml2js';
@Injectable()
export class CatalogService {
constructor (private http:Http) {
}
getCatalog() {
//return Promise.resolve(['This', 'That']);
var xml = "<root>Hello xml2js!</root>"
xml2js.parseString(xml, function (err, result) {
alert(result);
});

alert(_.indexOf([1, 2, 1, 2], 2));

}
}

index.html 文件

<html>
<head>
<title>Angular QuickStart</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="styles.css">
<!-- 1. Load libraries -->
<!-- Polyfill for older browsers -->
<script src="node_modules/core-js/client/shim.min.js"></script>
<script src="node_modules/zone.js/dist/zone.js"></script>
<script src="node_modules/reflect-metadata/Reflect.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>
<!-- 2. Configure SystemJS -->
<script src="systemjs.config.js"></script>
<script>
System.import('app').catch(function(err){ console.error(err); });
</script>
</head>
<!-- 3. Display the application -->
<body>
<dsa-app>Loading DSA App...</dsa-app>
</body>
</html>

使用该设置,我确实收到如下错误:

zone.js:1382 GET http://localhost:3000/node_modules/xml2js/ 404(未找到)

lodash 库加载正常,所以我很确定这里的问题与 xml2js 库的定义方式有关,而不是我的设置。

我看到的最接近的解决方案对于 Angular 2.0 来说有点过时了,但我把它放在这里以供引用:Use JS library xml2js with Angular 2

虽然这个问题专门针对使用该库,但关于如何将 xml 解析/转换为 TypeScript 中可管理的东西的任何其他想法也会派上用场。


更新:

按照建议,我开始将“主要”定义添加到我的 systemjs 配置中。但是,我认为部分原因是依赖项将从 Node 内容中加载/计算出来。

因此将 system.config.js 修改为:

  xml2js: {
main: 'lib/xml2js.js',
defaultExtension: 'js'
},

将问题移到现在我得到 404 个 sax 和 xmlbuilder 条目的地方。

因此,我也添加了以下内容:

  sax: {
main: 'lib/sax.js',
defaultExtension: 'js'
},
xmlbuilder: {
main: 'lib/index.js',
defaultExtension: 'js'
}

它解决了 sax 和 xmlbuilder,但随后会弹出更多错误。

我认为使用 SystemJS 和 tsconfig 的整个想法是,这些将从 Node 模块中计算出来,将所有树依赖项添加到包上似乎违背了目的。

最佳答案

我认为这里的问题出在您的 systemjs 配置中 - 您还没有为 xml2js 定义 main。因为 systemjs 不知道要加载哪个文件,所以出于某种原因尝试直接加载 NPM 目录(将/node_modules/xml2js 作为文件加载,这显然是行不通的)。请注意,您的 lodash 配置确实指定了“index.js”,可能会导致在您导入 lodash 时加载/node_modules/lodash/index.js,这就是它起作用的原因。

如果您在 systemjs 配置中为 xml2js 指定一个指向 lib/xml2js.js 的“main”,那么 systemjs 应该能够正确找到它。

关于node.js - 在 typescript 中解析 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40850630/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com