使用JavaScript从SOAP响应中提取特定值

时间:2017-08-28 08:18:30

标签: javascript soap

我需要使用JavaScript从下面的SOAP响应中提取患者ID(PATAA000000040)并插入到Mirth目的地。该值在livingSubjectId标记下​​。此标记可能会重复,但我们需要获取第一个livingSubjectId迭代值。

我该如何编写JavaScript?

<?xml version='1.0' encoding='UTF-8'?>
<S:Envelope
    xmlns:S="http://www.w3.org/2003/05/soap-envelope">
    <S:Body>
        <ns3:RespondingGateway_PRPA_IN201306UV02Response
            xmlns="urn:gov:hhs:fha:NwHINc:common:NwHINccommon"
            xmlns:ns2="http://schemas.xmlsoap.org/ws/2004/08/addressing"
            xmlns:ns3="urn:hl7-org:v3"
            xmlns:ns4="urn:gov:hhs:fha:NwHINc:common:patientcorrelationfacade">
            <ns3:communityResponse>
                <ns3:PRPA_IN201306UV02 ITSVersion="XML_1.0" nullFlavor="">
                    <ns3:id root="2.16.840.1.113883.3.609.20.330.002" extension="245c45ce:137cd5bcb6d:-7f4d"/>
                    <ns3:creationTime value="201268183250"/>
                    <ns3:interactionId root="2.16.840.1.113883.1.6" extension="PRPA_IN201306UV02"/>
                    <ns3:processingCode code="P"/>
                    <ns3:processingModeCode code="T"/>
                    <ns3:acceptAckCode code="NE"/>
                    <ns3:receiver typeCode="RCV">
                        <ns3:device classCode="DEV" determinerCode="INSTANCE">
                            <ns3:id/>
                            <ns3:asAgent classCode="AGNT">
                                <ns3:representedOrganization classCode="ORG" determinerCode="INSTANCE">
                                    <ns3:id root="2.16.840.1.113883.3.609.10.330.000"/>
                                </ns3:representedOrganization>
                            </ns3:asAgent>
                        </ns3:device>
                    </ns3:receiver>
                    <ns3:sender typeCode="SND">
                        <ns3:device classCode="DEV" determinerCode="INSTANCE">
                            <ns3:id root="2.16.840.1.113883.3.609.10.330.002"/>
                            <ns3:asAgent classCode="AGNT">
                                <ns3:representedOrganization classCode="ORG" determinerCode="INSTANCE">
                                    <ns3:id root="2.16.840.1.113883.3.609.10.330.002"/>
                                </ns3:representedOrganization>
                            </ns3:asAgent>
                        </ns3:device>
                    </ns3:sender>
                    <ns3:acknowledgement>
                        <ns3:typeId root="2.16.840.1.113883.1.6" extension="PRPA_IN201305UV02"/>
                        <ns3:typeCode code="AA"/>
                        <ns3:targetMessage xsi:nil="true"
                            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                        </ns3:acknowledgement>
                        <ns3:controlActProcess nullFlavor="" classCode="CACT" moodCode="EVN">
                            <ns3:code code="PRPA_TE201306UV" codeSystem="2.16.840.1.113883.1.6"/>
                            <ns3:authorOrPerformer nullFlavor="" typeCode="AUT">
                                <ns3:assignedDevice classCode="ASSIGNED">
                                    <ns3:id root="2.16.840.1.113883.3.609.20.330.002.1"/>
                                </ns3:assignedDevice>
                            </ns3:authorOrPerformer>
                            <ns3:subject typeCode="SUBJ">
                                <ns3:registrationEvent classCode="REG" moodCode="EVN">
                                    <ns3:id nullFlavor="NA"/>
                                    <ns3:statusCode code="active"/>
                                    <ns3:subject1 typeCode="SBJ">
                                        <ns3:patient classCode="PAT">
                                            <ns3:id root="2.16.840.1.113883.3.609.20.330.002.1" extension="PATAA002000040"/>
                                            <ns3:statusCode code="SD"/>
                                            <ns3:patientPerson classCode="PSN" determinerCode="INSTANCE">
                                                <ns3:name>
                                                    <ns3:family partType="FAM">LastName40</ns3:family>
                                                    <ns3:given partType="GIV">FirstName40</ns3:given>
                                                    <ns3:given partType="GIV">MN40</ns3:given>
                                                </ns3:name>
                                                <ns3:telecom value="tel:+1-417-831-1475"/>
                                                <ns3:administrativeGenderCode code="M"/>
                                                <ns3:birthTime value="19350213"/>
                                                <ns3:addr>
                                                    <ns3:streetAddressLine>225 E Commercial St</ns3:streetAddressLine>
                                                    <ns3:city>Springfield</ns3:city>
                                                    <ns3:state>MO</ns3:state>
                                                    <ns3:postalCode>65803</ns3:postalCode>
                                                </ns3:addr>
                                                <ns3:asOtherIDs classCode="SD">
                                                    <ns3:id root="2.16.840.1.113883.4.1" extension="990000040"/>
                                                    <ns3:scopingOrganization classCode="ORG" determinerCode="INSTANCE">
                                                        <ns3:id root="2.16.840.1.113883.4.1"/>
                                                    </ns3:scopingOrganization>
                                                </ns3:asOtherIDs>
                                            </ns3:patientPerson>
                                            <ns3:providerOrganization classCode="ORG" determinerCode="INSTANCE">
                                                <ns3:id root="2.16.840.1.113883.3.609.20.330.002.1"/>
                                                <ns3:contactParty xsi:nil="true"
                                                    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                                                </ns3:providerOrganization>
                                                <ns3:subjectOf1>
                                                    <ns3:queryMatchObservation classCode="CASE" moodCode="EVN">
                                                        <ns3:code code="IHE_PDQ"/>
                                                        <ns3:value xsi:type="ns3:INT" value="100"
                                                            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                                                        </ns3:queryMatchObservation>
                                                    </ns3:subjectOf1>
                                                </ns3:patient>
                                            </ns3:subject1>
                                            <ns3:custodian typeCode="CST">
                                                <ns3:assignedEntity classCode="ASSIGNED">
                                                    <ns3:id root="2.16.840.1.113883.3.609.20.330.002.1"/>
                                                    <ns3:code code="NotHealthDataLocator" codeSystem="1.3.6.1.4.1.19376.1.2.27.2"/>
                                                </ns3:assignedEntity>
                                            </ns3:custodian>
                                        </ns3:registrationEvent>
                                    </ns3:subject>
                                    <ns3:subject typeCode="SUBJ">
                                        <ns3:registrationEvent classCode="REG" moodCode="EVN">
                                            <ns3:id nullFlavor="NA"/>
                                            <ns3:statusCode code="active"/>
                                            <ns3:subject1 typeCode="SBJ">
                                                <ns3:patient classCode="PAT">
                                                    <ns3:id root="2.16.840.1.113883.3.609.20.330.002.2" extension="PATAA002000040"/>
                                                    <ns3:statusCode code="SD"/>
                                                    <ns3:patientPerson classCode="PSN" determinerCode="INSTANCE">
                                                        <ns3:name>
                                                            <ns3:family partType="FAM">LastName40</ns3:family>
                                                            <ns3:given partType="GIV">FirstName40</ns3:given>
                                                            <ns3:given partType="GIV">MN40</ns3:given>
                                                        </ns3:name>
                                                        <ns3:telecom value="tel:+1-417-831-1475"/>
                                                        <ns3:administrativeGenderCode code="M"/>
                                                        <ns3:birthTime value="19350213"/>
                                                        <ns3:addr>
                                                            <ns3:streetAddressLine>225 E Commercial St</ns3:streetAddressLine>
                                                            <ns3:city>Springfield</ns3:city>
                                                            <ns3:state>MO</ns3:state>
                                                            <ns3:postalCode>65803</ns3:postalCode>
                                                        </ns3:addr>
                                                        <ns3:asOtherIDs classCode="SD">
                                                            <ns3:id root="2.16.840.1.113883.4.1" extension="990000040"/>
                                                            <ns3:scopingOrganization classCode="ORG" determinerCode="INSTANCE">
                                                                <ns3:id root="2.16.840.1.113883.4.1"/>
                                                            </ns3:scopingOrganization>
                                                        </ns3:asOtherIDs>
                                                    </ns3:patientPerson>
                                                    <ns3:providerOrganization classCode="ORG" determinerCode="INSTANCE">
                                                        <ns3:id root="2.16.840.1.113883.3.609.20.330.002.2"/>
                                                        <ns3:contactParty xsi:nil="true"
                                                            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                                                        </ns3:providerOrganization>
                                                        <ns3:subjectOf1>
                                                            <ns3:queryMatchObservation classCode="CASE" moodCode="EVN">
                                                                <ns3:code code="IHE_PDQ"/>
                                                                <ns3:value xsi:type="ns3:INT" value="100"
                                                                    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                                                                </ns3:queryMatchObservation>
                                                            </ns3:subjectOf1>
                                                        </ns3:patient>
                                                    </ns3:subject1>
                                                    <ns3:custodian typeCode="CST">
                                                        <ns3:assignedEntity classCode="ASSIGNED">
                                                            <ns3:id root="2.16.840.1.113883.3.609.20.330.002.2"/>
                                                            <ns3:code code="NotHealthDataLocator" codeSystem="1.3.6.1.4.1.19376.1.2.27.2"/>
                                                        </ns3:assignedEntity>
                                                    </ns3:custodian>
                                                </ns3:registrationEvent>
                                            </ns3:subject>
                                            <ns3:queryAck>
                                                <ns3:queryId root="2.16.840.1.113883.3.609.10.330.000" extension="-abd3453dcd24wkkks545"/>
                                                <ns3:queryResponseCode code="OK"/>
                                            </ns3:queryAck>
                                            <ns3:queryByParameter>
                                                <ns3:queryId root="2.16.840.1.113883.3.609.10.330.000" extension="-abd3453dcd24wkkks545"/>
                                                <ns3:statusCode code="new"/>
                                                <ns3:responseModalityCode code="R"/>
                                                <ns3:responsePriorityCode code="I"/>
                                                <ns3:parameterList>
                                                    <ns3:livingSubjectAdministrativeGender>
                                                        <ns3:value code="M"/>
                                                        <ns3:semanticsText representation="TXT"/>
                                                    </ns3:livingSubjectAdministrativeGender>
                                                    <ns3:livingSubjectBirthTime>
                                                        <ns3:value value="19350213"/>
                                                        <ns3:semanticsText representation="TXT"/>
                                                    </ns3:livingSubjectBirthTime>
                                                    <ns3:livingSubjectId>
                                                        <ns3:value root="2.16.840.1.113883.3.609.20.330.000" extension="PATAA000000040"/>
                                                        <ns3:semanticsText representation="TXT"/>
                                                    </ns3:livingSubjectId>
                                                    <ns3:livingSubjectId>
                                                        <ns3:value root="2.16.840.1.113883.4.1" extension=""/>
                                                        <ns3:semanticsText representation="TXT"/>
                                                    </ns3:livingSubjectId>
                                                    <ns3:livingSubjectName>
                                                        <ns3:value>
                                                            <ns3:prefix partType="PFX"/>
                                                            <ns3:given partType="GIV">FirstName40</ns3:given>
                                                            <ns3:given partType="GIV"/>
                                                            <ns3:family partType="FAM">LastName40</ns3:family>
                                                            <ns3:suffix partType="PFX"/>
                                                        </ns3:value>
                                                        <ns3:semanticsText representation="TXT"/>
                                                    </ns3:livingSubjectName>
                                                    <ns3:patientAddress>
                                                        <ns3:value/>
                                                    </ns3:patientAddress>
                                                    <ns3:patientTelecom>
                                                        <ns3:value value=""/>
                                                        <ns3:semanticsText representation="TXT"/>
                                                    </ns3:patientTelecom>
                                                </ns3:parameterList>
                                            </ns3:queryByParameter>
                                        </ns3:controlActProcess>
                                    </ns3:PRPA_IN201306UV02>
                                    <ns3:NwHINTargetCommunity>
                                        <homeCommunity>
                                            <homeCommunityId>2.16.840.1.113883.3.609.10.330.002</homeCommunityId>
                                        </homeCommunity>
                                    </ns3:NwHINTargetCommunity>
                                </ns3:communityResponse>
                            </ns3:RespondingGateway_PRPA_IN201306UV02Response>
                        </S:Body>
                    </S:Envelope>

