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
sampath kumar 3sampath kumar 3 

How to load and acess xml in apex and visualforce

I have an xml file ,Dont know how to load and retrive  the xml file using Apex and to pass the xml to My visualforce page. I have gone through some of the exmaples related to this scenario which is a book list example . I didnt understand that can anyone explain to load and fetch data from xml using Apex  
sampath kumar 3sampath kumar 3
Cany one help  me?
ManojjenaManojjena
Hi Sampath ,
Try with below code.
<apex:page controller="XMLParser" showHeader="false" sidebar="false">
    <apex:pageBlock >
        <apex:pageBlockTable value="{!WebDetail}" var="wd">
            <apex:column value="{!wd.billingEmail}" headerValue="BillingEmail"/>
            <apex:column value="{!wd.regdEmail}" headerValue="RegdEmail"/>
            <apex:column value="{!wd.invoiceId}" headerValue="InvoiceId"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

public class XMLParser {
    public objWebDetail WebDetail = new objWebDetail(); 
    
    public objWebDetail getWebDetail() {
        return WebDetail;
    } 
    public class objWebDetail {
        
        public string billingEmail{get;set;}
        public string regdEmail{get;set;}
        public string invoiceId{get;set;}
        
        
        // Constructor
        public objWebDetail () {
            string requiredXml = '<?xml version=\'1.0\' encoding=\'utf-8\'?>\n'+
            '<root>\n'+
            '<BillingEmail>support@svn.com</BillingEmail>\n'+
            '<BillingName>Nadim Sravan</BillingName>\n'+
            '<InvoiceId>42479649</InvoiceId>\n'+
            '</root>';
            dom.Document doc = new dom.Document();
            doc.load(requiredXml);
            from_xml(doc.getRootElement());
        }
         
        public void from_xml(dom.XmlNode node) {
             billingEmail = node.getChildElement('BillingEmail',null).getText();
             regdEmail = node.getChildElement('RegEmail',null).getText();
             invoiceId = node.getChildElement('InvoiceId',null).getText();

            }
              
         }
    }

Try to implement your XML file accordingly .
You need to convert your xml file to string then you can use .

Let me know if it helps !!
Thanks
Manoj
sampath kumar 3sampath kumar 3
@ Manoj: Thanks for the reply , how to pass the xml path ? For that do i need to create a dcoument or static resource to load xml . which means loading external xml file
 
ManojjenaManojjena
Hi Sampath,
Basically are you working on webservice ? If you are working on webservice then you rresponse will come as a xml file .The response you need to load like below .

dom.Document doc = new dom.Document();
           doc.load(requiredXml);
Here requiredXml is your response xml .Aftre that you need to parse .
check below class which is standard apex class ,which I have used for parsing .

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_xml_dom_xmlnode.htm
Thanks
Manoj
sampath kumar 3sampath kumar 3
I tried the above example like the way xmlparser and the vf  page but I got the error : An unexpected error has occurred. Your development organization has been notified. dont understand about this
ManojjenaManojjena
Hi Sampath,
if possible post your response or send to my email I will check and let you know the issue .
Thanks
manoj
sampath kumar 3sampath kumar 3
I drop a mail to you , Please check it out
sampath kumar 3sampath kumar 3
@Manoj: I tried with your code for xml retriving ,Getting unknown exceptions.I sent a mail to you kindly let me know