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
}
}