0

我试图弄清楚如何将属性添加到数组元素

Error: Invalid character in name尝试从对象构建以下 XML 时出现错误。

<?xml version="1.0" encoding="UTF-8"?>
<Requests xmlns="http://example.com">
    <Request>
    </Request>
    <Request>
    </Request>
</Requests>

这是我的对象

let myObject = {
 Requests:[
   {$:{xmlns:"http://example.com"}},
   {Request:{}},
   {Request:{}}
  ]
}
let builder = new xml2js.Builder();
let xml = builder.buildObject(myObject);
console.log(xml)

我也试过用引号括起来的名字

{"$":{"xmlns":"http://example.com"}},

完全剥离属性声明会产生所需的 XML,但显然会遗漏所需的属性

<?xml version="1.0" encoding="UTF-8"?>
<Requests>
    <Request>
    </Request>
    <Request>
    </Request>
</Requests>
4

1 回答 1

0

我想你应该试试这个:

let myObject = {
 Requests:{
   $:{xmlns:"http://example.com"},
   Request:[{}, {}]
}
于 2020-09-10T17:38:53.570 回答