- Home /
How to read xml file on Android using www and StreamingAssets?
For my Android game, I'm trying to read (and eventually write) to xml file. To do that, I'm using a StreamingAssets folder that I created in the Assets folder.
The main source of information are those from Unity doc: http://docs.unity3d.com/Documentation/Manual/StreamingAssets.html http://docs.unity3d.com/Documentation/ScriptReference/WWW.html
I tried many different paths and setting (External SDCard and Internal Only). I searched a lot for solution, but without success.
On my Android Nexus 7 tablet in my try catch I get: System.Xml.XmlException: Document element did not appear. Line 1, position1.
Line 1 of my xml file is the basic xml version="1.0" encoding="ISO-8859-15". If I change the file name to something inexistent, I get the same exception.
This is my current cleaned code:
//OurText inherits from MonoBehaviour.
//Text is inherited.
public class TextMainMenuLineTwo : OurText
{
protected override void Start ()
{
base.Start();
StartCoroutine( Init() );
}
public IEnumerator Init()
{
string file;
string fileName;
WWW www;
XmlDocument xmlDocument;
file = "Texts.xml";
fileName = "jar:file//" + Application.dataPath + "!/assets/" + file;
www = new WWW(fileName);
yield return www;
xmlDocument = new XmlDocument();
try
{
xmlDocument.LoadXml(www.text);
Text = xmlDocument.SelectSingleNode ("texts/mainMenuTitle").InnerText;
}
catch(Exception e)
{
Text = e.ToString();
}
}
}
Thank you all.
Answer by whydoidoit · Jun 21, 2013 at 10:01 AM
Well you need to use:
fileName = Application.streamingAssetsPath + "/" + file;
There's no "dataPath" in that.
You cannot write to StreamingAssets only read from there. One trick is to copy all of the files from StreamingAssets to persistentDataPath and read/write everything to there.
That's what I chose to use and it is working. The fact that the editor and the device work differently was confusing.
if (Application.platform == RuntimePlatform.OSXEditor)
fileName = "file://" + Application.strea$$anonymous$$gAssetsPath + "/" + file;
else
fileName = Application.strea$$anonymous$$gAssetsPath + "/" + file;
Your trick is what I needed to read the most. File management is not a problem. So, in the end, I'll use the persistentDataPath. I will use Strea$$anonymous$$gAssets to initialize the persistent data.
Thank you.
Application.strea$$anonymous$$gAssetsPath not working when uploading WebGL build in L$$anonymous$$S server, is there any alternative for that. Even by using WWW class not working.
Hi,
Its worked fine. We have to define the path before calling its function, because of the exception, it wasn't worked earlier.
public string filePath = Application.strea$$anonymous$$gAssetsPath + "/UserDetails.xml";
public string result = "";
void Awake () { filePath = Application.strea$$anonymous$$gAssetsPath + "/UserDetails.xml"; }
void Start () { StartCoroutine(userDetailsXmlPath() ); }
IEnumerator userDetailsXmlPath()
{
print (filePath);
if (filePath.Contains ("://") || filePath.Contains (":///")) {
WWW www = new WWW (filePath);
yield return www;
result = www.text;
print (result);
FetchUserDetails ();
} else {
result = File.ReadAllText (filePath);
print (result);
FetchUserDetails ();
}
}
public void FetchUserDetails()
{
XmlDocument userXml1 = new XmlDocument ();
// userXml1.Load(Application.strea$$anonymous$$gAssetsPath + "/UserDetails.xml");
// userXml1.Load(Application.dataPath + "js/UserDetails 1.xml");
// userXml1.LoadXml(userXml.text);
userXml1.LoadXml(result);
XmlNodeList userList = userXml1.GetElementsByTagName ("user");
foreach(XmlNode userValue in userList)
{
XmlNodeList userContent = userValue.ChildNodes;
objUser = new Dictionary<string, string>();
foreach(XmlNode value in userContent)
{
objUser.Add (value.Name, value.InnerText);
}
userFullDetails.Add (objUser);
userCountInXml = userList.Count;
userId = new string[userList.Count];
questionSetOfUser = new string[userList.Count];
}
AssignUserXmlValuesToArray ();
}
I am using this path for Android:
xml_path = Application.strea$$anonymous$$gAssetsPath + "/" + curr_file_name;
It's not working...
Answer by shayan_315 · Sep 28, 2013 at 12:10 AM
please make simple example because i test all ways even what u say about reading Streaming Assets folder by www class but it does not work,then please make a simple program in unity and release it as soon as it possible (I want to read MSacess file in unity)i download 3 sample thorough internet but all of them does not work correctly
Please don't post comment as answer. $$anonymous$$S Access is a database, so it might be a different topic. I can't help more. $$anonymous$$aybe you should create your own question.