json - 如何解析从 SOAP WS 的 XML 响应创建的 JSON?

标签 json node.js xml xmldom

我需要帮助来解析从 NodeJS 中 SOAP Web 服务的 XML 响应创建的 JSON。我想要一个 通知 的 JSON 数组。

XML如下:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <ns2:getNotificationsResponse xmlns:ns2="---url---">
         <return>
            <notifications>
               <ackRequired>false</ackRequired>
               <body>Testing Notitfications</body>
               <created>1498798404874</created>
               <gpsAlt>1.0</gpsAlt>
               <gpsLat>1.0</gpsLat>
               <gpsLong>1.0</gpsLong>
               <messageId>253</messageId>
               <priority>INFORMATIONAL</priority>
               <senderClientId>PMC0</senderClientId>
               <status>SENT</status>
               <subject>Test Notification</subject>
               <updated>1498798404874</updated>
               <userId>1</userId>
               <userLogin>ipics</userLogin>
            </notifications>
            <notifications>
               <ackRequired>false</ackRequired>
               <body>Test notitfication</body>
               <created>1498797535714</created>
               <gpsAlt>0.0</gpsAlt>
               <gpsLat>0.0</gpsLat>
               <gpsLong>0.0</gpsLong>
               <messageId>244</messageId>
               <priority>HIGH</priority>
               <senderClientId>PMC_1234</senderClientId>
               <status>SENT</status>
               <subject>Test</subject>
               <updated>1498797535714</updated>
               <userId>1</userId>
               <userLogin>ipics</userLogin>
            </notifications>
            <notifications>
               <ackRequired>false</ackRequired>
               <body>Testing Notitfications</body>
               <created>1498794764538</created>
               <gpsAlt>1.0</gpsAlt>
               <gpsLat>1.0</gpsLat>
               <gpsLong>1.0</gpsLong>
               <messageId>239</messageId>
               <priority>INFORMATIONAL</priority>
               <senderClientId>PMC0</senderClientId>
               <status>SENT</status>
               <subject>Test Notification</subject>
               <updated>1498794764538</updated>
               <userId>1</userId>
               <userLogin>ipics</userLogin>
            </notifications>
            <notifications>
               <ackRequired>false</ackRequired>
               <body>Testing Notitfications</body>
               <created>1498794760123</created>
               <gpsAlt>1.0</gpsAlt>
               <gpsLat>1.0</gpsLat>
               <gpsLong>1.0</gpsLong>
               <messageId>234</messageId>
               <priority>INFORMATIONAL</priority>
               <senderClientId>PMC0</senderClientId>
               <status>SENT</status>
               <subject>Test Notification</subject>
               <updated>1498794760123</updated>
               <userId>1</userId>
               <userLogin>ipics</userLogin>
            </notifications>
         </return>
      </ns2:getNotificationsResponse>
   </soap:Body>
</soap:Envelope>

我正在使用 xmldom Node 模块。
我的代码如下,但它没有给出正确的响应。

var doc = new DOMParser().parseFromString(data.response, 'text/xml');
var valueXML = doc.getElementsByTagName('return');
var temp = valueXML[0].getElementsByTagName("notifications")[0];
var output = temp.getElementsByTagName("nextSibling")._node.childNodes.parentNode

最佳答案

const transform = require('camaro')
const fs = require('fs')

const xml = fs.readFileSync('so.xml', 'utf-8')
const template = {
    notifications: ['//notifications', {
        ackRequired: 'ackRequired',
        body: 'body',
        created: 'created',
        gpsAlt: 'number(gpsAlt)'
    }]
}

const result = transform(xml, template)
console.log(JSON.stringify(result, null, 2))

并输出:

{
  "notifications": [
    {
      "ackRequired": "false",
      "body": "Testing Notitfications",
      "created": "1498798404874",
      "gpsAlt": 1
    },
    {
      "ackRequired": "false",
      "body": "Test notitfication",
      "created": "1498797535714",
      "gpsAlt": 0
    },
    {
      "ackRequired": "false",
      "body": "Testing Notitfications",
      "created": "1498794764538",
      "gpsAlt": 1
    },
    {
      "ackRequired": "false",
      "body": "Testing Notitfications",
      "created": "1498794760123",
      "gpsAlt": 1
    }
  ]
}

我只是在模板中添加一些字段来测试。您可以根据需要添加更多内容。

关于json - 如何解析从 SOAP WS 的 XML 响应创建的 JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44859914/

相关文章:

javascript - 从本地主机加载 json

java - 如何在java中查询json对象?

c# - 在 xml 中获取属性名称和属性值

c# - 忽略空值 - 序列化

javascript - 将时间戳与 00 :00:00 of today? 进行比较的 Firebase 数据库规则

json - 从Elasticsearch 6迁移到7时的Json问题

javascript - node.js 可以使用哪些包来进行图像裁剪?

node.js - 在 nextjs 中使用 getServerSideProps 进行 graphql-codegen 突变

node.js - 在dat协议(protocol)中,如果我安装了dat并用它来共享文件夹,我就变成了所谓的peer,并且还存储了其他peer的数据,对吧?

php - javascript 不通过 php 代码发送数据