Loading xml files

From Unify Community Wiki
Revision as of 17:34, 19 September 2011 by TomoI (Talk | contribs)

Jump to: navigation, search
This page needs to be cleaned up.

Please refer to the Formatting Guidlines.

To load an XML file in Javascript with unity, I couldn't find any simple starter scripts so I thought this might be helpful as I had a hard time learning how do load XML with Javascript.

In order to load XML you must import System.Xml so Unity knows that it needs to parse an Xml file. The next step is to Load the Xml file so we use the Load("http://remotewebsite.com/file.zml") command; After that we basically loop through every root node which happens to be <playerdata> in this case(can be anything tho). While were looping through our root nodes with rootXml.MoveNext() we print out all tags inside the currently selected <playerdata> node.

import System.Xml;

function Start(){

}

function updatePlayers(){ //Get an update on player positions //Read Player data and commence functions accordingly var xmlData = new XmlDocument(); xmlData.Load("www.website.com/folder/loadPlayers.xml");

//Loop through all the <playerdata> nodes and print out there position & id data var rootXml = xmlData.DocumentElement.GetEnumerator(); while(rootXml.MoveNext()){ //Output the <tag> named "id"; Use innerText to get the value of <id> print(rootXml.Current.Item["id"].InnerText);

//Do the same with all the other tags that are nested inside the <playerdata>

} }

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox