2

I have an XML response, using XmlStreamReader I am getting data. The data is encapsulated between <Requisition> </Requisition>. The response XML contains multiple 'Requisition' tag set. The XML looks something like this.

 <e:Requisition>
     <e:Collaborators>
          <e:User>
              <e:EmployeeID>EMP12345</e:EmployeeID>
           </e:User>
      </e:Collaborators>
          .
          .
          //some other tags
          .
          .
          <e:RecruiterOwner>
                  <e:User>
                    <e:EmployeeID>ROI12345</e:EmployeeID>
                 </e:User>
        </e:RecruiterOwner>
 </e:Requisition>
 <e:Requisition>
     <e:Collaborators>
          <e:User>
              <e:EmployeeID>EMP555555</e:EmployeeID>
           </e:User>
      </e:Collaborators>
          .
          .
          //some other tags
          .
          .
          <e:RecruiterOwner>
                  <e:User>
                    <e:EmployeeID>ROI777777</e:EmployeeID>
                 </e:User>
        </e:RecruiterOwner>
 </e:Requisition>

I have written a function which retrieves EmployeeID for 'Collaborators' and 'RecruiterOwner'.

     try{   
        //First while to check Requisition start element    
               while(reader.hasNext()){
                 if(reader.getEventType() == XmlTag.START_ELEMENT && 'Requisition' == reader.getLocalName()){

                           //Loops to get Employee ID of collaborators
                           //First look for Collaborator tag, then User and finally get Employee Id     
                             while (reader.hasNext())
                        {
                             if (reader.getEventType() == XmlTag.START_ELEMENT && 'Collaborators' == reader.getLocalName()) 
                                {
                                    while (reader.hasNext()) 
                                    {
                                        if (reader.getEventType() == XmlTag.START_ELEMENT && 'User' == reader.getLocalName()) 
                                        {
                                            system.debug('+++++'+ reader.getLocalName());
                                            while(reader.hasNext()){
                                                 if(  reader.getEventType() == XmlTag.END_ELEMENT){
                                                    system.debug('++++breaking EmployeeId+++');
                                                    break;          
                                                    }else if( reader.getEventType() == XmlTag.CHARACTERS){
                                                        system.debug('------- EmployeeId ----------  '+reader.getText());

                                                    }
                                                reader.next();
                                            }
                                        }else if(  reader.getEventType() == XmlTag.END_ELEMENT && 'User' == reader.getLocalName()){
                                            system.debug('++++breaking 1 User1+++');
                                            break;          
                                            }
                                        reader.next();
                                    }

                                } else if(  reader.getEventType() == XmlTag.END_ELEMENT && 'Collaborators' == reader.getLocalName()){
                                    system.debug('++++breaking Collaborators+++');
                                    break ;         
                                }

                                reader.next();
                            }


                          //End of Collaborator loop

                           //Loops to get Employee ID of Recruiter Owner
                           //First look for RecruiterOwner tag, then User and finally get Employee Id           
                        while (reader.hasNext()) 
                        {
                             if (reader.getEventType() == XmlTag.START_ELEMENT && 'RecruiterOwner' == reader.getLocalName()) 
                                {
                                    while (reader.hasNext()) 
                                    {
                                        if (reader.getEventType() == XmlTag.START_ELEMENT && 'User' == reader.getLocalName()) 
                                        {
                                            system.debug('+++++'+ reader.getLocalName());
                                            while(reader.hasNext()){
                                                 if(  reader.getEventType() == XmlTag.END_ELEMENT){
                                                    system.debug('++++breaking RecruiterOwner EmployeeId+++');
                                                    break;          
                                                    }else if( reader.getEventType() == XmlTag.CHARACTERS){
                                                        system.debug('------- RecruiterOwner EmployeeId ----------  '+reader.getText());

                                                    }
                                                reader.next();
                                            }
                                        }else if(  reader.getEventType() == XmlTag.END_ELEMENT && 'User' == reader.getLocalName()){
                                            system.debug('++++breaking RecruiterOwner User1+++');
                                            break;          
                                            }
                                        reader.next();
                                    }

                                } else if(  reader.getEventType() == XmlTag.END_ELEMENT && 'RecruiterOwner' == reader.getLocalName()){
                                    system.debug('++++breaking RecruiterOwner+++');
                                    break ;         
                                }

                                reader.next();
                                //system.debug('--- after breaking RecruiterOwner ------' + reader.getLocalName());
                            }

                }else if(reader.getEventType() == XmlTag.END_ELEMENT && 'Requisition' == reader.getLocalName()){
                                       system.debug('---- breaking Requisition ----');
                                       break;          
                                    }
                reader.next();
                //break;
                //system.debug('--- after breaking Requisition ------' + reader.getLocalName());
            }
         }
    catch(exception e){

    }

The above code retrieves the two employee ids for the first requisition, but it breaks after the first set of 'Requisition'. I am unable to iterate over the entire set of requisitions to get all the Employee Ids.

I apologize, if the formatting is not correct!!!

2

@Yash Mehta I think you should try using xml dom reader class which would help you to iterate all requistions nodes instead of the first one. I think your code is just looping through the first node. So here is what you can do.

  1. Try to read the xml and load it on a DOM XML node.
  2. Iterate through the node for the child elements and get the values you want. sample code to load xml to dom node

    private DOM.Xmlnode  loadXmlFromStaticResource()
    {
      DOM.Xmlnode root;
      StaticResource sr = [select Body from StaticResource where Name =:XML_STATIC_RESOURCE];
      String xmlContent = sr.Body.toString();
      DOM.Document doc = new DOM.Document();
      try 
      {
        doc.load(xmlContent);    
        root = doc.getRootElement();        
      } 
      catch (System.XMLException e) 
      {  // invalid XML
        System.debug('Exception in parsing xml '+e);
      }
    
      return root;         
    }
    

Iterate through the node and get all elements..

    screenDriverNode = this.loadXmlFromStaticResource();
    //get Module Node and key value
    moduleNode = screenDriverNode.getChildElement(this.module,null);

    System.debug('node name for screen driver node'+screenDriverNode.getName());

    System.debug('node name for module node'+moduleNode.getName() + 'module'+this.module);
    System.debug('applicationId passed in populatescreenfrom xml method '+applicationId);
    lstScreenItemNode = moduleNode.getChildElements();

    for (Dom.XMLNode screenItemNode: lstscreenItemNode) 
    {
        screenItemSeq = screenItemNode.getChildElement(SCREEN_ITEM_DISP_SEQ, null).getText();
        screenId = screenItemNode.getChildElement(SCREEN_ITEM_SCREEN_ID, null).getText();
        //renderScreen = screenItemNode.getChildElement(SCREEN_ITEM_RENDER_SCREEN, null).getText();
        //isQuestionPage = screenItemNode.getChildElement(SCREEN_ITEM_IS_QUESTION, null).getText();
        status = screenItemNode.getChildElement(SCREEN_ITEM_STATUS, null).getText();
    }

This is just a sample code. You can search for reading xml through xml dom class and this should help. Please select like or answered if you like my answer.

1

Is there some wrapper xml element that you're not showing, its not valid xml to have more than one element at the root, there's probably an xml parser error getting thrown for that which you're not seeing because you have the whole thing wrapped in a try/catch where you're just ignoring the exception.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.