Sidebar

Parsing XML error message

0 votes
1.2K views
asked May 25, 2017 by jeremy-r-2799 (140 points)
I'm looking to take the following message below and break it apart so that I can read each individual error message in it's own line. It seems maybe the best way to do this is by StringUtils.splitByWholeSeparator and on the &#xD characters but I'm struggling. Is this the best way or is there a better way? Thanks in advance.

 

<?xml version='1.0' encoding='UTF-8'?><S:Envelope xmlns:S="http://www.w3.org/2003/05/soap-envelope"><S:Body><ns2:submitSingleMessageResponse xmlns:ns2="http://vaccination.org/" xmlns:ns3="urn:cdc:iisb:2011"><return>&#xD;MSH|^~\&amp;|WIR|||15510|20170404204326-0500||ACK^V04^ACK|665689|P|2.5.1|||NE|NE|||||Z23^CDCPHINVS&#xD;MSA|AE|2017040420432418&#xD;ERR|||207^Application internal error^HL70357|E||||Message Rejected.  Please review errors.&#xD;ERR||MSH^1|207^Application internal error^HL70357|E||||No valid RXAs -- Message rejected&#xD;ERR||MSH^1^4^1|101^Required field missing^HL70357|E||||Sending Facility is required&#xD;ERR||MSH^1^6^1|102^Data type error^HL70357|W|4^Invalid value^HL70533|||Value [WIR] is not valid for HL70362&#xD;ERR||MSH^1^7^1|102^Data type error^HL70357|W|2^Invalid date^HL70533|||TS_Z data type requires Time Zone&#xD;ERR||MSH^1^21^1|102^Data type error^HL70357|E|4^Invalid value^HL70533|||Need a valid profile&#xD;ERR||PID^1^11^1^6|103^Table value not found^HL70357|W|5^Table value not found^HL70533|||System does not have table definitions for [HL70399] to validate [USA]&#xD;ERR||ORC^1^10^1^13|101^Required field missing^HL70357|W||||Identifier Type Code is required: XCN-1 (ID Number) is valued&#xD;ERR||ORC^1^12^1^1|101^Required field missing^HL70357|W||||ID Number is required: XCN.2.1 (Surname) and XCN.3 (Given Name) are not valued&#xD;ERR||ORC^1^12^1^2|101^Required field missing^HL70357|W||||Family Name is required&#xD;ERR||ORC^1^12^1^2|102^Data type error^HL70357|W|4^Invalid value^HL70533|||Ordering provider last name is required to use ordering provider field.&#xD;ERR||ORC^1^12^1^13|101^Required field missing^HL70357|W||||Identifier Type Code is required: XCN-1 (ID Number) is valued&#xD;ERR||RXA^1^5^1^4|101^Required field missing^HL70357|W||||Alternate Identifier is blank or empty.  Ignoring second tripet&#xD;ERR||RXA^1^5^1^4|102^Data type error^HL70357|W||||WIR recommends specification of NDC in second triplet.&#xD;ERR||RXA^1^7^1|103^Table value not found^HL70357|E|5^Table value not found^HL70533|||Value [U] not found in table [UCUM]&#xD;ERR||RXA^1^9^1^3|102^Data type error^HL70357|E|4^Invalid value^HL70533|||Value [NIP0001] is not valid for Name of Coding System&#xD;ERR||RXA^1^10^1^13|101^Required field missing^HL70357|W||||Identifier Type Code is required: XCN-1 (ID Number) is valued&#xD;ERR||RXA^1^11^1^4^1|102^Data type error^HL70357|W|4^Invalid value^HL70533|||Incoming administering site (clinic) is not associated with owning provider.&#xD;ERR||RXR^1^1^1^3|102^Data type error^HL70357|W|4^Invalid value^HL70533|||Value [HL70162] is not valid for Name of Coding System&#xD;ERR||OBX^1^5^1|103^Table value not found^HL70357|W|5^Table value not found^HL70533|||Value [V00] not found in table [HL70064]&#xD;ERR||OBX^2^14^1|101^Required field missing^HL70357|W||||Date/Time of the Observation is required&#xD;ERR||OBX^3^14^1|101^Required field missing^HL70357|W||||Date/Time of the Observation is required&#xD;ERR||OBX^4^14^1|101^Required field missing^HL70357|W||||Date/Time of the Observation is required&#xD;ERR||OBX^5^14^1|101^Required field missing^HL70357|W||||Date/Time of the Observation is required&#xD;</return></ns2:submitSingleMessageResponse></S:Body></S:Envelope>

1 Answer

0 votes

The easiest way is to parse this as XML then as HL7 and break out each ERR segment. Here is some sample code.

var hl7 = qie.parseHL7String(message.getNode('//return'));
qie.debug('hl7: ' + hl7);
var errors = hl7.getAllNodes('ERR');
for (var i = 0; i < errors.length; i++) {
   var error = qie.parseHL7String(errors[i]);
   qie.debug('err segment: ' + error.getNode('ERR'));
   qie.debug('Segment:"' + error.getNode('ERR-2.1') + '-' + error.getNode('ERR-2.3') + '" Instance:"' + error.getNode('ERR-2.2') + '" has the following error:"' + error.getNode('ERR-8') + '"');
}

answered May 25, 2017 by brandon-w-8204 (33,270 points)
commented May 25, 2017 by jeremy-r-2799 (140 points)
reshown May 25, 2017 by brandon-w-8204
How would i go about moving this from debug to a output file.?
commented May 25, 2017 by brandon-w-8204 (33,270 points)
You can use the following to write any string to a file.

qie.writeFile('file', 'value', shouldCreatePath*);
...