英文:
xml2js building xml with one element being an array of objects
问题
以下是已翻译的内容:
我在创建XML中的数组时遇到了问题,其中数组是元素而不是原始类型。
期望的XML如下:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<orderdetails>
.....
<products>
<artnr>05633310000</artnr>
<artnr>05633310000</artnr>
</products>
</orderdetails>
生成的不正确的XML是:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<orderdetails>
<products>
<artnr>05633310000</artnr>
</products>
<products>
<artnr>05633310000</artnr>
</products>
</orderdetails>
我尝试过使用 explicitArray: true,但没有帮助。
您能指导如何获得一个包含多个artnr条目的products元素吗?
英文:
I am having an issue creating an array in XML where the array is one of elements rather than primatives.
The expected XML is:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<orderdetails>
.....
<products>
<artnr>05633310000</artnr>
<artnr>05633310000</artnr>
</products>
</orderdetails>
The incorrect XML produced is:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<orderdetails>
<products>
<artnr>05633310000</artnr>
</products>
<products>
<artnr>05633310000</artnr>
</products>
</orderdetails>
I have tried explicitArray: true but this does not help.
Could you assist in how to get 1 products element with multiple artnr entries?
答案1
得分: 0
To achieve the result you want, you'll need to modify your input data, i.e. group objects by property, which will have as a value an array of values of that property of other objects.
Here's an example:
const xml2js = require('xml2js');
const data = {
orderdetails: {
products: [{
artnr: '05633310000'
}, {
artnr: '05633310000'
}]
};
// create a copy
const clone = JSON.parse(JSON.stringify(data));
// get object property values as an array
clone.orderdetails.products = {
artnr: clone.orderdetails.products.map(({
artnr
}) => artnr)
};
console.log(clone);
/*
{
"orderdetails": {
"products": {
"artnr": ["05633310000", "05633310000"]
}
}
}
*/
const builder = new xml2js.Builder();
const xml = builder.buildObject(clone);
console.log(xml);
/*
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<orderdetails>
<products>
<artnr>778932224</artnr>
<artnr>778932224</artnr>
</products>
</orderdetails>
Note: The code is not translated, as requested.
英文:
To achieve the result you want, you'll need to modify your input data, i.e. group objects by property, which will have as a value an array of values of that property of other objects.
Here's an example:
const xml2js = require('xml2js');
const data = {
orderdetails: {
products: [{
artnr: '05633310000'
}, {
artnr: '05633310000'
}]
}
};
// create a copy
const clone = JSON.parse(JSON.stringify(data));
// get object property values as an array
clone.orderdetails.products = {
artnr: clone.orderdetails.products.map(({
artnr
}) => artnr)
};
console.log(clone);
/*
{
"orderdetails": {
"products": {
"artnr": ["05633310000", "05633310000"]
}
}
}
*/
const builder = new xml2js.Builder();
const xml = builder.buildObject(clone);
console.log(xml);
/*
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<orderdetails>
<products>
<artnr>778932224</artnr>
<artnr>778932224</artnr>
</products>
</orderdetails>
*/
答案2
得分: 0
<?xml version="1.0"?>
<orderdetails>
<products>
<artnr>05633310000</artnr>
<artnr>05633310001</artnr>
</products>
</orderdetails>
英文:
Built the xml using xmlbuilder2.
const { create } = require('xmlbuilder2');
const orderDetails = create().ele('orderdetails');
const productsEl = orderDetails.ele('products');
products.forEach((prod: Product) => {
const artnr = productsEl.ele('artnr');
artnr.txt(prod.artnr).up();
});
const xml = orderDetails.end({ prettyPrint: true });
console.log(xml);
This generated the output
<?xml version="1.0"?>
<orderdetails>
<products>
<artnr>05633310000</artnr>
<artnr>05633310001</artnr>
</products>
</orderdetails>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论