1

I'm trying to construct an XML with attributes from and object (in this case Account).

public string createXmlFromAccount(Account acc){
String sResOutPut = ''; 
Contact[] theContacts = [SELECT id FROM Contact where AccountId =:acc.Id LIMIT 50];
sResOutPut = '<?xml version="1.0" encoding="UTF-8"?>';
        sResOutPut += '<SOAP-ENV:Envelope xmlns:SOAPENV="http://schemas.xmlsoap.org/soap/envelope/">';
        sResOutPut += '<SOAP-ENV:Body>';
        sResOutPut += '<DocList>';
        sResOutPut += '<SFValues>';
        sResOutPut += '<sistema>2</sistema>';
        sResOutPut += '<evento>I</evento>';
        sResOutPut += '<objeto>2</objeto>';
        sResOutPut += '</SFValues>';
        sResOutPut += '<BOM>';
        sResOutPut += '<BO>';
        sResOutPut += '<BusinessPartners>';
        sResOutPut += '<row>';
        sResOutPut += '<CardCode>'+checkNullString(acc.Codigo__c)+'</CardCode>';
        sResOutPut += '<CardName>'+checkNullString(acc.Name)+'</CardName>';
        sResOutPut += '<GroupCode>'+checkNullString(acc.Grupo__c)+'</GroupCode>';
        sResOutPut += '<Currency>'+checkNullString(acc.Moneda__c)+'</Currency>';
        sResOutPut += '<FederalTaxID>'+checkNullString(acc.Rut__c)+'</FederalTaxID>';
        sResOutPut += '<Notes>'+''+'</Notes>';
        sResOutPut += '<Phone1>'+checkNullString(acc.Phone)+'</Phone1>';
        sResOutPut += '<PayTermsGrpCode>'+checkNullString(acc.Condicion_de_Pago__c)+'</PayTermsGrpCode>';
        sResOutPut += '<EmailAddress>'+checkNullString(acc.E_mail__c) +'</EmailAddress>';
        sResOutPut += '<Valid>'+checkNullString(acc.Activo__c)+'</Valid>';
        sResOutPut += '<U_Tipo>'+checkNullString(acc.Tipo_socio_negocio__c)+'</U_Tipo>';
        sResOutPut += '<U_CLASIFICACION>'+checkNullString(acc.Clasificacion__c)+'</U_CLASIFICACION>';
        sResOutPut += '<U_EJECUTIVA>'+checkNullString(acc.Ejecutiva__c)+'</U_EJECUTIVA>';
        sResOutPut += '</row>';
        sResOutPut += '</BusinessPartners>';
        sResOutPut += '<BPAddresses>';
        sResOutPut += '<row>';
        sResOutPut += '<AddressType>'+checkNullString(acc.Tipo_Direccion__c)+'</AddressType>';
        sResOutPut += '<AddressName>'+checkNullString(acc.ID_direccion__c)+'</AddressName>';
        sResOutPut += '<Street>'+checkNullString(acc.Calle_Numero__c)+'</Street>';
        sResOutPut += '<Country>'+checkNullString(acc.Pais__C)+'</Country>';
        sResOutPut += '<City>'+checkNullString(acc.Regiones__c)+'</City>';
        sResOutPut += '<ZipCode>'+checkNullString(acc.Clasificacion_Direcciones__c)+'</ZipCode>';
        sResOutPut += '<County>'+checkNullString(acc.Comunas__c)+'</County>';
        sResOutPut += '</row>';
        sResOutPut += '</BPAddresses>';
        sResOutPut += '<ContactEmployees>';
        sResOutPut += '<row>';
        sResOutPut += '<CardCode>Sin Info Desde SalesForce</CardCode>';
        sResOutPut += '<Name>Sin Info Desde SalesForce</Name>';
        sResOutPut += '<Address>Sin Info Desde SalesForce</Address>';
        sResOutPut += '<FirstName>Sin Info Desde SalesForce</FirstName>';
        sResOutPut += '<Phone1>Sin Info Desde SalesForce</Phone1>';
        sResOutPut += '<Phone2>Sin Info Desde SalesForce</Phone2>';
        sResOutPut += '<MobilePhone>Sin Info Desde SalesForce</MobilePhone>';
        sResOutPut += '<E_Mail>Sin Info Desde SalesForce</E_Mail>';
        sResOutPut += '<Remarks1 />';
        sResOutPut += '<Remarks2 />';
        sResOutPut += '</row>';
        sResOutPut += '</BO>';
        sResOutPut += '</BOM>';
        sResOutPut += '</DocList>';
        sResOutPut += '</SOAP-ENV:Body>';
        sResOutPut += '</SOAP-ENV:Envelope>';
        return sResOutPut;
}

(checkNullString() is a method to check if the attribute from the Account is empty)

My problem is that when I try to load into an Dom.Document, the following error appears.

System.XmlException: Failed to parse XML due to: could not determine namespace bound to element prefix SOAP-ENV (position: START_DOCUMENT seen ...nvelope xmlns:SOAPENV="http://schemas.xmlsoap.org/soap/envelope/">... @1:115)

Any help will be appreciatted.

Thanks.

1

You've a typo in your XML. It should be

xmlns:SOAP-ENV

Instead of

xmlns:SOAPENV

I strongly suggest you use Dom.Document and Dom.XmlNode instead of manually piecing together strings. Your code will run in to further problems if there are restricted characters in any of those fields.

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.