Angular 2 CLI:需要安装xml2json包

时间:2017-05-10 08:48:07

标签: angular angular-cli

刚开始使用angular 2我正在尝试安装xml2json包,但是遇到了一些错误。我已尝试过类似的问题,但没有任何帮助我

下面是我安装的东西 1)节点最新版本 2)python 2 ...还设置了env变量 3)npm install --global --production windows-build-tools 4)npm install node-gyp

但我仍然得到错误。我需要遵循什么顺序来安装xml2json

npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.0.0 (node_modules\chokidar\node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.1.1: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})
npm ERR! Windows_NT 10.0.14393
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "install" "xml2json"
npm ERR! node v7.7.4
npm ERR! npm  v4.1.2
npm ERR! code ELIFECYCLE

npm ERR! node-expat@2.3.15 install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the node-expat@2.3.15 install script 'node-gyp rebuild'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the node-expat package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node-gyp rebuild
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs node-expat
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls node-expat
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     C:\Angular2\XML2JS\npm-debug.log

1 个答案:

答案 0 :(得分:1)

[建议] 由于您使用的是Angular-CLI,因此它已经提供了一个xml实用程序,可以直接使用https://github.com/Leonidas-from-XIV/node-xml2js

因此您无需额外安装任何内容,只需确保在文件中正确导入即可。

Angular 2示例:

import { Component, OnInit } from '@angular/core';
import * as xml2js from 'xml2js';
import * as _ from 'lodash';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  title: string;
  xml = `<?xml version="1.0" encoding="UTF-8" ?>
          <page>
            <title>W3Schools Home Page</title>
          </page>`;

  ngOnInit() {
    const parser = new xml2js.Parser();
    parser.parseString(this.xml, (err, result) => {
        console.log(result);
        // result is your javascript parsed object
        // you can access attribut for example with lodash getters
        this.title = _.first(_.get(result, 'page.title'));
    });
  }
}