2017-07-01 5 views
1

Ich brauche Hilfe beim Parsen eines JSON, der aus einer XML-Antwort eines SOAP-Web-Service in NodeJS erstellt wurde. Ich möchte ein JSON-Array von notifications.Wie kann JSON, das aus der XML-Antwort von SOAP WS erstellt wurde, analysiert werden?

XML ist wie folgt:

<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> 

I xmldom Knotenmodul bin mit.
Mein Code ist wie folgt, aber es gibt keine richtige Antwort.

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 
+0

wie etwa 'valueXML [0] –

+0

@ShanilFernando .children', es undefined –

Antwort

1
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)) 

und Ausgang:

{ 
    "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 
    } 
    ] 
} 

Ich füge nur ein paar Felder in Vorlage zu testen. Sie können mehr Basis hinzufügen, was Sie brauchen.

+0

Was passiert, wenn ich Felder nicht kenne? –

+0

Danke, es hat für mich funktioniert. du hast meinen Tag gerettet. –

+0

@DarshanaAhalpara Ich habe dieses Paket geschrieben, um xml nach json zu transformieren, aber Sie müssen die Vorlage selbst schreiben (Sie kennen das Format der Eingabe). Aber wenn das nicht der Fall ist, müssen Sie möglicherweise ein anderes Paket dafür finden. –

1

Versuchen Sie dies. Sie können es wie gewünscht in JSON abbilden. Ich zeige dir nur die Richtung.

Working example in codepen

var response = `<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>`; 

var doc = new DOMParser().parseFromString(response, 'text/xml'); 
var valueXML = doc.getElementsByTagName('return'); 
var temps = valueXML[0].children; 
var nortifications=[]; 
for (var i = 0; i < temps.length; i++) { 
    var temp = temps[i].children; 
    var obj = {}; 
    for (var j = 0; j < temp.length; j++) { 
    var property = temp[j]; 

    obj[property.localName] = property.innerHTML; 
} 
console.log(JSON.stringify(obj)); 
nortifications.push(obj); 
} 

Ausgang:

{"ackRequired":"false","body":"Testing Notitfications","created":"1498798404874","gpsAlt":"1.0","gpsLat":"1.0","gpsLong":"1.0","messageId":"253","priority":"INFORMATIONAL","senderClientId":"PMC0","status":"SENT","subject":"Test Notification","updated":"1498798404874","userId":"1","userLogin":"ipics"} 
{"ackRequired":"false","body":"Test notitfication","created":"1498797535714","gpsAlt":"0.0","gpsLat":"0.0","gpsLong":"0.0","messageId":"244","priority":"HIGH","senderClientId":"PMC_1234","status":"SENT","subject":"Test","updated":"1498797535714","userId":"1","userLogin":"ipics"} 
{"ackRequired":"false","body":"Testing Notitfications","created":"1498794764538","gpsAlt":"1.0","gpsLat":"1.0","gpsLong":"1.0","messageId":"239","priority":"INFORMATIONAL","senderClientId":"PMC0","status":"SENT","subject":"Test Notification","updated":"1498794764538","userId":"1","userLogin":"ipics"} 
{"ackRequired":"false","body":"Testing Notitfications","created":"1498794760123","gpsAlt":"1.0","gpsLat":"1.0","gpsLong":"1.0","messageId":"234","priority":"INFORMATIONAL","senderClientId":"PMC0","status":"SENT","subject":"Test Notification","updated":"1498794760123","userId":"1","userLogin":"ipics"} 
+0

property.innerHTML kehrt gibt nicht definiert. –

+0

@DarshanaAhalpara Überprüfen Sie die Konsolenausgabe [https://jsfiddle.net/48qjec55/3/](https://jsfiddle.net/48qjec55/3/). Ich bekomme keine Fehler –

+0

Ich bekomme die HTML-Tags in der Eigenschaft. aber beim Versuch, property.innerHTML zu feuern, gibt es undefined. –

Verwandte Themen