在打字稿中解析 XML
Parsing XML in typescript
使用 Typescript 2.0(tsc 版本 2.0.3)。
我尝试在 angular2/typescript 应用程序中使用 xml2js 节点库但未成功(请参阅 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"
}
}
systemjs.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 来说有点过时了,但我把它放在这里以供参考:
虽然这个问题专门解决了使用该库的问题,但关于如何 parse/convert xml 进入 TypeScript 内部可管理的东西的任何其他想法也会派上用场。
更新:
按照建议,我开始向我的 systemjs 配置添加 "main" 定义。但是,我认为其中的一部分是依赖项将从节点内容中 loaded/figured 出来。
因此将 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 的整个想法是,这些将从节点模块中计算出来,将所有树依赖项添加到包上似乎违背了目的。
我认为这里的问题出在您的 systemjs 配置中 - 您没有为 xml2js 定义 main
。因为 systemjs 不知道要加载哪个文件,所以出于某种原因尝试直接加载 NPM 目录(加载 /node_modules/xml2js 作为文件,这显然是行不通的)。请注意,您的 lodash 配置确实指定了 'index.js',可能会导致在您导入 lodash 时加载 /node_modules/lodash/index.js,这就是它起作用的原因。
如果您在 systemjs 配置中为指向 lib/xml2js.js
的 xml2js 指定 'main',那么 systemjs 应该能够正确找到它。
如@tim-perry 所述,这里的问题是包的定义方式。
我工作中的另一个问题是,我对 SystemJS 会为我做什么做出了错误的假设。我需要研究一个不同的工具(从 JSPM 开始),它会在加载基本模块时进行树遍历。在我的示例中,lodash 之所以有效,是因为它是独立的。然而,xml2js 有很多依赖关系,有很多依赖关系,有很多依赖关系……因此应用为每个依赖项添加包定义的解决方案解决了这个问题,尽管是以一种非常麻烦的方式。
再次感谢@tim-perry 和@artem 的帮助。
使用 Typescript 2.0(tsc 版本 2.0.3)。
我尝试在 angular2/typescript 应用程序中使用 xml2js 节点库但未成功(请参阅 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"
}
}
systemjs.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 来说有点过时了,但我把它放在这里以供参考:
虽然这个问题专门解决了使用该库的问题,但关于如何 parse/convert xml 进入 TypeScript 内部可管理的东西的任何其他想法也会派上用场。
更新:
按照建议,我开始向我的 systemjs 配置添加 "main" 定义。但是,我认为其中的一部分是依赖项将从节点内容中 loaded/figured 出来。
因此将 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 的整个想法是,这些将从节点模块中计算出来,将所有树依赖项添加到包上似乎违背了目的。
我认为这里的问题出在您的 systemjs 配置中 - 您没有为 xml2js 定义 main
。因为 systemjs 不知道要加载哪个文件,所以出于某种原因尝试直接加载 NPM 目录(加载 /node_modules/xml2js 作为文件,这显然是行不通的)。请注意,您的 lodash 配置确实指定了 'index.js',可能会导致在您导入 lodash 时加载 /node_modules/lodash/index.js,这就是它起作用的原因。
如果您在 systemjs 配置中为指向 lib/xml2js.js
的 xml2js 指定 'main',那么 systemjs 应该能够正确找到它。
如@tim-perry 所述,这里的问题是包的定义方式。 我工作中的另一个问题是,我对 SystemJS 会为我做什么做出了错误的假设。我需要研究一个不同的工具(从 JSPM 开始),它会在加载基本模块时进行树遍历。在我的示例中,lodash 之所以有效,是因为它是独立的。然而,xml2js 有很多依赖关系,有很多依赖关系,有很多依赖关系……因此应用为每个依赖项添加包定义的解决方案解决了这个问题,尽管是以一种非常麻烦的方式。
再次感谢@tim-perry 和@artem 的帮助。