+ Start a Discussion
SF Beginner 2019SF Beginner 2019 

xml parsing Invalid Type.


I am trying to parse this 

<advertisers type="array">
    <id type="integer">7</id>
    <name>ABC Telecom</name>
    <id type="integer">106</id>
    <id type="integer">13</id>
    <name>Acme Corp</name>

with this code
public with sharing class xmltosobject{
    public string s='';
    public void readxml(ID TestObjectID){
        XMLParsing__c TObj = [SELECT xml__c FROM XMLParsing__c where id= : TestObjectID];
        s= TObj.Event_Message_XML__c ;
        List<ParsedXML__c> newaccounts = new List<ParsedXML__c> ();
        Dom.Document docx = new Dom.Document();
        dom.XmlNode xroot = docx.getrootelement() ;
        dom.XmlNode [] xrec = xroot.getchildelements() ; //Get all Record Elements
        for(Dom.XMLNode child : xrec) //Loop Through Records
            ParsedXML__c a = new ParsedXML__c();
            for (dom.XmlNode awr : child.getchildren() ) {
                if (awr.getname() == 'id') {
                    system.debug('Id' + awr.gettext());
                    a.accountnumber__c = awr.gettext();
                if (awr.getname() == 'name') {
                    system.debug('name' + awr.gettext());
                    a.name__c = awr.gettext();
        system.debug(newaccounts);//you could insert here or upsert based on ID with an external Id field

I got an invalid type when running this in execute anonymous
String str = '<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>';
xmltosobject.readxml objPartnerCreds = new xmltosobject.readxml();

Abdul KhatriAbdul Khatri
Not sure what are you trying to do. readxml is a method of class xmltosobject. 

Please first create the instance of class like this
xmltosobject objPartnerCreds = new xmltosobject();
Also readxml method is expecting an ID as a parameter which I believe is for the SObject XML_Parsing__c. So please make sure you pass this Id, I am not sure in whihc context like Trigger, on button click etc. you are calling this so can't much help further until you clarify this.