[英]Create XML using array of JSON values as input - Javascript
我有一個 JSON 值(文章編號)的數組,其大小可以從 1 到幾百個項目(文章編號)不等。
基於該數組,我需要創建一個 XML,其中需要輸入部分商品編號。 每篇文章的前 6 位數字始終相同,並且應位於標簽 ipc:familyVersion 下。 然后,對於每篇文章,接下來的 4 位數字應位於 ipc:characteristic 下,最后 3 位數字應位於 ipc:value 下。 xml 應該為每個商品編號生成一個 ipc:charValue 標簽。
This is the JSON array (6 items as an example, but always varies)
[
'117101-1100-005',
'117101-1640-015',
'117101-1650-005',
'117101-5100-005',
'117101-7850-040',
'117101-7900-005'
]
This is the XML i would need to produce (desired output):
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ipc="http://kiongroup.com/sapIpcService">
<soapenv:Header/>
<soapenv:Body>
<ipc:sapIpcRequest>
<ipc:wsTransferBean>
<ipc:function>i</ipc:function>
<ipc:language>en</ipc:language>
<ipc:familyVersion>**117101**</ipc:familyVersion>
<ipc:style>s</ipc:style>
<ipc:charValue>
<ipc:characteristic>**1100**</ipc:characteristic>
<ipc:value>**105**</ipc:value>
</ipc:charValue>
<ipc:charValue>
<ipc:characteristic>1640</ipc:characteristic>
<ipc:value>015</ipc:value>
</ipc:charValue>
<ipc:charValue>
<ipc:characteristic>1650</ipc:characteristic>
<ipc:value>005</ipc:value>
</ipc:charValue>
<ipc:charValue>
<ipc:characteristic>5100</ipc:characteristic>
<ipc:value>005</ipc:value>
</ipc:charValue>
<ipc:charValue>
<ipc:characteristic>7850</ipc:characteristic>
<ipc:value>040</ipc:value>
</ipc:charValue>
<ipc:charValue>
<ipc:characteristic>7900</ipc:characteristic>
<ipc:value>005</ipc:value>
</ipc:charValue>
</ipc:wsTransferBean>
</ipc:sapIpcRequest>
</soapenv:Body>
</soapenv:Envelope>
This is the XSD:
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://kiongroup.com/sapIpcService" targetNamespace="http://kiongroup.com/sapIpcService" elementFormDefault="qualified">
<element name="sapIpcRequest">
<complexType>
<sequence>
<element name="wsTransferBean" type="tns:wsTransferBeanType" maxOccurs="1" minOccurs="1" />
</sequence>
</complexType>
</element>
<complexType name="wsTransferBeanType">
<sequence>
<element name="function" maxOccurs="1" minOccurs="0" type="string" />
<element name="language" maxOccurs="1" minOccurs="0" type="string" />
<element name="familyVersion" maxOccurs="1" minOccurs="1" type="string" />
<element name="style" maxOccurs="1" minOccurs="1" type="string" />
<element name="charValue" maxOccurs="unbounded" minOccurs="1" type="tns:charValueType" />
</sequence>
</complexType>
<complexType name="charValueType">
<sequence>
<element name="characteristic" type="string" maxOccurs="1" minOccurs="1" />
<element name="value" type="string" maxOccurs="1" minOccurs="1" />
</sequence>
</complexType>
<element name="sapIpcResponse">
<complexType>
<sequence>
<element name="wsResultBean" type="tns:wsResultBeanType" maxOccurs="1" minOccurs="0" />
<element name="wsErrorBean" type="tns:wsErrorBeanType" maxOccurs="1" minOccurs="0" />
</sequence>
</complexType>
</element>
<complexType name="wsResultBeanType">
<sequence>
<element name="materialnumber" maxOccurs="1" minOccurs="1" type="string" />
<element name="language" maxOccurs="1" minOccurs="0" type="string" />
<element name="statusOfCompletion" maxOccurs="1" minOccurs="1" type="string" />
<element name="message" maxOccurs="1" minOccurs="1" type="string" />
<element name="numberOfPossibleCharValuePairs" maxOccurs="1" minOccurs="1" type="string" />
<element name="numberOfDefaultSystemCharValuePairs" maxOccurs="1" minOccurs="1" type="string" />
<element name="numberOfRequiredChars" maxOccurs="1" minOccurs="1" type="string" />
<element name="lastAssignedCharValuePair" maxOccurs="1" minOccurs="0" type="tns:charValueType" />
<element name="requiredChars" maxOccurs="unbounded" minOccurs="0" type="string" />
<element name="defaultSystemCharValuePairs" maxOccurs="unbounded" minOccurs="0" type="tns:charValueType" />
<element name="possibleCharValuePairs" maxOccurs="unbounded" minOccurs="0" type="tns:charValueType" />
</sequence>
</complexType>
<complexType name="wsErrorBeanType">
<sequence>
<element name="errorMessages" maxOccurs="unbounded" minOccurs="0" type="string" />
</sequence>
</complexType>
</schema>
Did it this way, maybe not the rightest way, but it works so far:
export default defineComponent({
async run({ steps, $ }) {
const jsonObj = ["117101-1100-005","115202-1640-015","115202-1650-005","112000-5100-005","117101-7850-040","117101-7900-005"]
let familyVersion = jsonObj[0].slice(0,6);
const xml1 = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\""+
"xmlns:ipc=\"http://kiongroup.com/sapIpcService\">"+
"<soapenv:Header/>"+
"<soapenv:Body>"+
"<ipc:sapIpcRequest>"+
"<ipc:wsTransferBean>"+
"<ipc:function>i</ipc:function>"+
"<ipc:language>en</ipc:language>"+
"<ipc:familyVersion>"+familyVersion+"</ipc:familyVersion>"+
"<ipc:style>s</ipc:style>";
;
let xmlFinal = xml1
for (let obj of jsonObj){
let characteristic = obj.slice(7,11);
let value = obj.slice(12,15);
let xmlBody = "<ipc:charValue><ipc:characteristic>"+characteristic+"</ipc:characteristic><ipc:value>"+value+"</ipc:value></ipc:charValue>"
xmlFinal+=xmlBody
}
const xml2="</ipc:wsTransferBean>"+
"</ipc:sapIpcRequest>"+
"</soapenv:Body></soapenv:Envelope>"
xmlFinal+=xml2
return xmlFinal
},
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.