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
cloudleadercloudleader 

parse xml response which has to show on vf page

Hello,

 

I want to display the following xml response in a vf page

 

<?xml version="1.0" encoding="utf-8"?>
<weatherdata>
  <location>
    <name>Hyderabad</name>
    <type/>
    <country>IN</country>
    <timezone/>
    <location altitude="0" latitude="17.37528" longitude="78.474442" geobase="geonames" geobaseid="0"/>
  </location>
  <credit/>
  <meta>
    <lastupdate>2013-09-27T05:10:00</lastupdate>
    <calctime>0.0148</calctime>
    <nextupdate>2013-09-27T08:10:00</nextupdate>
  </meta>
  <sun rise="2013-09-27T00:35:45" set="2013-09-27T12:38:05"/>
  <forecast>
    <time day="2013-09-27">
      <symbol number="501" name="moderate rain" var="10d"/>
      <precipitation value="5.5" type="rain"/>
      <windDirection deg="274" code="W" name="West"/>
      <windSpeed mps="4.36" name="Gentle Breeze"/>
      <temperature day="32.04" min="23.12" max="33.35" night="23.12" eve="33.35" morn="28"/>
      <pressure unit="hPa" value="961.68"/>
      <humidity value="73" unit="%"/>
      <clouds value="sky is clear" all="8" unit="%"/>
    </time>
    <time day="2013-09-28">
      <symbol number="800" name="sky is clear" var="01n"/>
      <precipitation/>
      <windDirection deg="296" code="WNW" name="West-northwest"/>
      <windSpeed mps="3.8" name="Gentle Breeze"/>
      <temperature day="23.72" min="23.72" max="30.26" night="24.37" eve="30.26" morn="23.72"/>
      <pressure unit="hPa" value="960.8"/>
      <humidity value="92" unit="%"/>
      <clouds value="sky is clear" all="0" unit="%"/>
    </time>
    <time day="2013-09-29">
      <symbol number="800" name="sky is clear" var="01d"/>
      <precipitation/>
      <windDirection deg="288" code="WNW" name="West-northwest"/>
      <windSpeed mps="4.21" name="Gentle Breeze"/>
      <temperature day="27.32" min="23.76" max="31.25" night="24.6" eve="30.27" morn="23.76"/>
      <pressure unit="hPa" value="960.04"/>
      <humidity value="78" unit="%"/>
      <clouds value="sky is clear" all="0" unit="%"/>
    </time>
    <time day="2013-09-30">
      <symbol number="803" name="broken clouds" var="04n"/>
      <precipitation/>
      <windDirection deg="267" code="W" name="West"/>
      <windSpeed mps="4.46" name="Gentle Breeze"/>
      <temperature day="22.68" min="22.68" max="28.61" night="24.53" eve="28.61" morn="22.68"/>
      <pressure unit="hPa" value="957.04"/>
      <humidity value="92" unit="%"/>
      <clouds value="broken clouds" all="56" unit="%"/>
    </time>
    <time day="2013-10-01">
      <symbol number="501" name="moderate rain" var="10d"/>
      <precipitation value="7.45" type="rain"/>
      <windDirection deg="302" code="WNW" name="West-northwest"/>
      <windSpeed mps="5.01" name="Gentle Breeze"/>
      <temperature day="26.6" min="21.68" max="27.03" night="27.03" eve="26.6" morn="21.68"/>
      <pressure unit="hPa" value="960.13"/>
      <humidity value="0" unit="%"/>
      <clouds value="few clouds" all="14" unit="%"/>
    </time>
    <time day="2013-10-02">
      <symbol number="501" name="moderate rain" var="10d"/>
      <precipitation value="4.68" type="rain"/>
      <windDirection deg="284" code="WNW" name="West-northwest"/>
      <windSpeed mps="6.47" name="Moderate breeze"/>
      <temperature day="27.03" min="23.29" max="27.7" night="23.29" eve="27.7" morn="27.03"/>
      <pressure unit="hPa" value="959.65"/>
      <humidity value="0" unit="%"/>
      <clouds value="few clouds" all="18" unit="%"/>
    </time>
    <time day="2013-10-03">
      <symbol number="501" name="moderate rain" var="10d"/>
      <precipitation value="3.25" type="rain"/>
      <windDirection deg="283" code="WNW" name="West-northwest"/>
      <windSpeed mps="7.78" name="Moderate breeze"/>
      <temperature day="26.75" min="22.33" max="27.19" night="24.95" eve="27.19" morn="22.33"/>
      <pressure unit="hPa" value="958.26"/>
      <humidity value="0" unit="%"/>
      <clouds value="few clouds" all="17" unit="%"/>
    </time>
  </forecast>
</weatherdata>

 

jordi vosjordi vos

You probably have to give some more information...

 

What you could do, is something like this:

 

Dom.Document doc = new Dom.Document();

doc.load(yourstringwiththexml);

 

DOM.XMLNode root = doc.getRootElement();

for(Dom.XMLNode dataNode : root.getChildren()) {

loop trough the fields this needs some more code.

}

 

define some custom classes like:

 

public class WeatherData {

     Location location;

     Forcast forcast;

}

 

public class Location {

     public String locationName;

     public String locationType;

     public String locationCountry;

     public String locationTimezone;

}

 

public class Forecast {

     list <ForcastTime> forcastTimes;

}

 

public class ForcastTime {

     String symbolNumber;

     etc.

}

 

When you loop trough the XML, you create the classes and populate them. Then you return the weatherData class to your visualForce page.

 

This is somewhat typed from the head so you'll have to dive into this yourself.

cloudleadercloudleader

Hi

Thanks for the reply,i'm very new to this i'm unable to parse and display it on vf page

my requirement is

i have to display forecast details of a particular city which is entered by user in a input text box on the vf page could you please elaborate  your answer.

jordi vosjordi vos

If you're new to this than I advise you to read some documentation first on Visualforce and Apex.

 

Here is some documentation on XML parsing:

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_xml_dom.htm

 

This is not something basic so this may be a bit over your head if you're new to this.