簡體   English   中英

在打字稿中解析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來說有點過時了,但我把它放在這里供參考: 使用帶有Angular 2的JS庫xml2js

雖然這個問題專門解決了使用該庫的問題,但是關於如何將xml解析/轉換為可在TypeScript中管理的東西的任何其他想法也會派上用場。


更新

正如所建議的那樣,我開始在systemjs配置中添加“主要”定義。 但是,我的部分原因是依賴項將從節點內容中加載/計算出來。

因此修改system.config.js有:

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

將問題移至現在我獲得sax和xmlbuilder的404條目的位置。

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

  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 config中為xml2js指定了一個指向lib/xml2js.js的'main',那么systemjs應該能夠正確找到它。

正如@ tim-perry所指出的,這里的問題是包的定義方式。 我的工作中的另一個問題是我對SystemJS將為我做什么做出錯誤的假設。 我需要研究一個不同的工具(從JSPM開始),它將在加載基礎模塊時執行樹步行。 在我的例子中,lodash正在工作,因為它是自包含的。 但是,xml2js有很多依賴關系,它有很多依賴關系,有很多依賴關系......因此,應用為每個依賴關系添加包定義的解決方案雖然以一種非常麻煩的方式解決了問題。

再次感謝@ tim-perry和@artem的幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM