0

For example, if I have XML data with address records, I need to insert the address data of XML into Address fields in the Account object respectively

Can you please help... Update the code

public class XmlUtils {
  public static void parseContactAddress() {
    String s = '<advertisers type="array">' +
'<advertiser>   <id type="integer">7</id>  <name>ABC Telecom</name>   </advertiser> ' +
'<advertiser>   <id type="integer">106</id>  <name>ABC_Ozone</name>  </advertiser> ' +
'<advertiser>   <id type="integer">13</id>   <name>Acme Corp</name>  </advertiser> ' + 
'</advertisers> ';

List<Account> newaccounts = new List<Account> ();

Dom.Document docx = new Dom.Document();
docx.load(s);
dom.XmlNode xroot = docx.getrootelement() ;
dom.XmlNode [] xrec = xroot.getchildelements() ; //Get all Record Elements

for(Dom.XMLNode child : xrec) //Loop Through Records
{
 account a = new Account ();

  for (dom.XmlNode awr : child.getchildren() ) {
                         if (awr.getname() == 'id') {
                               system.debug('Id' + awr.gettext());
                               a.accountnumber = awr.gettext();
 }  
      
                         if (awr.getname() == 'name') {
                               system.debug('name' + awr.gettext());
                               a.name = awr.gettext();
 }  


}
newaccounts.add(a);
}
system.debug(newaccounts);//you could insert here or upsert based on ID with an external Id field
  }
}
2

1 Answer 1

1

That code needs to be in a class, and then in a method. In other words, what you have is a code snippet, not a complete solution. To get started, you'd wrap the code inside a class and method:

public class XmlUtils {
  public static void parseContactAddress() {
    String s = '<advertisers type="array">' +
'<advertiser>   <id type="integer">7</id>  <name>ABC Telecom</name>   </advertiser> ' +
'<advertiser>   <id type="integer">106</id>  <name>ABC_Ozone</name>  </advertiser> ' +
'<advertiser>   <id type="integer">13</id>   <name>Acme Corp</name>  </advertiser> ' + 
'</advertisers> ';

List<Account> newaccounts = new List<Account> ();

Dom.Document docx = new Dom.Document();
docx.load(s);
dom.XmlNode xroot = docx.getrootelement() ;
  // Rest of code...
  }
}

Which you'd then call using another class, or Execute Anonymous if you're just testing it:

XmlUtils.parseContactAddress();

To get to Execute Anonymous, use Debug > Open Execute Anonymous Window... from the Developer Console (the developer area you're already in).

0

You must log in to answer this question.