Announcement

Collapse

Mirth Connect 3.12.0 Released!

Mirth Connect 3.12.0 is now available as an appliance update and on our GitHub page. This release includes database performance improvements, improves visual HL7 representation, message pruning, keystore handling, PDF generation, community contributions, and fixes several security vulnerabilities. This release also contains many improvements to commercial extensions. See the release notes for the list of fixes and updates.

Download | See What's New | Upgrade Guide | Release Notes

For discussion on this release, see this thread.
See more
See less

How to access attribute in xml

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to access attribute in xml

    I have some xml (included below) returned from a web service. I know how to examine one of the elements in the xml, but how do I examine an attribute?

    //Get destination
    var pDestination = responseMap.get('Destination 1');
    //Get destination response
    var xmlResponse = new XML(pDestination.getMessage());
    //Get MessageText element contents of xml
    var strMessageText = xmlResponse..*::MessageText.toString();
    //How to get attribute Type in Result Element?


    Soap response:

    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
    <UploadDocumentsResponse xmlns="www.mycompany.ca/webservices">
    <Result Type="Error" xmlns="http://www.mycompany.ca/Schema">
    <MessageText>Invalid Source 'MAPLE'.</MessageText>
    </Result>
    </UploadDocumentsResponse>
    </soap:Body>
    </soap:Envelope>

  • #2
    Re:How to access attribute in xml (Solved)

    //Get destination
    var pDestination = responseMap.get('Destination 1');
    //Get destination response
    var xmlResponse = new XML(pDestination.getMessage());
    //Get MessageText element contents of xml
    var strMessageText = xmlResponse..*::MessageText.toString();
    //Get Type attribute
    var strType = xmlResponse..*::Result['@Type'].toString();

    Comment


    • #3
      Re:How to access attribute in xml

      Could you describe the steps that you are using?
      I need something similar and I don't know the exact steps that I need to do.
      My first destination is the one that connect to the web service and at the second destination I need to get the response from the web service (an xml file) and apply some transformation. Where should I put the transformer?What type of transformer I must create?

      Thanks a lot!

      Comment


      • #4
        Re:How to access attribute in xml

        I have two destinations in my channel. The first is responsible for calling the web service, the second is responsible for parsing the response of the web service. What happens is the first destination does the web service call, and the destinations message will contain the web service response.

        Create a SOAP Sender destination, name it "WebService.Call" (no quotes). Fill in the fields so that it can call your web service.

        Create a JavaScript Writer destination, name it "WebService.Response" (no quotes). The javascript will look similar to this:

        //Get the destination that is named "WebService.Call"
        var pDestination = responseMap.get('WebService.Call');
        //Get the status of the destination
        var strStatus = pDestination.getStatus().toString();

        //The status will be "SUCCESS","FAILURE","FILTERED". If it is "SUCCESS" then the
        //web service was called correctly.
        if (strStatus == "SUCCESS")
        {
        //Get the xml response of the web service call
        var xmlResponse = new XML(pDestination.getMessage());

        /*
        In my case the web service response looks like the following, and I want to get the "Type" attribute from the "Result" element.

        <?xml version="1.0" encoding="utf-8"?>
        <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <soap:Body>
        <UploadDocumentsResponse xmlns="www.mycompany.ca/webservices">
        <Result Type="Error" xmlns="http://www.interiorhealth.ca/Schema">
        <Details>
        <Detail Name="File Count">1</Detail>
        <Detail Name="Source">MAPLE</Detail>
        </Details>
        <Messages>
        <Message>Invalid Source 'MAPLE'.</Message>
        </Messages>
        </Result>
        </UploadDocumentsResponse>
        </soap:Body>
        </soap:Envelope>
        */

        //This is how I get the Type attribute in the result element.
        //xmlRespnse holds the xml returned by the web service
        //..*Result gets the Result element, regardless of the namespaces
        //['@Type'] returns the Type attrute within the Result element.
        var strType = xmlResponse..*::Result['@Type'].toString();

        //Put the strType value in the channel map
        channelMap.put('Type',strType);

        //Now you can get this data from any other destination after this one by using
        //channelMap.get('Type')
        }

        If you want to access the value using variables that you can drag from the "Available Variables" list, create a 3rd destination. Name it "WebService.Variables". Now edit the destination transformer and add a Mapper step. Set the variable field to "Type" (no quotes) and set the Mapping field to "channelMap.get('Type')" (no quotes).

        Now in any additional transformers, filters, templates, etc you will be able to easily access the "Type" variable from "Destination Mappings" or "Available Variables" lists in the channel editor.

        Hope this helps.

        Comment


        • #5
          Interesting. And how can we write an attribute?

          Comment


          • #6
            Jerchap,

            Just wanted to say thank you. Your response pointed out an issue I was having with syntax for getting an XML response element to show up as a variable. Very helpful.

            Thanks again.

            Comment

            Working...
            X