简体   繁体   English

在量角器中使用xml2js

[英]using xml2js in protractor

I am using a protractor application with typescript where I am converting an XML file to JSON format; 我正在使用带有Typescript的量角器应用程序,在其中我将XML文件转换为JSON格式; I have a few questions : 我有几个问题 :

Here is the code in the source.ts file: 这是source.ts文件中的代码:

 {
    var parseString = require('xml2js').parseString;
    var xml = "<root><Copy><Home>true</Home></Copy><More><MoreArray><name>A</name></MoreArray><MoreArray><name>B</name></MoreArray></More></root>"

    parseString(xml, function (err, result) {
    var strinRes = result.root.More[0].MoreArray[0].name;
    console.dir(strinRes.toUpperCase());
    });

}

The output is : TypeError: strinRes.toUpperCase is not a function 输出为:TypeError:strinRes.toUpperCase不是函数

The problem is strinRes is coming as [A] instead of A. This happens only with xml2js library and also "More" is represented as an array instead of an object. 问题是strinRes将以[A]而不是A出现。这仅在xml2js库中发生,并且“ More”表示为数组而不是对象。

Now, how do I parse this and print uppercase of A using same 'xml2js' library? 现在,如何使用相同的“ xml2js”库对此进行解析并打印A的大写字母?

您的name变量是一个数组,请尝试以下操作:

var strinRes = result.root.More[0].MoreArray[0].name[0];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM