XML data from an API to a displayable String?
Hey guys!
At the moment I am trying to get some XML data from the Open Weather API and display it as a string, I have been using some online documentation to display it in the debug, but I need help getting this information to Strings or Text so that I can display it within my scenes.
Below are a few of the methods I have tried to use.
This is WeatherAPI using UnityEngine; using System.Collections; using System.Xml; using System; using UnityEngine.UI; using System.IO;
public class WeatherAPI : MonoBehaviour {
public string City;
public Text Temperature;
public Text Humidity;
public Text Cloudiness;
public Text Title;
IEnumerator Start()
{
string url = "http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=MY API KEY";
WWW www = new WWW(url);
yield return www;
if (www.error == null){
Debug.Log("Loaded following XML " + www.data);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(www.data);
City = ("City: " + xmlDoc.SelectSingleNode("cities/list/item/city/@City").InnerText).ToString();
Debug.Log("Temperature: " + xmlDoc.SelectSingleNode("cities/list/item/temperature/@value").InnerText);
Debug.Log("humidity: " + xmlDoc.SelectSingleNode("cities/list/item/humidity /@value").InnerText);
Debug.Log("Cloud : " + xmlDoc.SelectSingleNode("cities/list/item/clouds/@value").InnerText);
Debug.Log("Title: " + xmlDoc.SelectSingleNode("cities /list/item/weather/@value").InnerText);
xmlDoc.PreserveWhitespace = true;
xmlDoc.Save("/Users/MY NAME/MY PROJECT/Assets/Weather.xml");
// City.text = "cities/list/item/city/@City";
Temperature.text = xmlDoc.SelectSingleNode("cities/list/item/temperature/@value").InnerText;
Humidity.text = xmlDoc.SelectSingleNode("cities/list/item/humidity /@value").InnerText;
Cloudiness.text = xmlDoc.SelectSingleNode("cities/list/item/clouds/@value").InnerText;
Title.text = xmlDoc.SelectSingleNode("cities /list/item/weather/@value").InnerText;
}
else
{
Debug.Log("ERROR: " + www.error);
}
}
}
This is how I was going to display the Text from another script (if text was possible)
DisplayWeather.cs`using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class displayWeather : MonoBehaviour {
public WeatherAPI _WeatherAPI;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
}
void SetTempData ()
{
_WeatherAPI.Temperature.text = "Temperature: " + _WeatherAPI.Temperature.ToString();
}
}`
Now I feel like I am on the right path and may need some pointers, but I might be completely wrong! I have also tried using an XML reader, but cant seem to get the XML files to save upon playing using Microsoft's XML .Save documentation.
Please help you guys!
Thank-you!
Your answer
Follow this Question
Related Questions
Masking based on angles 0 Answers
how to implement online leader board to my project? 0 Answers
Teleport 2D Character on TriggerEnter 2 Answers
Help with Trigger Colliders.. 0 Answers