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
AbAb 

VF table or page to display XML data

Hello,

I get a response XML.

How can i build a VF page to display the data ?

Thanks
Best Answer chosen by Ab
ManojjenaManojjena
Hi Sandrine ,
You need to parse the XML response with help of Node , Document  Or XML reader class .While parsing the file you need to create a wrapper class with variable as your response .Each response tag which ever you want to dispaly needs to bind in teh wrapper and finally you need to create a wrappr list and display in VF with help of repeat or page block table .

Check below code which is to prepare a list og wrapper in Apex then that wrapper you can use in VF .
 
public class XMLConveterWithDocumentAndXmlNode{
	//Variable declaration
    public String  requiredXml;
    public String blingEmail{get;set;}
    public String regdEmail{get;set;}
    public String esmExp{get;set;}
    public String esmPrgm{get;set;}
    public String invcId{get;set;}
    public String prdName{get;set;}
    public String prseDate{get;set;}
    //constructor declaration
    public  XMLConveterWithDocumentAndXmlNode(){
     requiredXml=
            '<?xml version=\'1.0\' encoding=\'utf-8\'?>'+
            '<root>'+
            '<BillingEmail>billing@gmail.com</BillingEmail>'+
            '<BillingName>Nadim Sravan</BillingName>'+
            '<Expiration/>'+
            '<Program>Test Program</Program>'+
            '<InvoiceId>42479649</InvoiceId>'+
            '<ProductName>Test Product</ProductName>'+
            '<PurchaseDate>2015-10-18</PurchaseDate>'+
            '<RegEmail>test@gmail.com</RegEmail>'+
            '<RegName>Elvedin Kendic</RegName>'+
            '<SKU>DEMO</SKU>'+
            '<SN>Test SN</SN>'+
            '<Status>Normal</Status>'+
            '</root>';
           //Instantiate Document class
          Dom.Document doc=new Dom.Document();
          //Loading the Xml File
            doc.load(requiredXml);
            //getting the root element
            Dom.XMLNode address=doc.getRootElement();
            //traverse through the list of child element inside the root element
            for(Dom.XMLNode child : address.getChildElements()) {
              //Checking the element name and adding to the classlevel variable
                if(child.getName()=='BillingEmail')blingEmail= child.getText();
                if(child.getName()=='RegEmail')regdEmail= child.getText();
                if(child.getName()=='InvoiceId')invcId= child.getText();
                if(child.getName()=='ProductName')prdName= child.getText();
                if(child.getName()=='PurchaseDate')prseDate= child.getText();
                if(child.getName()=='Program')esmPrgm= child.getText();
                if(child.getName()=='Expiration')esmExp= child.getText();
             
           }
      } 
      //method to display the element value in page block
     public List<XmlWrapper> getXmlWrapper(){
        //List of wrapper class 
        List<XmlWrapper> listXmlWrp=new List<XmlWrapper>();
        //Instance of wrapper class to add the value in wrapper property and adding to the above list 
        XmlWrapper xwrp=new XmlWrapper();
            xwrp.billingEmail=blingEmail;
            xwrp.billingName=regdEmail;
            xwrp.esmExpiration=esmExp;
            xwrp.esmProgram=esmPrgm;
            xwrp.invoiceId=invcId;
            xwrp.productName=prdName;
            xwrp.purchaseDate=prseDate;
            listXmlWrp.add(xwrp);
            return listXmlWrp;
    } 
    //wrapper class to collect the  value of child elements 
    public class XmlWrapper{
       public String billingEmail{get;set;}
       public String billingName{get;set;}
       public String esmExpiration{get;set;}
       public String esmProgram{get;set;}
       public String invoiceId{get;set;}
       public String productName{get;set;}
       public String purchaseDate{get;set;}
       public XmlWrapper(){}
   } 
}
Let me know if it helps !!
Thanks
Manoj

 

All Answers

ManojjenaManojjena
Hi Sandrine ,
You need to parse the XML response with help of Node , Document  Or XML reader class .While parsing the file you need to create a wrapper class with variable as your response .Each response tag which ever you want to dispaly needs to bind in teh wrapper and finally you need to create a wrappr list and display in VF with help of repeat or page block table .

Check below code which is to prepare a list og wrapper in Apex then that wrapper you can use in VF .
 
public class XMLConveterWithDocumentAndXmlNode{
	//Variable declaration
    public String  requiredXml;
    public String blingEmail{get;set;}
    public String regdEmail{get;set;}
    public String esmExp{get;set;}
    public String esmPrgm{get;set;}
    public String invcId{get;set;}
    public String prdName{get;set;}
    public String prseDate{get;set;}
    //constructor declaration
    public  XMLConveterWithDocumentAndXmlNode(){
     requiredXml=
            '<?xml version=\'1.0\' encoding=\'utf-8\'?>'+
            '<root>'+
            '<BillingEmail>billing@gmail.com</BillingEmail>'+
            '<BillingName>Nadim Sravan</BillingName>'+
            '<Expiration/>'+
            '<Program>Test Program</Program>'+
            '<InvoiceId>42479649</InvoiceId>'+
            '<ProductName>Test Product</ProductName>'+
            '<PurchaseDate>2015-10-18</PurchaseDate>'+
            '<RegEmail>test@gmail.com</RegEmail>'+
            '<RegName>Elvedin Kendic</RegName>'+
            '<SKU>DEMO</SKU>'+
            '<SN>Test SN</SN>'+
            '<Status>Normal</Status>'+
            '</root>';
           //Instantiate Document class
          Dom.Document doc=new Dom.Document();
          //Loading the Xml File
            doc.load(requiredXml);
            //getting the root element
            Dom.XMLNode address=doc.getRootElement();
            //traverse through the list of child element inside the root element
            for(Dom.XMLNode child : address.getChildElements()) {
              //Checking the element name and adding to the classlevel variable
                if(child.getName()=='BillingEmail')blingEmail= child.getText();
                if(child.getName()=='RegEmail')regdEmail= child.getText();
                if(child.getName()=='InvoiceId')invcId= child.getText();
                if(child.getName()=='ProductName')prdName= child.getText();
                if(child.getName()=='PurchaseDate')prseDate= child.getText();
                if(child.getName()=='Program')esmPrgm= child.getText();
                if(child.getName()=='Expiration')esmExp= child.getText();
             
           }
      } 
      //method to display the element value in page block
     public List<XmlWrapper> getXmlWrapper(){
        //List of wrapper class 
        List<XmlWrapper> listXmlWrp=new List<XmlWrapper>();
        //Instance of wrapper class to add the value in wrapper property and adding to the above list 
        XmlWrapper xwrp=new XmlWrapper();
            xwrp.billingEmail=blingEmail;
            xwrp.billingName=regdEmail;
            xwrp.esmExpiration=esmExp;
            xwrp.esmProgram=esmPrgm;
            xwrp.invoiceId=invcId;
            xwrp.productName=prdName;
            xwrp.purchaseDate=prseDate;
            listXmlWrp.add(xwrp);
            return listXmlWrp;
    } 
    //wrapper class to collect the  value of child elements 
    public class XmlWrapper{
       public String billingEmail{get;set;}
       public String billingName{get;set;}
       public String esmExpiration{get;set;}
       public String esmProgram{get;set;}
       public String invoiceId{get;set;}
       public String productName{get;set;}
       public String purchaseDate{get;set;}
       public XmlWrapper(){}
   } 
}
Let me know if it helps !!
Thanks
Manoj

 
This was selected as the best answer
ManojjenaManojjena
DesaiDesai
@Manojjena, could you please let us know how to get the values on VF page ?

Thanks