Last active
December 8, 2020 13:58
-
-
Save Vijaysinh/f11c2102c720891424e66d0daee8d351 to your computer and use it in GitHub Desktop.
VersionOne API XML Error Parsing using JS DOMParser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
xmlstring = | |
<Error href="/safetest/rest-1.oauth.v1/Data/Request/602092"> | |
<Message>Bad Request</Message> | |
<Exception class="VersionOne.DataException"> | |
<Message>Violation'ScopeMustBeActive</Message> | |
</Exception> | |
<Exception class="System.Data.SqlClient.SqlException"> | |
<Message>Violation'ScopeMustBeActive</Message> | |
</Exception> | |
</Error> | |
var parser = new DOMParser(); | |
var docError = parser.parseFromString('INVALID', 'text/xml'); | |
var parsererrorNS = docError.getElementsByTagName("parsererror")[0].namespaceURI; | |
var doc = parser.parseFromString(xmlstring, 'text/xml'); | |
if (doc.getElementsByTagNameNS(parsererrorNS, 'parsererror').length > 0) { | |
throw new Error('Error parsing XML'); | |
} | |
var rootElement = doc.documentElement; | |
var children = rootElement.childNodes; | |
for(var i =0; i< children.length; i++) { | |
var child = children[i]; | |
if(child.nodeType == Node.ELEMENT_NODE) { | |
var api_error = child.getElementsByTagName("Message")[0]; | |
if(api_error!=undefined){ | |
console.log(api_error.textContent); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment