Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
1
Question by axCed · Jun 20, 2013 at 08:44 PM · androidwwwxmlstreamingassets

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.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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.

Comment
Add comment · Show 4 · 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
avatar image axCed · Jun 21, 2013 at 03:20 PM 1
Share

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.

avatar image ben06feb axCed · Aug 29, 2016 at 07:43 AM 0
Share

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.

avatar image ben06feb ben06feb · Aug 30, 2016 at 05:23 AM 0
Share

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 ();
       }
 



avatar image koslovdenis axCed · Mar 08, 2019 at 05:50 PM 0
Share

I am using this path for Android:

 xml_path = Application.strea$$anonymous$$gAssetsPath + "/" + curr_file_name;


It's not working...

avatar image
-1

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

Comment
Add comment · Show 1 · 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
avatar image axCed · Sep 30, 2013 at 12:38 PM 1
Share

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.

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.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

ArgumentException: Path is empty while saving data in an XML 0 Answers

Read .xml file content with WWW class 0 Answers

In Unity 4.1 www.isDone doesn't seem to work in Android. Any suggestion? 0 Answers

Problem using StreamingAssets on Android build 0 Answers

StreamingAssets - reading from file on android 0 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