I’ve looked around, I’ve google’d around, I can find various bit of source code, but nothing seems to match what i need.
Various Links I’ve found
Accessing XML through WWW
Scripting API:WWW
Upload & Download XML Files
I am looking for a way of passing over a URL, the parameters of the URL can vary, can have additional commands in there, but the result is generally the same.
I have a XML file which is returned to me (if I pass the URL in a web browser) is generally laid out like.
<response status="ok" version="1.2" si:noNamespaceSchemaLocation="[]">
<artists>
<page>1</page>
<pageSize>50</pageSize>
<totalItems>44773</totalItems>
<artist id="12345">
<name>Name1</name>
<sortName>Name1</sortName>
<url>weburlhere</url>
<image>artworkurlhere</image>
<popularity>0.48</popularity>
</artist>
<artist id="567890">
<name>Name 2</name>
<sortName>Name 2</sortName>
<url>weburlhere</url>
<image>artworkurlhere</image>
<popularity>0.27</popularity>
</artist>
I would like to be able to pass a WWW URL, and take back into Unity what is returned via XML and then parse that data into some sort of array to be able to reference it.
Is it possible? or should I be looking at pulling the data, and saving the data, then load it back in a parse it?
Hope someone can help me.
Thanks in advance.
1 Answer
1
With regards to accessing the data, you can just do this:
WWW www = new WWW("https://www.mytestwebsite.com/api_xml_call");
yield return www;
Debug.Log(www.text);
As far as parsing it goes, I’m having a similar issue where I can only see the headers of the xml, and not the actual parts with the data I need. I’m thinking to somehow save the webpage as an xml file, then parse that, which can easily be done like:
void parseXmlFile(string xmlData)
{
string totVal = "";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(new StringReader(xmlData));
string xmlPathPattern = "//firstID/secondID";
XmlNodeList myNodeList = xmlDoc.SelectNodes(xmlPathPattern);
foreach(XmlNode node in myNodeList)
{
XmlNode param1 = node.FirstChild;
XmlNode param2 = param1.NextSibling;
totVal += "Param 1 =" + param1.InnerXml + "
Param2 =" + param2.InnerXml + "
";
}
}
In short, you can use third party APIs to call xml data. Parsing that data is also possible, but the specifics I’m having trouble with, which leads me to believe either Unity is having an issue displaying all the data despite having all been collected, or the data would need to be saved locally then parsed.