- Home /
Is pulling and parsing XML data from 3rd party API call possible?
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
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.
Answer by aeroplane490 · Apr 24, 2017 at 07:52 AM
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 + "\n Param2 =" + param2.InnerXml + "\n\n";
}
}
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.
A single Debug.Log message is limited to 64k as far as i remember. So if your returned data is longer it will be truncated in the console.
Note that 64k usually is the memory limit in byte. Since a string contains chars which are 16 bit it basically should display no more than about 32k of characters.
ps: you have your totVal += ...
line outside the for loop. This wouldn't compile as param1 and 2 are declared inside the loop and can only be used there. Also if you have a lot data, you shouldn't use string concat operations. It will create massive amount of garbage. You may want to use a StringBuilder ins$$anonymous$$d. However, if it's just for debugging, you don't want a string that is too long. If you want to know if the right amount of xml elements have been parsed you should count them and print the count.
Awesome! That answers some of the questions I had. Thanks very much!
Your answer
Follow this Question
Related Questions
WWW is not ready downloading yet? 1 Answer
Parsing large XML files on-the-fly, best practice? 1 Answer
Does XML Serialization Support "Class Object" Arrays? 1 Answer
convert xml into array? 2 Answers
Handle HTML response 0 Answers