+ Start a Discussion

How to loop the XML tags




Here is my code.



trigger dmldata on CnPData__c (before insert,before update) {
 Custom_Question__c[] question = new Custom_Question__c[0];
String dataxml;
  for(CnP__CnPData__c cpdata:Trigger.new){
  XmlStreamReader reader= new XmlStreamReader(dataxml);
                Custom_Question__c cq = parseQuestion(reader);
                System.debug('questions after parsinggg'+question);
                insert question;
Custom_Question__c parseQuestion(XmlStreamReader reader5){
     Custom_Question__c qstn= new Custom_Question__c();
    while(reader5.hasNext()) {
     System.debug('event typpeeee in transaction'+reader5.getLocalName());
  if('CustomFieldList' == reader.getLocalName() && reader.getEventType() == XmlTag.END_ELEMENT) {
        else if('FieldName' == reader.getLocalName() && reader.getEventType() == XmlTag.START_ELEMENT){
          if(reader.getEventType() == XmlTag.CHARACTERS) { 
  List<CnP_Transaction__c> cnptransact = [Select Id,Name from CnP_Transaction__c where Name=:ordnum];
            qstn.Name = cnptransact[0].Name;
            qstn.Custom_Question__c= reader.getText();
            System.debug('Question  Nameeeeeeee'+qstn.Name);
        else if('FieldValue' == reader.getLocalName() && reader.getEventType() == XmlTag.START_ELEMENT){
          if(reader.getEventType() == XmlTag.CHARACTERS) { 
            qstn.Answer__c= reader.getText();
            System.debug('Question  Answer'+qstn.Answer__c);
    return qstn;





And Here is my xml file.



<?xml version="1.0" encoding="utf-8"?>








        <FieldValue>Good Company</FieldValue>




        <FieldValue>Good Job</FieldValue>



        <FieldName>Place of Employment</FieldName>

        <FieldValue>Blacksburg, Virginia</FieldValue>










My problem is: I can insert a record in custom question easily. but I need to insert 3 records of Custom questions for a single order number(thatis transaction name). How can I loop the code so that 3 custom questions for a single order number has been araised



Thanks in advance.

Best Answer chosen by Admin (Salesforce Developers) 

All Answers

Afzal MohammadAfzal Mohammad
This was selected as the best answer

Hi Afzal,



Thanks alot. I solved my problem with your link.


Thanks a  ton.