Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by peejaygee · Sep 13, 2014 at 06:03 PM · arraywwwxmlapiparse

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

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Apr 24, 2017 at 08:15 AM 1
Share

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.

avatar image aeroplane490 Bunny83 · Apr 24, 2017 at 06:10 PM 0
Share

Awesome! That answers some of the questions I had. Thanks very much!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges