cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - You can Bookmark boards, posts or articles that you'd like to access again easily! X

Problem in yahoo weather

mpatel-21
12-Amethyst

Problem in yahoo weather

Hello Expert ,

I am getting this error on get yahoo weather :

If you know the solution for this error please suggest me.@

Execution error in service script [WeatherThing GetWeather] : Wrapped org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 429; The content of elements must consist of well-formed character data or markup. Cause: The content of elements must consist of well-formed character data or markup.

Code :

var stringLocation=location+" ";

var arrayLocations=stringLocation.split(",");

var lat=arrayLocations[0];

var long=arrayLocations[1];

//Modify the open weather url here if you are using your own API key

var params = {

url: "http://api.openweathermap.org/data/2.5/forecast?lat="+lat+"&lon="+long+ "&units=imperial" +"&type=accurate&mode=xml&APPID=4abc8a3c9f101ccf9c7cfe7cbf3dfed7" /* STRING */, 

    timeout: 60 /* NUMBER */

};

// result: XML

var xmlPage = Resources["ContentLoaderFunctions"].LoadXML(params);   // Error Generate on This line

var params = {

    infoTableName : "InfoTable",

    dataShapeName : "OpenWeatherFeed"

};

// CreateInfoTableFromDataShape(infoTableName:STRING("InfoTable"), dataShapeName:STRING):INFOTABLE(YahooWeatherFeed)

var result = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(params);

var from_today_value=xmlPage.forecast.time[0].@from.substring(0,10);

logger.warn("from_today_value= "+from_today_value);

for each (var item in xmlPage.forecast.time) {

    var row = new Object();

    var from_current_value=item.@from.substring(0,10);

    if(from_current_value!=from_today_value){

        row.WindDirection = item.windDirection.@name;

        row.WindSpeed=item.windSpeed.@mps;

        row.WeatherTemp=parseFloat(item.temperature.@value);

        row.WeatherHumidity=parseFloat(item.humidity.@value);

        row.From=item.@from;

        row.To=item.@to;

        row.Title=xmlPage.location.name+" "+xmlPage.location.country;

        row.Precipitation=(item.precipitation.@type+" "+item.precipitation.@value)==" "?"No Precipitation":item.precipitation.@type;

        result.AddRow(row);

    }

}

Thanks ,

Mayank

ACCEPTED SOLUTION

Accepted Solutions
vmihai
12-Amethyst
(To:mpatel-21)

Hi Mayank,

Yahoo Weather API is no longer in service. Please use OpenWeatherMap as a replacement or Wunderground API.

Below is an implementation for a  OpenWeatherMap API service call:

var stringLocation=location+" ";

var arrayLocations=stringLocation.split(",");

var lat=arrayLocations[0];

var long=arrayLocations[1];

var params = {

url: "http://api.openweathermap.org/data/2.5/forecast?lat="+lat+"&lon="+long+"&units=metric&type=accurate&mode=xml&APPID=4abc8a3c9f101ccf9c7cfe7cbf3dfed7" /* STRING */,

timeout: 160 /* NUMBER */

};

// result: XML

var xmlPage = Resources["ContentLoaderFunctions"].LoadXML(params);

var params = {

infoTableName : "InfoTable",

dataShapeName : "OpenWeatherFeed"

};

var result = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(params);

var from_today_value=xmlPage.forecast.time[0].@from.substring(0,10);

  1. logger.warn("from_today_value= "+from_today_value);

for each (var item in xmlPage.forecast.time) {

var row = new Object();

var from_current_value=item.@from.substring(0,10);

row.WindDirection = item.windDirection.@name;

    row.WindSpeed=item.windSpeed.@mps;

row.WeatherTemp=parseInt(item.temperature.@value);

row.WeatherHumidity=parseFloat(item.humidity.@value);

row.From =  item.@from;

row.To =  item.@to;

row.Title=xmlPage.location.name+" "+xmlPage.location.country;

row.Precipitation=(item.precipitation.@type+" "+item.precipitation.@value)==" "?"NoPrecipitation":item.precipitation.@type;

result.AddRow(row);

}


Here are the inputs and outputs of the service

Please let me know if you are in need of anything else.

Thank you,

Veronica

View solution in original post

14 REPLIES 14
Aanjan
12-Amethyst
(To:mpatel-21)

Mayank, you might've already checked this, but just in case - are you getting a valid xml from that url?

mpatel-21
12-Amethyst
(To:Aanjan)

Yes Ravi , i got valid xml and Json also , but problem in parsing this xml and as well json in Thingworx service, I tested this online parser and no any error shown me on online parser. Would you provide me any url for weather. so i can test it.

Thanks ,

vmihai
12-Amethyst
(To:mpatel-21)

Hi Mayank,

Yahoo Weather API is no longer in service. Please use OpenWeatherMap as a replacement or Wunderground API.

Below is an implementation for a  OpenWeatherMap API service call:

var stringLocation=location+" ";

var arrayLocations=stringLocation.split(",");

var lat=arrayLocations[0];

var long=arrayLocations[1];

var params = {

url: "http://api.openweathermap.org/data/2.5/forecast?lat="+lat+"&lon="+long+"&units=metric&type=accurate&mode=xml&APPID=4abc8a3c9f101ccf9c7cfe7cbf3dfed7" /* STRING */,

timeout: 160 /* NUMBER */

};

// result: XML

var xmlPage = Resources["ContentLoaderFunctions"].LoadXML(params);

var params = {

infoTableName : "InfoTable",

dataShapeName : "OpenWeatherFeed"

};

var result = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(params);

var from_today_value=xmlPage.forecast.time[0].@from.substring(0,10);

  1. logger.warn("from_today_value= "+from_today_value);

for each (var item in xmlPage.forecast.time) {

var row = new Object();

var from_current_value=item.@from.substring(0,10);

row.WindDirection = item.windDirection.@name;

    row.WindSpeed=item.windSpeed.@mps;

row.WeatherTemp=parseInt(item.temperature.@value);

row.WeatherHumidity=parseFloat(item.humidity.@value);

row.From =  item.@from;

row.To =  item.@to;

row.Title=xmlPage.location.name+" "+xmlPage.location.country;

row.Precipitation=(item.precipitation.@type+" "+item.precipitation.@value)==" "?"NoPrecipitation":item.precipitation.@type;

result.AddRow(row);

}


Here are the inputs and outputs of the service

Please let me know if you are in need of anything else.

Thank you,

Veronica

rzalman
1-Visitor
(To:vmihai)

Hi Veronica,

this code is for forecast in weather application, is possible to get code for actual weather in same application? If I am wrong please correct me.

http://openweathermap.org/forecast5  Here you use this, it's for forecast in 3 hours interval

http://openweathermap.org/current

And i would like to get data from this page if it is possible.

So i would like exchange yahoo weather for this one:YahooWeatherFeed.

Thank you for any reply,

Robert

vmihai
12-Amethyst
(To:rzalman)

Hi Robert,

Indeed, the code I posted is for retrieving forecast data. You can use the  Rest call described in the tutorial link you mentioned.

The parsing of the data once you get it in your thingworx service will be different from the one did for the forecast call. Let me know if you are experiencing problems with parsing it.

Thank you,

Veronica

rzalman
1-Visitor
(To:vmihai)

I am working on parsing of the data but it still doesn't work. If you can help me with that i will be glad.

Thank you,

Robert

vmihai
12-Amethyst
(To:rzalman)

Hi Robert,

I would be glad to help. Please post the code your are experiencing trouble with.

Thank you,

Veronica

rzalman
1-Visitor
(To:vmihai)

I have solved it but thank you for your interest.

Robert.

vmihai
12-Amethyst
(To:rzalman)

You're welcome Robert. Please don't hesitate to post your questions on the forum if you have trouble with anything else.

Thank you,

Veronica

vmihai
12-Amethyst
(To:rzalman)

You're welcome Robert. Please don't hesitate to post your questions on the forum if you have trouble with anything else.

Thank you,

Veronica

hlee1
1-Visitor
(To:vmihai)

Hi,

I'm still having an error message of

  • org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog Cause: Content is not allowed in prolog

I guess it's related to this issue

( Mark S. Kolich - Java: Resolving org.xml.sax.SAXParseException: Content is not allowed in prolog )

But I have no idea how to modify the code

Here is the code

var stringLocation=location+" ";

var arrayLocations=stringLocation.split(",");

var lat=arrayLocations[0];

var long=arrayLocations[1];

var params = {

url: "http://api.openweathermap.org/data/2.5/forecast?lat="+lat+"&lon="+long+"&units=metric&type=accurate&mode=xml&APPID=ff07c7c18d3a5d86f13a93134f1d94a9" /* STRING */,

timeout: 160 /* NUMBER */

};

// result: XML

