+ Start a Discussion
nagalakshminagalakshmi 

How parse the xml if we are having more than one elements

Hi,

 

I am trying to integrate the mail chimp with salesforce. For this i have wrote the http callouts. I am getting the result in xml. But the xml result having more than one elements. How can i parse this and how can i display all fields in vf page.

 

I have wrote the program like this.

public class mailchimp1
{
public string body1{set;get;}
String from_name;
String from_email;
public void detail()
{
Http h = new Http();
HttpRequest req = new HttpRequest();
final string username = 'xxxxxx';
final string password = 'xxxxx';
Blob headerValue = Blob.valueOf(username + ':' + password);
String authorizationHeader = 'BASIC ' +EncodingUtil.base64Encode(headerValue);
req.setHeader('Authorization',authorizationHeader);
//req.setHeader('Content-length', '1753' );
req.setHeader('Host','http://us5.api.mailchimp.com/1.3/ ');
req.setHeader('Connection','keep-alive');
req.setHeader('Content-Type', 'application/xml;charset=UTF-8');
req.setMethod('GET');
//req.setbody('http://api.createsend.com/api/v3/clients.xml');
//req.setEndpoint('http://us5.api.mailchimp.com/1.3/ ');
req.setEndpoint('http://us5.api.mailchimp.com/1.3/?output=xml&method=campaigns&apikey=eb7d2ec8ba15120cc1115c44d32a35cd-us5&campaign_id=89405&list_id=90cdce872a&start=0&limit=1000');
HttpResponse res = h.send(req);
system.debug('********'+res.getbody());
body1=res.getbody();
Dom.Document doc = res.getBodyDocument();
//Retrieve the root element for this document.
Dom.XMLNode address = doc.getRootElement();
from_name= address.getChildElement('from_name',from_name).getText();
from_email = address.getChildElement('from_email',from_email).getText();
 // print out specific elements
    
        System.debug('Name== ' + from_name);
        System.debug('email=== ' + from_email);
        
        // Alternatively, loop through the child elements.
    
        // This prints out all the elements of the address
    
        for(Dom.XMLNode child : address.getChildElements()) {
           System.debug('c==='+child.getText());
        }
}
}

 

I am getting the result as

