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
Hersh321Hersh321 

Bind Xml Attribute to Salesforce Field

Hi Everyone,

Does anyone know how to bind the xml attribute to a Salesforce field.  For some reason all the records that are being created are null.  I am trying to get the player name and id.  

xml:

<?xml version="1.0" encoding="UTF-8"?>
<team xmlns="http://feed.elasticstats.com/schema/nfl/roster-v1.0.xsd" id="BAL" name="Ravens" market="Baltimore">
  <player id="c5dfc54e-fd64-468f-81a8-073918776412" name_full="Bernard Pierce" name_first="Bernard" name_last="Pierce" name_abbr="B.Pierce" birthdate="1990-05-10" birth_place="Ardmore, PA, USA" high_school="Glen Mills (PA)" height="72" weight="218" college="Temple" position="RB" jersey_number="30" status="ACT" salary="0" experience="2" draft_pick="84" draft_round="3" draft_team="BAL">
    <injuries>
      <injury id="b7fec083-21c4-4244-a705-d5af4b024f41" start_date="2014-09-18T00:00:00+00:00" game_status="PRO" practice_status="Full Participation">
        <description>Thigh</description>
        <notes>
</notes>
      </injury>
    </injuries>
  </player>
       
Apex:

        Http h = new Http();
       
        HttpRequest req = new HttpRequest();
        req.setEndpoint(url);
        req.setMethod('GET');
       
        HttpResponse res = h.send(req);
        Dom.Document docx = res.getBodyDocument();

        dom.XmlNode xroot = docx.getrootelement() ;
       
        dom.XmlNode [] xrec = xroot.getchildelements() ; //Get all Record Elements
       
        for(Dom.XMLNode child : xrec) //Loop Through Records
        {
         Roster__c rstr = new Roster__c();
       
          for (dom.XmlNode awr : child.getchildren() ) {
                                 if (awr.getname() == 'player') {
                                        rstr.PlayerID__c = awr.getAttributeValue ('id',null);
         } 
             
                                 if (awr.getname() == 'player') {
                                       rstr.Full_Name__c = awr.getAttributeValue ('name_full',null);
         } 
       
       
        }
        newroster.add(rstr);
        }
        system.debug(newroster);//you could insert here or upsert based on ID with an external Id field
        upsert newroster;
   }