[英]How do you add attribute to array element with xml2js?
I'm trying to figure out how to add attributes to an array element我想弄清楚如何向数组元素添加属性
I get an error Error: Invalid character in name
when trying to build the following XML from an object.尝试从对象构建以下 XML 时,出现
Error: Invalid character in name
。
<?xml version="1.0" encoding="UTF-8"?>
<Requests xmlns="http://example.com">
<Request>
</Request>
<Request>
</Request>
</Requests>
Here is my object这是我的对象
let myObject = {
Requests:[
{$:{xmlns:"http://example.com"}},
{Request:{}},
{Request:{}}
]
}
let builder = new xml2js.Builder();
let xml = builder.buildObject(myObject);
console.log(xml)
I've also tried wrapping the name in quotes我也试过用引号括起名字
{"$":{"xmlns":"http://example.com"}},
Stripping out the attribute declaration altogether produces the desired XML but obviously leaves out the needed attribute完全剥离属性声明会生成所需的 XML,但显然会遗漏所需的属性
<?xml version="1.0" encoding="UTF-8"?>
<Requests>
<Request>
</Request>
<Request>
</Request>
</Requests>
I suppose you should try this:我想你应该试试这个:
let myObject = {
Requests:{
$:{xmlns:"http://example.com"},
Request:[{}, {}]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.