在打字稿中解析 XML

Jua*_*ina 8 node.js typescript systemjs

使用 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
Run Code Online (Sandbox Code Playgroud)

以下是相关文件:

tsconfig.json:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false,
    "outDir" : "build"
  }
}
Run Code Online (Sandbox Code Playgroud)

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);
Run Code Online (Sandbox Code Playgroud)

消费文件:

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));

  }
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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

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

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

我见过的最接近的解决方案对于 Angular 2.0 来说有点过时,但我把它放在这里以供参考:使用 JS 库 xml2js 和 Angular 2

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


更新

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

因此修改 system.config.js 使其具有:

  xml2js: {
    main: 'lib/xml2js.js', 
    defaultExtension: 'js'
  },
Run Code Online (Sandbox Code Playgroud)

将问题移至现在我为 sax 和 xmlbuilder 获得 404 个条目的位置。

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

  sax: {
    main: 'lib/sax.js',
    defaultExtension: 'js'        
  },
  xmlbuilder: {
    main: 'lib/index.js', 
    defaultExtension: 'js'        
  }
Run Code Online (Sandbox Code Playgroud)

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

我认为使用 SystemJS 和 tsconfig 的整个想法是从节点模块中找出这些,在包上添加所有树依赖似乎违背了目的。

Tim*_*rry 3

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

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