xml2js构建XML时,一个元素是对象数组。

huangapple go评论66阅读模式
英文:

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:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;yes&quot;?&gt;
&lt;orderdetails&gt;
 .....
&lt;products&gt;
 &lt;artnr&gt;05633310000&lt;/artnr&gt;
 &lt;artnr&gt;05633310000&lt;/artnr&gt;
&lt;/products&gt;
&lt;/orderdetails&gt;

The incorrect XML produced is:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;yes&quot;?&gt;
&lt;orderdetails&gt;
&lt;products&gt;
 &lt;artnr&gt;05633310000&lt;/artnr&gt;
&lt;/products&gt;
&lt;products&gt;
 &lt;artnr&gt;05633310000&lt;/artnr&gt;
&lt;/products&gt;

</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(&#39;xml2js&#39;);
const data = {
	orderdetails: {
		products: [{
			artnr: &#39;05633310000&#39;
		}, {
			artnr: &#39;05633310000&#39;
		}]
	}
};

// 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
	}) =&gt; artnr)
};

console.log(clone);
/*
{
	&quot;orderdetails&quot;: {
		&quot;products&quot;: {
			&quot;artnr&quot;: [&quot;05633310000&quot;, &quot;05633310000&quot;]
		}
	}
}
 */

const builder = new xml2js.Builder();
const xml = builder.buildObject(clone);

console.log(xml);

/*
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;yes&quot;?&gt;
&lt;orderdetails&gt;
  &lt;products&gt;
    &lt;artnr&gt;778932224&lt;/artnr&gt;
    &lt;artnr&gt;778932224&lt;/artnr&gt;
  &lt;/products&gt;
&lt;/orderdetails&gt;
 */

答案2

得分: 0

<?xml version="1.0"?>
<orderdetails>
  <products>
    <artnr>05633310000</artnr>
    <artnr>05633310001</artnr>
  </products>
</orderdetails>
英文:

Built the xml using xmlbuilder2.

const { create } = require(&#39;xmlbuilder2&#39;);
const orderDetails = create().ele(&#39;orderdetails&#39;);

const productsEl = orderDetails.ele(&#39;products&#39;);
products.forEach((prod: Product) =&gt; {
  const artnr = productsEl.ele(&#39;artnr&#39;);
  artnr.txt(prod.artnr).up();
});
const xml = orderDetails.end({ prettyPrint: true });
console.log(xml);

This generated the output

&lt;?xml version=&quot;1.0&quot;?&gt;
 &lt;orderdetails&gt;
  &lt;products&gt;
   &lt;artnr&gt;05633310000&lt;/artnr&gt;
   &lt;artnr&gt;05633310001&lt;/artnr&gt;
  &lt;/products&gt;
 &lt;/orderdetails&gt;

huangapple
  • 本文由 发表于 2023年4月11日 14:01:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75982798.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定