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
unidhaunidha 

XMLReader for Inner Element

Hi,

 

I am trying to do tutorial on XMLReader, all went okay until I modified xml file to have element child  such as below.

 

<User xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
 <CreatedDate>11/7/2013 5:52:12 PM</CreatedDate>
<Id>31209297</Id>
  <DisplayName>Unidha</DisplayName>
<Email>unidha@yahoo.com</Email>
<UserGroups>
 <UserGroup><Id>301</Id><Name>Administrators</Name></UserGroup> <UserGroup><Id>302</Id><Name>Sales</Name></UserGroup>
</UserGroups>
</User>

 

 

I want to Group Id to List of String of Employee object.Here is my code, I am not able to read UserGroups element such as UserGroup Id and User Group Name.Any Idea?

 

 public class MyXmlStreamReader {

public List<Employee> listRecords = new List<Employee>();

   //object that hold information on transaction,user etc.
     class Employee{
        
       private List<String> GroupId{get;set;} //one user might have multiple group being assigned
         private Boolean autoProvision{get;set;}
        private Id userId{get;set;}
        private String email{get;set;}
        private String firstName{get;set;}
        private String lastName{get;set;}
        private String displayName{get;set;}
      
   
    }
    
    public void test() {
     String str = '<User xmlns:i="http://www.w3.org/2001/XMLSchema-instance">'
                  +'<CreatedDate>11/7/2013 5:52:12 PM</CreatedDate>'
                  +'<Id>31209297</Id>'
                   +'<DisplayName>Unidha</DisplayName>'
                   +'<Email>unidha@yahoo.com</Email>'
                   +'<UserGroups>'
                   +'<UserGroup><Id>301</Id><Name>Administrators</Name></UserGroup>'
                   +'<UserGroup><Id>302</Id><Name>Sales</Name></UserGroup>'
                   +'</UserGroups></User>';
                   
     XmlStreamReader xsr = new XmlStreamReader(str);
    readResponse(xsr);
  
     }

   public void readResponse(XmlStreamReader reader) {
     Employee empRecord; 
    // List of emp to store the value
    List < Employee > empList= new list < Employee > (); 

    while (reader.hasNext()) { 
          if (reader.getEventType() == XmlTag.START_ELEMENT) { 
             if ('User' == reader.getLocalName()) {
                  empRecord= new Employee ();
            } else if ('CreatedDate' == reader.getLocalName()) {
                 system.debug('@UNID--- '+getValueFromTag(reader)); 
            }
            
            else if ('Id' == reader.getLocalName()) {
                 system.debug('@UNID Id--- '+getValueFromTag(reader)); 
            }
             else if ('Display Name' == reader.getLocalName()) {
                empRecord.displayName= getValueFromTag(reader);
                system.debug('@UNID displayName =='+ empRecord.displayName);
            }
             else if ('Email' == reader.getLocalName()) {
                // If you find any other opening tag, extract the string value
                empRecord.email= getValueFromTag(reader);
                system.debug('@UNID Email=='+ empRecord.email);
            }
           else if('UserGroups' == reader.getLocalName()) {
                empRecord.GroupId=new List<String>();
                 system.debug('@UNID =='+ empRecord.email);
               
                      
            }
            else if('UserGroup' == reader.getLocalName()  ) {
            //HOW TO READ USER GROUP ELEMENT add the Id into GroupId
                   if(reader.getEventType() == XmlTag.CHARACTERS){
                    system.debug('@UNID =='+ empRecord.email);
                   
                     }
                empRecord.GroupId.add(getValueFromTag(reader));
                 }
           
        }
        
       /* else if (reader.getEventType() == XmlTag.END_ELEMENT){
        
              if('UserGroup' == reader.getLocalName()){
               system.debug('@UNIDEnd UserGroup=='+ getValueFromTag(reader));
              }
              
              else if('UserGroups' == reader.getLocalName()){
              system.debug('@UNIDEnd UserGroups=='+ getValueFromTag(reader));
              }
        }
        */
        
           
        
        reader.next();
    }
}
    
    
   // This is an extra function to read data between opening and closing tag. 
// It will return the string of value from between tags
public string getValueFromTag(XMLStreamReader reader) {
    String DataValue;

    while (reader.hasNext()) {
        if (reader.getEventType() == XmlTag.END_ELEMENT) {
            break;
        } else if (reader.getEventType() == XmlTag.CHARACTERS) {
            DataValue = reader.getText();
        }
        reader.next();
    }

    return DataValue;
}   
      


}

 

Thanks a lot.

 

Best Answer chosen by Admin (Salesforce Developers) 
unidhaunidha

I solved this.I used boolean isFlag to check when to get Group Id and when to get Id.So far it works.

 

  boolean isFlag=false;
    Employee   empRecord= new Employee ();
    empRecord.GroupId=new List<String>();
   
   while (reader.hasNext()) { 
       
        if (reader.getEventType() == XmlTag.START_ELEMENT) { 
           if ('Id' == reader.getLocalName()) {
              String strId= getValueFromTag(reader);
                          
              if(isFlag){
                 empRecord.GroupId.add(strId);
              }
             
            }
            else if ('DisplayName' == reader.getLocalName()) {
                empRecord.displayName= getValueFromTag(reader);
          
            }
             else if ('Email' == reader.getLocalName()) {
                empRecord.email= getValueFromTag(reader);
                
            }
           else if('UserGroups' == reader.getLocalName()) {
                isFlag=true;
            }      
            else if('City' == reader.getLocalName()) {
                empRecord.city= getValueFromTag(reader);
            }       
                
           }
          else if (reader.getEventType() == XmlTag.END_ELEMENT){
             if('UserGroups' == reader.getLocalName()){
                 isFlag=false;
             }
            }
      reader.next();
            
            
            }//end of while
     
      }

 I wrote detail at unid-machine.blogspot.com/2013/11/screaming-for-xmlstreamreader.html