【发布时间】:2017-07-28 18:20:30
【问题描述】:
我想生成一个类似于以下内容的 XML 文档:
<?xml version="1.0" encoding="UTF-8"?>
<Documents>
<Document>
<ih_account><![CDATA[WEB100]]></ih_account>
<DocLine>
<quan><![CDATA[1]]></quan>
<sellprice><![CDATA[7.4916666666667]]></sellprice>
</DocLine>
<DocLine>
<quan><![CDATA[1]]></quan>
<sellprice><![CDATA[8.325]]></sellprice>
</DocLine>
<DocLine>
<it_desc><![CDATA[Shipping]]></it_desc>
<quan><![CDATA[1]]></quan>
<sellprice><![CDATA[3.99]]></sellprice>
</DocLine>
</Document>
</Documents>
我正在使用node-xmlhttps://www.npmjs.com/package/xml,这是我目前拥有的代码
var xml = require('xml');
var order = {_id: 'WEB100', items: [{price: 2450, quantity: 10},{price: 2480, quantity: 10, },{ price: 100, quantity: 10 }]};
var createXML;
createXML = function(order) {
var items, options, orderObject, ordered, toXML;
options = {
declaration: {
version: "1.0",
encoding: "UTF-8"
}
};
items = order.items.ordered;
ordered = items.map(function(it) {
return {
DocLine: [
{
quan: {
_cdata: it.quantity
}
}, {
sellprice: {
_cdata: it.price
}
}
]
};
});
orderObject = {
OperaDocuments: [
{
Document: [
{
ih_account: {
_cdata: order._id
}
}
]
}
]
};
orderObject.OperaDocuments.Document = ordered;
toXML = XML([orderObject], options);
return console.log(toXML);
};
但这就是我要返回的:
<?xml version="1.0" encoding="UTF-8"?>
<OperaDocuments>
<Document>
<ih_account><![CDATA[WEB100]]></ih_account>
</Document>
</OperaDocuments>
我如何包含Docline 项目?非常感谢任何建议。
【问题讨论】:
-
<![CDATA[8.325]]>wat -
这是一个问题吗?
标签: javascript json xml