使用JSON值数组作为输入创建XML- Javascript

sd2nnvve  于 2023-01-03  发布在  Java
关注(0)|答案(1)|浏览(97)

我有一个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>
kcwpcxri

kcwpcxri1#

这样做,也许不是最好的方法,但它的工作到目前为止:

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

  },
})

相关问题