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