Loading xml files

From Unify Community Wiki
Jump to: navigation, search


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 to 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 command:

 Load("http://remotewebsite.com/file.xml");

After that we basically loop through every root node which happens to be <playerdata> in this case(can be anything tho). While we're 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("http://www.website.com/folder/loadPlayers.xml");
 
	//Loop through all the <playerdata> nodes and print out their 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