Search Unity

Nested xml.SelectNodes

Discussion in 'Scripting' started by ShaneTheVeryTall, Oct 1, 2011.

  1. ShaneTheVeryTall

    ShaneTheVeryTall

    Joined:
    Nov 28, 2009
    Posts:
    11
    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:

    Code (csharp):
    1.  
    2. function loadXML() {
    3.     var www = new WWW ("http://www.oldno19.com/upload/timeline.xml");
    4.     yield www;
    5.     var xml = new XmlDocument();
    6.     xml.LoadXml(www.text);
    7.    
    8.     var i : int = 1;
    9.     for (var node in xml.SelectNodes ("/how_we_got_here_stories/year/title")) {
    10.         var thisCube = Instantiate(cube, Vector3 (i*1.25, 1, 0), Quaternion.identity) as Transform;
    11.         yearTxt = thisCube.Find("YearText").GetComponent(TextMesh);
    12.         titleTxt = thisCube.Find("TitleText").GetComponent(TextMesh);
    13.         yearTxt.text = node.InnerText;
    14.         thisCube.transform.parent = this.transform;
    15.         i++;
    16.        
    17.         for (var node2 in node.SelectNodes ("/how_we_got_here_stories/year/how_we_got_here_story/title")) {
    18.             var thisCube2 = Instantiate(cube, Vector3 (i*1.25, 1, 0), Quaternion.identity) as Transform;
    19.             yearTxt = thisCube2.Find("YearText").GetComponent(TextMesh);
    20.             titleTxt = thisCube2.Find("TitleText").GetComponent(TextMesh);
    21.             yearTxt.text = "";
    22.             titleTxt.text = WrapTxt(node2.InnerText,charLimit);
    23.             thisCube2.transform.parent = this.transform;
    24.             i++;
    25.         }
    26.     }
    27. }
    28.  
    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 <how_we_got_here_story> 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!
     
  2. Krodil

    Krodil

    Joined:
    Jun 30, 2010
    Posts:
    141
    bump :)
    did you solve it?
     
unityunity