- Home /
getting certain strings from WWW.text? (scoreoid)
Hello all, I am using an open web api known as Scoreoid
in order to implement high score funcionality. I can use the getScore() function to return what appears to be the contents of an XML file. As far as I know, all the stored data uses XML on a server, but I'm not too good in networking of any sort.
Anyways, I can use WWW.text to get the contents of the returned..page? Here is what is returned
request completed!: <?xml version="1.0" encoding="UTF-8" ?><scores><player username="teh" email="" first_name="" last_name="" platform=""><score score="9989" created="2012-08-01 23:41:05" difficulty="0" platform="" /></player><player username="teh" email="" first_name="" last_name="" platform=""><score score="9989" created="2012-07-30 01:04:01" difficulty="0" platform="" /></player><player username="teh" email="" first_name="" last_name="" platform=""><score score="9989" created="2012-07-30 01:03:22" difficulty="0" platform="" /></player><player username="teh" email="" first_name="" last_name="" platform=""><score score="9989" created="2012-07-29 21:03:40" difficulty="0" platform="" /></player><player username="teh" email="" first_name="" last_name="" platform=""><score score="9989" created="2012-07-29 19:20:43" difficulty="0" platform="" /></player></scores>
UnityEngine.Debug:Log(Object)
$:MoveNext() (at Assets/Scripts/Chat.js:47)
This is nice, but how can I grab for example, the name of the user who submitted a score? I can't exactly use any XML parsing techniques, as all I have is the text data.
I could be overthinking all this. But here is the full code if necessary.
function Start () {
/* API method */
var url = "https://www.scoreoid.com/api/createScore";
var url2 = "https://www.scoreoid.com/api/getScores";
/* Unity WWW Class used for HTTP Request */
var form = new WWWForm();
form.AddField( "api_key", "string_edited_for_security" );
form.AddField( "game_id", "string_edited");
form.AddField( "response", "xml");
form.AddField( "username", "teh");
form.AddField( "score", "9989");
var www = new WWW( url, form );
/* Wait for request to complete */
yield www;
/* Check for errors */
if (www.error == null)
{
/* Request completed! */
Debug.Log("request completed!: " + www.text);
} else {
/* Something wrong! */
Debug.Log("WWW Error: "+ www.error);
}
var form2 = new WWWForm();
form2.AddField( "api_key", "edited_string" );
form2.AddField( "game_id", "edit_string");
form2.AddField( "response", "xml");
var www2 = new WWW( url2, form2 );
/* Wait for request to complete */
yield www2;
/* Check for errors */
if (www2.error == null)
{
/* Request completed! */
Debug.Log("request completed!: " + www2.text);
} else {
/* Something wrong! */
Debug.Log("WWW Error: "+ www2.error);
}
}
Answer by rutter · Aug 01, 2012 at 11:36 PM
I'm not familiar with the specific API you're using, but you should probably try using some of .NET's built-in XML parsers.
Some helpful threads to start from:
You can try to do the parsing, yourself, by checking for substrings, writing regular expressions, and so on, but it's usually not the best idea.
Answer by DaveA · Aug 01, 2012 at 11:49 PM
You could use XML parsing, like:
xml = new XmlDocument();
xml.LoadXml(www.text);
XmlNode plyrNode = xml.FirstChild; // gets 'player' node
string fname = plyrNode.Attributes["first_name"].Value;
But the overhead of including and using an XML parser might be overkill.
You could just use standard string methods to dig the info out of the string. Smaller faster code (but may become a burden if you have large data sets)
There's probabaly a RegEx that would parse things out for you, I'm not sure.
Answer by loihb · Aug 01, 2012 at 11:52 PM
Save - load script settings from xml
Download :
https://docs.google.com/open?id=0B0aMXRZ2PWXZQjhKMXdDLVJLZDA