在我的Vue应用程序中,我获得了一个带有Axios的XML文件,并使用parseString将XML解析为JSON.然后我需要传递result给Vue数据(this.events).我的控制台日志将解析后的XML显示为Json,但我无法在此函数中推送到Vue数据.
var parseString = require('xml2js').parseString;
axios.get(`http://url.to/events.xml`)
.then(response => {
parseString(response.data, function (err, result) {
console.log(result); // returns a json array
this.events = result // nothing happens
});
})
}
Run Code Online (Sandbox Code Playgroud)
如何在Vue中将我的JSON数组存储到this.data?
xml2js <=0.4.23 严重性:高 xml2js 容易受到原型污染 - https://github.com/advisories/GHSA-776f-qx25-q3cc 没有可用的修复 node_modules/xml2js aws-sdk * 取决于 xml2js node_modules 的易受攻击版本/aws-sdk
2个高危漏洞
将 aws-sdk npm 软件包升级到最新版本。但脆弱性仍然存在。
我试图解析一串xml,我收到一个错误
[Error: Invalid character entity
Line: 0
Column: 837
Char: ]
Run Code Online (Sandbox Code Playgroud)
xml不喜欢括号吗?我是否需要用\\]等替换所有括号.谢谢
xml = `
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<GetCurrentRoomStatusGraphResponse xmlns="www.example.com">
...
</GetCurrentRoomStatusGraphResult>
</GetCurrentRoomStatusGraphResponse>
</soap:Body>
</soap:Envelope>
`
var parseString = require('xml2js').parseString;
parseString(xml, function (err, result) {
console.dir(result);
});
Run Code Online (Sandbox Code Playgroud)
我想要另一个函数使用这个值,如何返回这样的值?
parseString(xml, function (err, result) {
return result
});
Run Code Online (Sandbox Code Playgroud) 我正在Node中构建一个服务器,它将搜索文件夹以查看是否存在XML文件(glob),如果存在,则将(fs)中的文件作为JSON对象(xml2js)读取并最终将其存储在数据库中某处.我想要将解析器的结果输出到另一个变量中,以便我可以对数据执行其他操作.从我所知道的情况来看,有些东西是同步运行的,但是我无法弄清楚如何阻止它并让我等到它完成继续前进.
我将我的功能分离到app.js的其他控制器:
app.controller.js
const fs = require('fs-extra');
const glob = require('glob');
const xml2js = require('xml2js');
exports.requests = {};
exports.checkFileDrop = async () => {
console.log('Checking for xml in filedrop...');
// this is the only place await works...
await glob('./filedrop/ALLREQUESTS-*.xml', (err, files) => {
var parser = new xml2js.Parser();
// this is looking for a specific file now, which I'll address later once I can figure out this issue
fs.readFile('./filedrop/ALLREQUESTS-20170707.xml', 'utf16le', function (err, data) {
if (err) {
console.log('ERROR: ', err); …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Express 并解析 XML 正文。我将 bodyparser 用于文本,但无法让 xml2js 将我的 XML 解析为可用格式。
import * as xml2js from 'xml2js';
try {
const XML:string = '<Pan>0000000000000702203</Pan>';
xml2js.parseString(XML, {trim: true}, function (err, result) {
if(err) console.log(err);
console.log(result);
});
} catch (e) {
console.log(e);
}
Run Code Online (Sandbox Code Playgroud)
要么不起作用:
try {
xml2js.parseString(XML, {trim: true}, (err, result) => {
if(err) console.log(err);
console.log(result);
});
} catch (e) {
console.log(e);
}
Run Code Online (Sandbox Code Playgroud)
在 VSCode 调试器中运行它时,代码会立即跳过该函数并且不处理 xml2js.parseString()。错误和结果永远不会得到值。
我也尝试过使用 Parser() 类:
const p:xml2js.Parser = new xml2js.Parser();
p.parseString(XML, (err:{}, result:{}) => {
if(err) console.log(err);
console.log(result); …Run Code Online (Sandbox Code Playgroud) 我有一个带有&的xml文档,因此出现错误
[Error: Invalid character in entity name
Line: 155
Column: 63
Char: ]
Run Code Online (Sandbox Code Playgroud)
我编写了一个函数来转义非法的xml字符:
const escapeIllegalCharacters = (xml) => {
xml = xml
.replace(/&/g,'&')
.replace(/"/g, '"')
.replace(/'/g, ''')
.replace(/>/g, '>')
.replace(/</g, '<');
return (xml);
}
Run Code Online (Sandbox Code Playgroud)
并将其放入valueProcessor中:
return parse.parseString(xml, {valueProcessors: [escapeIllegalCharacters]});
Run Code Online (Sandbox Code Playgroud)
但是我仍然遇到同样的错误。这是使用xml2js模块转义字符的错误方法吗?
对于使用angular2的应用程序(来自带有webpack的angular-cli),我需要从soap web服务中使用xml中的一些数据.我使用的angular-cli本身提供了用于将xml解析为json的库xml2js但是当我尝试使用以下语法在我的组件中使用此库:
import {Parser} from 'xml2js';
Run Code Online (Sandbox Code Playgroud)
我收到了消息:
Cannot find module 'xml2js'.)
Run Code Online (Sandbox Code Playgroud)
但是,此库实际上安装在node_modules文件中.
我到处搜索,但我只找到了一些system.js配置的解决方案.
有没有人知道如何使用webpack在angular2中使用这个库?
我使用以下代码将xml转换为json: -
var parseString = require('xml2js').parseString;
var xml = "<root><param_name>Hello</param_name><param_entry> xml2js! </param_entry> </root>";
parseString(xml, {trim: true},function(err,result){
console.dir(JSON.stringify(result));
});
Run Code Online (Sandbox Code Playgroud)
它返回以下结果 -
{
"root":{
"param_name":[
"Hello"
],
"param_entry":[
" xml2js!"
]
}
}
Run Code Online (Sandbox Code Playgroud)
它返回作为对象集合的值,即"param_name":[
"Hello"].
但我希望它是一个简单的键和价值形式.这是我的结果JSON应该看起来像 -
{
"root":{
"param_name":
"Hello"
,
"param_entry":
" xml2js!"
}
}
Run Code Online (Sandbox Code Playgroud)
这里出了什么问题?
我有一个 xml,其中标记名称包含冒号 (:) 它看起来像这样:
<samlp:Response>
data
</samlp:Response>
Run Code Online (Sandbox Code Playgroud)
我正在使用以下代码将此 xml 解析为 json,但无法使用它,因为标记名称包含冒号。
var xml2js = require('xml2js');
var parser = new xml2js.Parser();
var fs = require('fs');
fs.readFile(
filePath,
function(err,data){
if(!err){
parser.parseString(data, function (err, result) {
//Getting a linter warning/error at this point
console.log(result.samlp:Response);
});
}else{
callback('error while parsing assertion'+err);
}
}
);
};
Run Code Online (Sandbox Code Playgroud)
错误:
events.js:161
throw er; // Unhandled 'error' event
^
TypeError: Cannot read property 'Response' of undefined
Run Code Online (Sandbox Code Playgroud)
如何在不更改 xml 内容的情况下成功解析此 XML?
到目前为止,我无法使用 Angular 6 让 xml2js 代码正常工作。我有一个 service.ts 返回 xml,但是当调用 xml2js.parseString 转换为 json 时,我始终收到未定义的错误。
“core.js:1673错误TypeError:无法读取未定义的属性'parseString'”
服务.ts
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable, of } from 'rxjs';
import { map, filter, catchError, mergeMap } from 'rxjs/operators';
import { HttpHeaders } from '@angular/common/http';
export interface Zid {
zpid: number;
}
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/x-www-form-urlencoded',
}),
responseType: 'text' as 'text'
};
@Injectable({
providedIn: 'root'
})
export class CdataServiceService {
constructor(private …Run Code Online (Sandbox Code Playgroud) xml2js ×12
xml ×7
node.js ×5
angular ×3
javascript ×3
json ×3
typescript ×2
angular-cli ×1
angular5 ×1
angular6 ×1
aws-sdk ×1
axios ×1
ecmascript-6 ×1
fs ×1
npm ×1
python ×1
security ×1
vue.js ×1
xml-parsing ×1