<MCAPI type="array">
<total type="integer">21</total>
<data type="array">
<struct key="0" type="array">
<id type="string">5bef810a37</id>
<web_id type="double">94233</web_id>
<list_id type="string">90cdce872a</list_id>
<folder_id type="double">0</folder_id>
<template_id type="double">0</template_id>
<content_type type="string">html</content_type>
<title type="string">testvf</title>
<type type="string">regular</type>
<create_time type="string">2012-09-07 10:09:42</create_time>
<send_time type="NULL" />
<status type="string">save</status>
<from_name type="string">lakshmi</from_name>
<from_email type="string">nagalakshmi_b@dskvap.com</from_email>
<subject type="string">testvf</subject>
<to_name type="string">lakshmi</to_name>
<archive_url type="string">http://eepurl.com/psBDz</archive_url>
<emails_sent type="integer">0</emails_sent>
<inline_css type="boolean" />
<analytics type="string">N</analytics>
<analytics_tag type="string" />
<authenticate type="boolean" />
<ecomm360 type="boolean" />
<auto_tweet type="boolean" />
<auto_fb_post type="string" />
<auto_footer type="boolean" />
<timewarp type="boolean" />
<timewarp_schedule type="NULL" />
<tracking type="array">
<html_clicks type="boolean">1</html_clicks>
<text_clicks type="boolean">1</text_clicks>
<opens type="boolean">1</opens>
</tracking>
<segment_text type="string">No segment used</segment_text>
<segment_opts type="array" />
<type_opts type="array" />
</struct>
<struct key="3" type="array">
<id type="string">69c9dd75ee</id>
<web_id type="double">93077</web_id>
<list_id type="string">90cdce872a</list_id>
<folder_id type="double">0</folder_id>
<template_id type="double">0</template_id>
<content_type type="string">html</content_type>
<title type="string">campaignthroughform</title>
<type type="string">regular</type>
<create_time type="string">2012-09-06 12:34:28</create_time>
<send_time type="NULL" />
<status type="string">save</status>
<from_name type="string">lakshmi</from_name>
<from_email type="string">nagalakshmi_b@dskvap.com</from_email>
<subject type="string">campaignthroughform</subject>
<to_name type="string">naga</to_name>
<archive_url type="string">http://eepurl.com/pdzrz</archive_url>
<emails_sent type="integer">0</emails_sent>
<inline_css type="boolean" />
<analytics type="string">N</analytics>
<analytics_tag type="string" />
<authenticate type="boolean" />
<ecomm360 type="boolean" />
<auto_tweet type="boolean" />
<auto_fb_post type="string" />
<auto_footer type="boolean" />
<timewarp type="boolean" />
<timewarp_schedule type="NULL" />
<tracking type="array">
<html_clicks type="boolean">1</html_clicks>
<text_clicks type="boolean">1</text_clicks>
<opens type="boolean">1</opens>
</tracking>
<segment_text type="string">No segment used</segment_text>
<segment_opts type="array" />
<type_opts type="array" />
</struct>
<struct key="4" type="array">
<id type="string">45d150a7e8</id>
<web_id type="double">93069</web_id>
<list_id type="string">90cdce872a</list_id>
<folder_id type="double">0</folder_id>
<template_id type="double">0</template_id>
<content_type type="string">html</content_type>
<title type="string">samplecamapign</title>
<type type="string">regular</type>
<create_time type="string">2012-09-06 12:29:16</create_time>
<send_time type="NULL" />
<status type="string">save</status>
<from_name type="string">lakshmi</from_name>
<from_email type="string">nagalakshmi_b@dskvap.com</from_email>
<subject type="string">samplecamapign</subject>
<to_name type="string">naga</to_name>
<archive_url type="string">http://eepurl.com/pdzrD</archive_url>
<emails_sent type="integer">0</emails_sent>
<inline_css type="boolean" />
<analytics type="string">N</analytics>
<analytics_tag type="string" />
<authenticate type="boolean" />
<ecomm360 type="boolean" />
<auto_tweet type="boolean" />
<auto_fb_post type="string" />
<auto_footer type="boolean" />
<timewarp type="boolean" />
<timewarp_schedule type="NULL" />
<tracking type="array">
<html_clicks type="boolean">1</html_clicks>
<text_clicks type="boolean">1</text_clicks>
<opens type="boolean">1</opens>
</tracking>
<segment_text type="string">No segment used</segment_text>
<segment_opts type="array" />
<type_opts type="array" />
</struct>
</data>
</MCAPI>

When i am executing the above code i am getting error as 'Attempt to de-reference a null object' . Please any one help me.

 

Thanks,

Lakshmi

priyanka.mv26priyanka.mv26

Hi Lakshmi,

 

In Salesforce you have to use XmlStreamReader class to read a XML. but if you use this, you may face error like 'Too many script statements' if your XML is too large as you have to iterate your whole XML in for loop.

 

Use the "Fast Xml Dom" to read your XML. Below is the link to get "Fast XML DOM"

 

http://developer.force.com/projectpage?id=a06300000062Z2kAAE

 

Here is the link on how to use the Fast Xml Dom

 

http://www.tgerm.com/2010/03/fast-apex-xml-dom-quick-started-guide.html

 

Please let me know if you have any questions

 

Thanks,

Priyanka

nagalakshminagalakshmi

Hi Priyaka,

 

Thanks for your help... I have solved the issue.... But i have small issue on that... can you help me please... I am having 6 different element records. it reads only first element records... how can i read the remaining....

 

I have used the code like that

 

