在手机读取xml文件,需要使用www的方式或者是streamreader的方式。
经试验,在android手机上读取persistance目录下的文件,读取不到,而streamreader的方式可以。
下面是读取的实例:
我们知道unity的若干目录,只有persistentDataPath目录下才能读和写,所以在记录写应用数据的时候,则需要在此目录完成了。
1、文件的路径:
我们希望在持久化目录下创建一个文件叫做gm.xml的文件
private string filePath = Application.persistentDataPath + "/gm.xml";
2、创建文件:
private void CreateFile()
{
if (!File.Exists(filePath))
{
try
{
string text = @"<?xml version=""1.0""?>" + "\r\n" + "<root></root>";
using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write))
{
StreamWriter sw = new StreamWriter(fs);
sw.Write(text);
sw.Flush();
sw.Close();
fs.Close();
}
}
catch (Exception e)
{
Debug.LogError("create gm.xml file failed! " + e.ToString() + " " + filePath);
}
}
}
3、读取文件:
private void LoadFile()
{
if (!File.Exists(filePath))
{
CreateFile();
}
else
{
using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Write))
{
StreamReader sw = new StreamReader(fs);
string str = sw.ReadToEnd();
XmlDocument doc = new XmlDocument();
doc.LoadXml(str);
// do sth....
}
}
}
4、写入文件:
private bool WriteFile()
{
try
{
if (!File.Exists(filePath))
{
File.Create(filePath);
}
StringBuilder sb = new StringBuilder();
string title = @"<?xml version=""1.0""?>";
sb.Append(title);
sb.AppendLine();
sb.Append("<root>");
sb.AppendLine();
using (FileStream fs = new FileStream(filePath, FileMode.Truncate, FileAccess.Write, FileShare.Write))
{
StreamWriter sw = new StreamWriter(fs);
foreach (var item in favouriateCmdDict)
{
string typeStr = string.Format("\t<group id=\"{0}\" name=\"{1}\">\r\n", item.Key.id, item.Key.name);
sb.Append(typeStr);
for (int i = 0; i < item.Value.Count; ++i)
{
Command cmd = item.Value[i];
cmd.isFavouriate = true;
string cmdStr = string.Format("\t\t<cmd id=\"{0}\" name=\"{1}\" cmd=\"{2}\"/>\r\n", cmd.id, cmd.name, cmd.cmd);
sb.Append(cmdStr);
}
sb.Append("\t</group>\r\n");
}
sb.Append("</root>");
sw.Write(sb.ToString());
sw.Flush();
sw.Close();
fs.Close();
}
return true;
}
catch
{
Debug.LogError("write file failed!");
return false;
}
}
5、解析xml:
xml的格式为:
<?xml version="1.0"?>
<root>
<group id="1" name="消耗">
<cmd id="1" name="物104+10" cmd="additem 104 10"/>
</group>
<group id="2" name="战斗">
<cmd id="1" name="xxx" cmd="kill me"/>
<cmd id="2" name="xxx" cmd="kill me"/>
</group>
</root>
节点类:
public class CommandType
{
public int id;
public string name;
}
public class Command
{
public int id { get; set; }
public int typeID { get; set; }
public string name { set; get; }
public string cmd { set; get; }
public bool isFavouriate { set; get; }
}
private void ParseXml(XmlDocument doc, Dictionary<CommandType, List<Command>> dict, bool isFavouriate = false)
{
XmlNode root = doc.SelectSingleNode("root");
foreach (XmlNode node in root.ChildNodes)
{
CommandType cmdType = new CommandType();
cmdType.id = int.Parse(node.Attributes["id"].Value);
cmdType.name = node.Attributes["name"].Value;
if (!IsExist(dict, cmdType))
{
dict.Add(cmdType, new List<Command>());
}
List<Command> list = dict[cmdType];
foreach (XmlNode child in node.ChildNodes)
{
Command cmd = new Command();
int id = 0;
int.TryParse(child.Attributes["id"].Value, out id);
cmd.id = id;
cmd.typeID = cmdType.id;
cmd.name = child.Attributes["name"].Value;
cmd.cmd = child.Attributes["cmd"].Value;
cmd.isFavouriate = isFavouriate;
list.Add(cmd);
}
}
}