I've imported a wsdl and generated the classes into apex.

I can call the web service ok, but the webservice returns an xml doc with content dependant on what xml doc you send it in the first place.

Is there a way I can generically process the response rather than it expecting the results to map directly to salesforce classes?

Thanks, David


Yes, you can use the XMLStreamReader class to parse the XML file. The downside is that you have to perform all the processing yourself (i.e. you have to step through the entire XML response and process the elements).

The general format works like this:

XmlStreamReader xml = new XmlStreamReader(xmlString);
while(xml.hasNext()) {
    if(xml.getEventType() == XmlTag.START_ELEMENT) {
        if(xml.getLocalName() == 'foo') {
            // etc
        if(xml.getLocalName() == 'bar') {
            // etc
        if(xml.getLocalName() == 'baz') {
            // etc

Read the docs for more details.


The WSDL might not help you in this case. Generally speaking, you can either use WSDL2Apex for everything or nothing at all. Fortunately, SOAP is pretty easy to write yourself, and you can use the XmlStreamWriter class to help you with this.

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.