public class mailchimp1
{
public string body1{set;get;}
public class wrap
{
public String mid1{set;get;}
public String webid1{set;get;}
public String listid1{set;get;}
public String folderid1{set;get;}
public String templateid1{set;get;}
public String contenttype1{set;get;}
public String title1{set;get;}
public String type1{set;get;}
public String createtime1{set;get;}
public String status1{set;get;}
public String fromname1{set;get;}
public String fromemail1{set;get;}
public String subject1{set;get;}
public String toname1{set;get;}
public String archiveurl1{set;get;}
}
list<wrap> wlist=new list<wrap>();
public void detail()
{
Http h = new Http();
HttpRequest req = new HttpRequest();
final string username = 'xxxx';
final string password = 'xxx';
Blob headerValue = Blob.valueOf(username + ':' + password);
String authorizationHeader = 'BASIC ' +EncodingUtil.base64Encode(headerValue);
req.setHeader('Authorization',authorizationHeader);
//req.setHeader('Content-length', '1753' );
req.setHeader('Host','http://us5.api.mailchimp.com/1.3/ ');
req.setHeader('Connection','keep-alive');
req.setHeader('Content-Type', 'application/xml;charset=UTF-8');
req.setMethod('GET');
//req.setbody('http://api.createsend.com/api/v3/clients.xml');
//req.setEndpoint('http://us5.api.mailchimp.com/1.3/ ');
req.setEndpoint('http://us5.api.mailchimp.com/1.3/?output=xml&method=campaigns&apikey=eb7d2ec8ba15120cc1115c44d32a35cd-us5&campaign_id=89405&list_id=90cdce872a&start=0&limit=1000');
HttpResponse res = h.send(req);
system.debug('********'+res.getbody());
body1=res.getbody();
Dom.Document doc = res.getBodyDocument();
//Retrieve the root element for this document.
Dom.XMLNode address = doc.getRootElement();
system.debug('add==='+address);
dom.XMLNode  dataele= address.getChildElement('data',null);
system.debug('dt==='+dataele);
dom.XMLNode  structele= dataele.getChildElement('struct',null);
system.debug('st==='+structele);
String mid=structele.getChildElement('id', null).getText();
String webid=structele.getChildElement('web_id', null).getText();
String listid=structele.getChildElement('list_id', null).getText();
String folderid=structele.getChildElement('folder_id', null).getText();
String templateid=structele.getChildElement('template_id', null).getText();
String contenttype=structele.getChildElement('content_type', null).getText();
String title=structele.getChildElement('title',null).getText();
String type=structele.getChildElement('type', null).getText();
String createtime=structele.getChildElement('create_time', null).getText();
String status=structele.getChildElement('status', null).getText();
String fromname=structele.getChildElement('from_name', null).getText();
String fromemail=structele.getChildElement('from_email', null).getText();
String subject=structele.getChildElement('subject', null).getText();
String toname=structele.getChildElement('to_name', null).getText();
String archiveurl=structele.getChildElement('archive_url', null).getText();

system.debug('ii**ii'+mid);
       
        // Alternatively, loop through the child elements.
    
        // This prints out all the elements of the address
    
        for(Dom.XMLNode child : structele.getChildElements()) {
        wrap w=new wrap();
         w.mid1=structele.getChildElement('id', null).getText();
w.webid1=structele.getChildElement('web_id', null).getText();
w.listid1=structele.getChildElement('list_id', null).getText();
w.folderid1=structele.getChildElement('folder_id', null).getText();
w.templateid1=structele.getChildElement('template_id', null).getText();
w.contenttype1=structele.getChildElement('content_type', null).getText();
w.title1=structele.getChildElement('title',null).getText();
w.type1=structele.getChildElement('type', null).getText();
w.createtime1=structele.getChildElement('create_time', null).getText();
w.status1=structele.getChildElement('status', null).getText();
w.fromname1=structele.getChildElement('from_name', null).getText();
w.fromemail1=structele.getChildElement('from_email', null).getText();
w.subject1=structele.getChildElement('subject', null).getText();
w.toname1=structele.getChildElement('to_name', null).getText();
w.archiveurl1=structele.getChildElement('archive_url', null).getText();  
 wlist.add(w);          
   //structele.next();       
        }
}
public list<wrap> getdet()
{
return wlist;
}
}

 

mid1 displays 6 records for same id... It reads only first element.... can you help me please..

 

Thanks,

Lakshmi

 

priyanka.mv26priyanka.mv26

Hi Lakshmi,

 

Use the below code to get all the data

 

 

responseBody = res.getbody();
TG_XmlDom dom = new TG_XmlDom(responseBody);
List<TG_XmlNode> structele = dom.root.getElementsByTagName('struct');
if(!structele.isempty())
{
Integer structelesize = structele.size();
 for(integer i=0;i<structelesize;i++)
{
wrap w = new wrap();
w.mid1 = structele[i].getAttribute('id');
wlist.add(w);
}
}

 

This is just a sample to retrive ids list.. Please add all the attributes like web_id,list_id etc. to ur code.. please try and let me know if this works or not.. 

 

Thanks,
Priyanka

nagalakshminagalakshmi

Hi Priyaka,

 

while using this code, i am getting error as 'Invalid type: TG_XmlDom '. I have tried in different ways but not getting... Can you please help me..

 

Thanks.

priyanka.mv26priyanka.mv26

Have you downloaded and installed the "Fast Xml Dom"?

 

I have provided the link yesterday.. http://developer.force.com/projectpage?id=a06300000062Z2kAAE