• 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 ShaneTheVeryTall · Oct 01, 2011 at 05:32 AM · javascriptxmlfor-loopnested

Nested SelectNodes

Hello, I'm trying to iterate through an XML file. I have nested iterations that are returning more data (all of it, actually) than I intend.

The xml is here: http://www.oldno19.com/upload/timeline.xml

Here is my code:

 function loadXML() {
 var www = new WWW ("http://www.oldno19.com/upload/timeline.xml");
 yield www;
 var xml = new XmlDocument();
 xml.LoadXml(www.text);
 
 var i : int = 1;
 for (var node in xml.SelectNodes ("/how_we_got_here_stories/year/title")) {
     var thisCube = Instantiate(cube, Vector3 (i*1.25, 1, 0), Quaternion.identity) as Transform;
     yearTxt = thisCube.Find("YearText").GetComponent(TextMesh);
     titleTxt = thisCube.Find("TitleText").GetComponent(TextMesh);
     yearTxt.text = node.InnerText;
     thisCube.transform.parent = this.transform;
     i++;
     
     for (var node2 in node.SelectNodes ("/how_we_got_here_stories/year/how_we_got_here_story/title")) {
         var thisCube2 = Instantiate(cube, Vector3 (i*1.25, 1, 0), Quaternion.identity) as Transform;
         yearTxt = thisCube2.Find("YearText").GetComponent(TextMesh);
         titleTxt = thisCube2.Find("TitleText").GetComponent(TextMesh); 
         yearTxt.text = "";
         titleTxt.text = WrapTxt(node2.InnerText,charLimit);
         thisCube2.transform.parent = this.transform;
         i++;
     }
 }

}

In the 2nd for-loop, I want it to iterate through the first <"year">'s children and return the <"how_we_got_here_story"> items that appear under the first year... then do the same for the next <"year">... but for each year, i end up with EVERY item.

I have been searching the msdn pages for xmldocument methods, but I'm uncertain how to use them since they don't give javascript examples. :/ Guess I should learn C# soon.

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
0
Best Answer

Answer by ShaneTheVeryTall · Oct 02, 2011 at 08:15 AM

Well I figured something out... less lines of code, actually. Not sure how I managed to finally figure it out, I just kept trying stuff until I got clues and then found some other problems to fix, etc... LUCKY!

Here's the code in case anyone else can use it:

 function loadXML() {
 var www = new WWW ("http://www.oldno19.com/upload/timeline.xml");
 yield www;
 var xml = new XmlDocument();
 xml.LoadXml(www.text);
 //print(xml.InnerXml);
 
 var i : int = 0;
 var totalCount : int = 0;
 var newYear : boolean = false;
 for (var node in xml.SelectNodes ("/how_we_got_here_stories/year/title")) {
     newYear = true;
     for (var node2 in node.SelectNodes("../how_we_got_here_story/title")) {
         var thisCube2 = Instantiate(cube, Vector3 (totalCount*1.25, 1, 0), Quaternion.identity) as Transform;
         yearTxt = thisCube2.Find("YearText").GetComponent(TextMesh);
         titleTxt = thisCube2.Find("TitleText").GetComponent(TextMesh);      
         if (newYear) {
             yearTxt.text = node.InnerText;
         }else{
             yearTxt.text = "";
         }
         newYear = false;
         titleTxt.text = WrapTxt(node2.InnerText,charLimit);
         thisCube2.transform.parent = this.transform;
         print("--"+node2.InnerText);
         totalCount++;
         //yield WaitForSeconds (1);
     }
     i++;
 }

}

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

for loop not working if I start at the end number. 1 Answer

Turn all objects in the array to make same thing 0 Answers

For statement stops function? 1 Answer

javascript: declaring a variable in 'while' loop 2 Answers

JS Loop question 2 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