0

My SOAP response has some tags which are repeating. For e.g. below, the Identifier tag is repeating for different types of Identifiers. I want to extract the ME Number and DEA Number from this response.

I have been able to reach the Identifier tag using below -

    if (awr.getname() == 'Identifier') {
      system.debug('Identifier Block - ' + awr);

      dom.XmlNode [] IdentifierDtls = awr.getchildren();
      system.debug('Identifier Dtls - ' + IdentifierDtls);

How do I get to ME Number and DEA Number? The below code gives me 'ME Number' as text. I want the corresponding number which is present inside tag 'IdentifierValueText'

for (dom.XmlNode idntf : IdentifierDtls) {
    if (idntf.getname() == 'IdentifierTypeDescription')
    if (idntf.gettext() == 'ME Number')
    System.debug(idntf.gettext())

SOAP Response

    <?xml version="1.0" encoding="UTF-8"?>
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
        <SOAP-ENV:Body>
            <ns0:getHCPApprovedExcludedProductsResponse xmlns:ns0="http://www.xyz.com/schemas/integration/ProductsModel.xsd">
                <ns0:MatchCount>1</ns0:MatchCount>
                <ns0:RecordCount>1</ns0:RecordCount>
                <ns0:ProductData>    


                 <ns0:Identifier>
                    <ns0:IdentifierTypeCode>202306</ns0:IdentifierTypeCode>
                    <ns0:IdentifierTypeDescription>ME Number</ns0:IdentifierTypeDescription>
                    <ns0:IdentifierValueText>xxxxx</ns0:IdentifierValueText>
                    <ns0:IdentifierStatusCode>0</ns0:IdentifierStatusCode>
                    <ns0:IdentifierStatusDescription>Active</ns0:IdentifierStatusDescription>
                    <ns0:IdentifierEffectiveDate>2008-02-15 22:07:54.0</ns0:IdentifierEffectiveDate>
                    <ns0:IdentifierDefaultFlag>Y</ns0:IdentifierDefaultFlag>
                    <ns0:DataSourceProviderID>200015</ns0:DataSourceProviderID>
                    <ns0:DataSourceProviderName>Comp A</ns0:DataSourceProviderName>
                    <ns0:ProfileID>111111</ns0:ProfileID>
                </ns0:Identifier>
                <ns0:Identifier>
                    <ns0:IdentifierTypeCode>202305</ns0:IdentifierTypeCode>
                    <ns0:IdentifierTypeDescription>DEA Number</ns0:IdentifierTypeDescription>
                    <ns0:IdentifierValueText>kkkkkk</ns0:IdentifierValueText>
                    <ns0:IdentifierStatusCode>0</ns0:IdentifierStatusCode>
                    <ns0:IdentifierStatusDescription>Active</ns0:IdentifierStatusDescription>
                    <ns0:IdentifierEffectiveDate>2008-02-16 12:39:24.0</ns0:IdentifierEffectiveDate>
                    <ns0:IdentifierDefaultFlag>Y</ns0:IdentifierDefaultFlag>
                    <ns0:DataSourceProviderID>200024</ns0:DataSourceProviderID>
                    <ns0:DataSourceProviderName>Comp A</ns0:DataSourceProviderName>
                    <ns0:ProfileID>222222</ns0:ProfileID>
                </ns0:Identifier>
                <ns0:Identifier>
                    <ns0:IdentifierTypeCode>202325</ns0:IdentifierTypeCode>
                    <ns0:IdentifierTypeDescription>Physician Number</ns0:IdentifierTypeDescription>
                    <ns0:IdentifierValueText>yyyyy</ns0:IdentifierValueText>
                    <ns0:IdentifierStatusCode>0</ns0:IdentifierStatusCode>
                    <ns0:IdentifierStatusDescription>Active</ns0:IdentifierStatusDescription>
                    <ns0:IdentifierEffectiveDate>2011-11-16 20:44:42.0</ns0:IdentifierEffectiveDate>
                    <ns0:IdentifierDefaultFlag>Y</ns0:IdentifierDefaultFlag>
                    <ns0:DataSourceProviderID>200069</ns0:DataSourceProviderID>
                    <ns0:DataSourceProviderName>Comp A</ns0:DataSourceProviderName>
                    <ns0:ProfileID>3333333</ns0:ProfileID>
                </ns0:Identifier>
                <ns0:Identifier>
                    <ns0:IdentifierTypeCode>202300</ns0:IdentifierTypeCode>
                    <ns0:IdentifierTypeDescription>SFA ID</ns0:IdentifierTypeDescription>
                    <ns0:IdentifierValueText>zzzzzz</ns0:IdentifierValueText>
                    <ns0:IdentifierStatusCode>0</ns0:IdentifierStatusCode>
                    <ns0:IdentifierStatusDescription>Active</ns0:IdentifierStatusDescription>
                    <ns0:IdentifierEffectiveDate>2003-02-14 20:13:47.0</ns0:IdentifierEffectiveDate>
                    <ns0:IdentifierDefaultFlag>Y</ns0:IdentifierDefaultFlag>
                    <ns0:DataSourceProviderID>200078</ns0:DataSourceProviderID>
                    <ns0:DataSourceProviderName>Comp A</ns0:DataSourceProviderName>
                    <ns0:ProfileID>444444</ns0:ProfileID>
                </ns0:Identifier>
0

Your posted XML does not appear to be complete. You should have a parent node which lists the namespace. Something like:

<xml xmlns:ns0="http://ns0">

Assuming you have that, here is an anonymous script that demonstrates how you might use getChildElement(name, namespace) here:

String xml = '<xml xmlns:ns0="http://ns0"><ns0:Identifier><ns0:IdentifierTypeCode>202306</ns0:IdentifierTypeCode><ns0:IdentifierTypeDescription>MENumber</ns0:IdentifierTypeDescription><ns0:IdentifierValueText>xxxxx</ns0:IdentifierValueText><ns0:IdentifierStatusCode>0</ns0:IdentifierStatusCode><ns0:IdentifierStatusDescription>Active</ns0:IdentifierStatusDescription><ns0:IdentifierEffectiveDate>2008-02-1522:07:54.0</ns0:IdentifierEffectiveDate><ns0:IdentifierDefaultFlag>Y</ns0:IdentifierDefaultFlag><ns0:DataSourceProviderID>200015</ns0:DataSourceProviderID><ns0:DataSourceProviderName>CompA</ns0:DataSourceProviderName><ns0:ProfileID>111111</ns0:ProfileID></ns0:Identifier><ns0:Identifier><ns0:IdentifierTypeCode>202305</ns0:IdentifierTypeCode><ns0:IdentifierTypeDescription>DEANumber</ns0:IdentifierTypeDescription><ns0:IdentifierValueText>kkkkkk</ns0:IdentifierValueText><ns0:IdentifierStatusCode>0</ns0:IdentifierStatusCode><ns0:IdentifierStatusDescription>Active</ns0:IdentifierStatusDescription><ns0:IdentifierEffectiveDate>2008-02-1612:39:24.0</ns0:IdentifierEffectiveDate><ns0:IdentifierDefaultFlag>Y</ns0:IdentifierDefaultFlag><ns0:DataSourceProviderID>200024</ns0:DataSourceProviderID><ns0:DataSourceProviderName>CompA</ns0:DataSourceProviderName><ns0:ProfileID>222222</ns0:ProfileID></ns0:Identifier><ns0:Identifier><ns0:IdentifierTypeCode>202325</ns0:IdentifierTypeCode><ns0:IdentifierTypeDescription>PhysicianNumber</ns0:IdentifierTypeDescription><ns0:IdentifierValueText>yyyyy</ns0:IdentifierValueText><ns0:IdentifierStatusCode>0</ns0:IdentifierStatusCode><ns0:IdentifierStatusDescription>Active</ns0:IdentifierStatusDescription><ns0:IdentifierEffectiveDate>2011-11-1620:44:42.0</ns0:IdentifierEffectiveDate><ns0:IdentifierDefaultFlag>Y</ns0:IdentifierDefaultFlag><ns0:DataSourceProviderID>200069</ns0:DataSourceProviderID><ns0:DataSourceProviderName>CompA</ns0:DataSourceProviderName><ns0:ProfileID>3333333</ns0:ProfileID></ns0:Identifier><ns0:Identifier><ns0:IdentifierTypeCode>202300</ns0:IdentifierTypeCode><ns0:IdentifierTypeDescription>SFAID</ns0:IdentifierTypeDescription><ns0:IdentifierValueText>zzzzzz</ns0:IdentifierValueText><ns0:IdentifierStatusCode>0</ns0:IdentifierStatusCode><ns0:IdentifierStatusDescription>Active</ns0:IdentifierStatusDescription><ns0:IdentifierEffectiveDate>2003-02-1420:13:47.0</ns0:IdentifierEffectiveDate><ns0:IdentifierDefaultFlag>Y</ns0:IdentifierDefaultFlag><ns0:DataSourceProviderID>200078</ns0:DataSourceProviderID><ns0:DataSourceProviderName>CompA</ns0:DataSourceProviderName><ns0:ProfileID>444444</ns0:ProfileID></ns0:Identifier></xml>';
DOM.Document document = new DOM.Document();
document.load(xml);

for (DOM.XmlNode node : document.getRootElement().getChildren())
{
    DOM.XmlNode description = node.getChildElement('IdentifierTypeDescription', 'http://ns0');
    DOM.XmlNode value = node.getChildElement('IdentifierValueText', 'http://ns0');

    if (description != null && value != null)
    {
        system.debug(description.getText() + ' = ' + value.getText());
    }
}

Which gives me a readable log:

USER_DEBUG [12]|DEBUG|MENumber = xxxxx
USER_DEBUG [12]|DEBUG|DEANumber = kkkkkk
USER_DEBUG [12]|DEBUG|PhysicianNumber = yyyyy
USER_DEBUG [12]|DEBUG|SFAID = zzzzzz

1
  • Excellent! Thanks Adrian! I was trying to overcomplicate it somehow. Thanks again – Maneesh Sep 19 '17 at 3:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.