function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Vamshi KrishnaVamshi Krishna 

Issue with mapping the fields when there is an apex webservice xml response

Apex call out has an xml response.How can I map the fields in Salesforce to the response I receieved. I could map the fields like clean etc. But how could I map fields like make,model, year relatively.
Assume the fields are : Make__c ,Model__c etc.... Considering the XML response.
XML response is:
<?xml version="1.0" encoding="UTF-8"?> <result> <vin>xxxxxxxxxxx</vin> <id>xxxxxxxxxxx</id> <date>2018-02-08 05:02:15 PST</date> <specs> <VIN /> *<Year>2011</Year> <Make>Chevrolet</Make> <Model>Suburban</Model>* <Trim>LS 1500</Trim></specs><clean>0</clean> <success>1</success> <error /> </result>



Apex callout:

 
public PageReference doVinExtract()
    { 

            HttpRequest httpRequest = new HttpRequest();
            httpRequest.setEndpoint('https:/VinProviderxxxx.php');
            String body = 'id=01123999999198&key=xxxxxxx&vin=1GNSKHE34BRxxxxxxx&mode=test&user=vxxxxxx&pass=slll4250&format=xml';
            httpRequest.setBody(body);
            httpRequest.setMethod('GET');
            Http htt = new hTTP();
            HttpResponse httpRe = htt.send(httpRequest);
            system.debug(httpRe.geTbody());
            String re = httpRe.geTbody();

            Dom.Document doc = httpRe.getBodyDocument();
            Dom.XMLNode address = doc.getRootElement();


        String vinDate= address.getChildElement('date', null).getText();

       String state = address.getChildElement('specs', null).getText();
        // print out specific elements

        System.debug('address: ' + address);
          System.debug('state: ' + state);



            system.debug('result'+re);


             for(Dom.XMLNode child : address.getChildElements()) {
             if(child.getName() == 'specs'){
           System.debug('******'+child.getText());
           }
        }
 I was struck and could not map these three fields:
*<Year>2011</Year> <Make>Chevrolet</Make> <Model>Suburban</Model>
              to the fields in salesforce.
 
Best Answer chosen by Vamshi Krishna
Raj VakatiRaj Vakati
Here is the parsing code  

 
String xml ='<?xml version="1.0" encoding="UTF-8"?> <result> <vin>xxxxxxxxxxx</vin> <id>xxxxxxxxxxx</id> <date>2018-02-08 05:02:15 PST</date> <specs> <Year>2011</Year> <Make>Chevrolet</Make> <Model>Suburban</Model>* <Trim>LS 1500</Trim></specs><clean>0</clean> <success>1</success> <error /> </result>';

Dom.Document doc = new Dom.Document();
doc.load(xml);
DOM.XmlNode rootNode=doc.getRootElement();
for (Dom.XMLNode child: rootNode.getChildElements()) {
    if (child.getNodeType() == DOM.XMLNodeType.ELEMENT) {
        if(child.getName()=='specs'){
            List<Dom.XMLNode> trempNode=   child.getChildElements();
            for(Dom.XMLNode nodeCH :trempNode ){
                if (nodeCH.getNodeType() == DOM.XMLNodeType.ELEMENT) {
                    system.debug(nodeCH.getName());
                    if(nodeCH.getName()=='Make'){
                        
                        System.debug('Make'+nodeCH.getText().trim());            
                    }
                    
                }
                
                
            }
            
        }
        
    }
}

 

All Answers

Raj VakatiRaj Vakati
Here is the parsing code  

 
String xml ='<?xml version="1.0" encoding="UTF-8"?> <result> <vin>xxxxxxxxxxx</vin> <id>xxxxxxxxxxx</id> <date>2018-02-08 05:02:15 PST</date> <specs> <Year>2011</Year> <Make>Chevrolet</Make> <Model>Suburban</Model>* <Trim>LS 1500</Trim></specs><clean>0</clean> <success>1</success> <error /> </result>';

Dom.Document doc = new Dom.Document();
doc.load(xml);
DOM.XmlNode rootNode=doc.getRootElement();
for (Dom.XMLNode child: rootNode.getChildElements()) {
    if (child.getNodeType() == DOM.XMLNodeType.ELEMENT) {
        if(child.getName()=='specs'){
            List<Dom.XMLNode> trempNode=   child.getChildElements();
            for(Dom.XMLNode nodeCH :trempNode ){
                if (nodeCH.getNodeType() == DOM.XMLNodeType.ELEMENT) {
                    system.debug(nodeCH.getName());
                    if(nodeCH.getName()=='Make'){
                        
                        System.debug('Make'+nodeCH.getText().trim());            
                    }
                    
                }
                
                
            }
            
        }
        
    }
}

 
This was selected as the best answer
Vamshi KrishnaVamshi Krishna
Thanks Raj.It worked