var xmlPage = Resources["ContentLoaderFunctions"].LoadXML(params);

var params = {

infoTableName : "InfoTable",

dataShapeName : "OpenWeatherFeed"

};

var result = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(params);

var from_today_value=xmlPage.forecast.time[0].@from.substring(0,10);

logger.warn("from_today_value= "+from_today_value);

for each (var item in xmlPage.forecast.time) {

var row = new Object();

var from_current_value=item.@from.substring(0,10);

row.WindDirection = item.windDirection.@name;

    row.WindSpeed=item.windSpeed.@mps;

row.WeatherTemp=parseInt(item.temperature.@value);

row.WeatherHumidity=parseFloat(item.humidity.@value);

row.From =  item.@from;

row.To =  item.@to;

row.Title=xmlPage.location.name+" "+xmlPage.location.country;

row.Precipitation=(item.precipitation.@type+" "+item.precipitation.@value)==" "?"NoPrecipitation":item.precipitation.@type;

result.AddRow(row);

}

Thank you.

mpatel-21
12-Amethyst
(To:hlee1)

Hello Lee ,

Take input parameter as a Location ,

var test=Location+" ";

var arrayLocations=test.split(",");

var lat=arrayLocations[0];

var long=arrayLocations[1];

var prm = {

    url : "http://api.openweathermap.org/data/2.5/forecast?lat="+lat+"&lon="+long+"&units=metric&type=accurate&mode=xml&APPID=4abc8a3c9f101ccf9c7cfe7cbf3dfed7",

    timeout: 160 /* NUMBER */  

};

var xmlPage = Resources["ContentLoaderFunctions"].LoadXML(prm);

// result: XML

var params = {

    infoTableName : "InfoTable",

    dataShapeName : "OpenWeatherFeed"

};

var result = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(params);

var from_today_value=xmlPage.forecast.time[0].@from.substring(0,10);

logger.warn("from_today_value= "+from_today_value);

for each (var item in xmlPage.forecast.time) {

    var row = new Object();

    var from_current_value=item.@from.substring(0,10);

    row.WindDirection = item.windDirection.@name;

    row.WindSpeed=item.windSpeed.@mps;

    row.WeatherTemp=parseInt(item.temperature.@value);

    row.WeatherHumidity=parseFloat(item.humidity.@value);

    row.From =  item.@from;

    row.To =  item.@to;

    row.Title=xmlPage.location.name+" "+xmlPage.location.country;

    //  row.Precipitation=(item.precipitation.@type+" "+item.precipitation.@value)==" "?"NoPrecipitation":item.precipitation.@type;

    result.AddRow(row);

}

Thanks ,

Mayank

Hello Im have a diferent error mi code is

// Need to make a free wunderground.com account to get this key

var apiKey = "381603bc1002b5ce";

var temperature;

var humidity;

  //Inputted location needs to be valid lat, long: 40.7127, 74.0059

if(location != null){

    var test=location+" ";

var arrayLocations=test.split(",");

var lat=arrayLocations[0];

var long=arrayLocations[1];

var prm = {

    url : "http://api.openweathermap.org/data/2.5/forecast?lat="+lat+"&lon="+long+"&units=metric&type=accurate&mode=xml&APPID=4abc8a3c9f101ccf9c7cfe7cbf3dfed7",

    timeout: 160 /* NUMBER */ 

};

var xmlPage = Resources["ContentLoaderFunctions"].LoadXML(prm);

// result: XML

var params = {

    infoTableName : "InfoTable",

    dataShapeName : "OpenWeatherFeed"

};

var result = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(params);

var from_today_value=xmlPage.forecast.time[0].@from.substring(0,10);

logger.warn("from_today_value= "+from_today_value);

for each (var item in xmlPage.forecast.time) {

    var row = new O

atbject();

    var from_current_value=item.@from.substring(0,10);

    row.WindDirection = item.windDirection.@name;

    row.WindSpeed=item.windSpeed.@mps;

    row.WeatherTemp=parseInt(item.temperature.@value);

    row.WeatherHumidity=parseFloat(item.humidity.@value);

    row.From =  item.@from;

    row.To =  item.@to;

    row.Title=xmlPage.location.name+" "+xmlPage.location.country;

    //  row.Precipitation=(item.precipitation.@type+" "+item.precipitation.@value)==" "?"NoPrecipitation":item.precipitation.@type;

    result.AddRow(row);

}

    }

attach image error.png

Have you create a data shape for store value ?

Announcements


Top Tags