这就是我的尝试:

msg = new XML(msg);
logger.info(msg);
var soap = new Namespace('http://www.w3.org/2003/05/soap-envelope');
var edeia = new Namespace('http://schemas.xmlsoap.org/ws/2004/08/addressing');
var PatientID = msg.soap::Body.edeia::controlActProcess.edeia::queryByParameter.edeia::livingSubjectId.toString();
logger.info(PatientID);
channelMap.put('PatientID',PatientID);

但是在Mirth中得到了以下错误:

Transformer error
ERROR MESSAGE: Error evaluating transformer
com.mirth.connect.server.MirthJavascriptTransformerException: 
CHANNEL:    Test SOAP Response
CONNECTOR:  sourceConnector
SCRIPT SOURCE:  
SOURCE CODE:    
39:         }
40:         eval('importPackage(' + Packages.java.lang.Class.forName(className).getPackage().getName() + ')');
41:     }
42: }
43: function doScript() {
44: msg = new XML(connectorMessage.getTransformedData());
45: if (msg.namespace('') != undefined) { default xml namespace = msg.namespace(''); } else { default xml namespace = ''; }
46: function doFilter() { phase[0] = 'filter'; return true; }function doTransform() { phase[0] = 'transformer'; logger = Packages.org.apache.log4j.Logger.getLogger(phase[0]);
47: 
48: 
LINE NUMBER:    44
DETAILS:    TypeError: The content of elements must consist of well-formed character data or markup.
    at a2b07c33-e451-4e76-9a96-81c0fdf80552:44 (doScript)
    at a2b07c33-e451-4e76-9a96-81c0fdf80552:79
    at com.mirth.connect.server.transformers.JavaScriptFilterTransformer$FilterTransformerTask.doCall(JavaScriptFilterTransformer.java:154)
    at com.mirth.connect.server.transformers.JavaScriptFilterTransformer$FilterTransformerTask.doCall(JavaScriptFilterTransformer.java:119)
    at com.mirth.connect.server.util.javascript.JavaScriptTask.call(JavaScriptTask.java:113)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

1 个答案:

答案 0 :(得分:0)

我认为您没有正确解析XML标记,这就是错误所说的内容。我正在获得这样的SOAP响应。

<S:Envelope xmlns:S="http://test.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENV="http://test.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<S:Body>
<ns1:addInformation xmlns:ns2="http://Getdatawebservice/">;
<send>
<MrnNumber>MRN5023150</MrnNumber>
<isError>True</isError>
<ErrorReason>Patient deceased</ErrorReason>
</send>
</ns1:addInformation>
</S:Body>
</S:Envelope>

以下是我用于从响应消息中获取 ErrorReason 标记详细信息的代码。我在目的地的编辑回复标签中编写了此代码。

//Replace : completely, mirth somehow not takes it
var updateMsg= new XML (msg).toString().replace(/:/g,”)
    //Create a new XML using it
    var newMsg= new XML (updateMsg);
    //Get the specific Error Reason XML tag value
    var checkError = newmsg[‘SBody’][‘ns1addInformation ‘][‘send’][‘isError’].toString();
    // Not required in your scenario
    if(checkError==”false”)
    {
    var errorMessage = “Message Sent Successfully”;
    channelMap.put(“errorMessage”,errorMessage);
    }

您也可以使用相同的上述代码来解析您的必填字段。它应该工作。对于你的情况下的迭代,你可能会多次出现,你必须做循环逻辑。