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
2
Question by usagi · Nov 22, 2012 at 09:08 AM · arrayxmlparser

XML parsing problem - array

Hi guys,

I have to implement a quiz game and I'm trying to parse an XML file in order to retrieve all the contained data and use them to fill some buttons and labels with questions and answers.

This is my XML:

 <?xml version="1.0" encoding="utf-8"?>
 <test nr="4">
     <quiz quizNr="1">
         <question>How are you?</question>
         <answers totalNr="2">
             <answer id="1" correct="true">Fine</answer>
             <answer id="2" correct="false">bad</answer>
         </answers>
     </quiz>
     <quiz quizNr="2">
         <question>What is your name</question>
         <answers totalNr="3">
             <answer id="1" correct="true">Mark</answer>
             <answer id="2" correct="false">John</answer>
             <answer id="3" correct="false">Paul</answer>
         </answers>
     </quiz>
 </test>


I'm able to parse it, but observing the inspector while running the simulation in the editor it seems that they are not stored correctly (that's a screencapture of my inspector).

alt text

I don't understand why Element1 and Element2 are empty and all the data about my second quiz are stored in Element3. Can someone help me in understanding what I did wrong?

This is my parser:

 import System.Xml;
 
 class Answer{
     var answerID:int = 0;
     var correct:String = " ";
     var text:String = " ";
 }
 
 class Quiz{
     var quizNr:int = 0;
     var questionText:String;
     var answerNr:int =0;
     var answers:Answer[];
 }
 
 class Test{
     var nr:int;
     var quiz:Quiz[];
 }
 
 var test:Test;
  
 function Start()
 {
     //readxml from chat.xml in project folder
       var reader:XmlReader = XmlReader.Create("chat.xml");
       
       reader.Read();
       
       do
       {
 
         //when you find a quesiti tag do this
         if(reader.IsStartElement("test"))
         {
             test.nr = parseInt(reader.GetAttribute("nr"));
             test.quiz = new Quiz[test.nr];
             
             for(var i:int = 0; i<test.nr; i++)
             {
                
                 reader.Read();
                 if(reader.IsStartElement("quiz"))
                 {  
                    test.quiz[i] = new Quiz();    
                    test.quiz[i].quizNr=parseInt(reader.GetAttribute("quizNr"));
                    
                    reader.Read();
                    
                    if(reader.IsStartElement("question"))
                    {
                         test.quiz[i].questionText = reader.ReadString();
                         
                         reader.Read();
                    
                        if(reader.IsStartElement("answers"))
                        {
                        
                             test.quiz[i].answerNr = parseInt(reader.GetAttribute("totalNr"));
             
                             test.quiz[i].answers = new Answer[test.quiz[i].answerNr];
                             
                             for (var j:int = 0; j <test.quiz[i].answerNr; j++)
                             {
                                 
                                 reader.Read();
                                 if(reader.IsStartElement("answer"))
                                 {            
                                     test.quiz[i].answers[j] = new Answer();
                                     
                                     test.quiz[i].answers[j].answerID = parseInt(reader.GetAttribute("id"));
                                     test.quiz[i].answers[j].correct = reader.GetAttribute("correct");
                                     test.quiz[i].answers[j].text = reader.ReadString();
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }while(reader.Read());
 }
 



inspector.png (17.7 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

web page html parsing 1 Answer

XML File, or Array? 1 Answer

UnityScript XML parser 0 Answers

How can I turn XML to array? 0 Answers

Unity3D: How to save an array of gameobjects with arrays of position in xml and load them back in correctly? 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