unity中WWW读取xml
2017-11-27 20:54
134 查看
https://code.csdn.net/snippets/2603028
using UnityEngine;
using System.IO;
using System.Xml;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System;
public class loadxml : MonoBehaviour {
#if UNITY_PC
public string url;
#elif UNITY_ANDROID
private string url =(Application.streamingAssetsPath + "/android/xml/fbxt.xml" ).Trim(); //3w读取数据路径
#endif
// Use this for initialization
void Start () {
Debug.Log( url );
StartCoroutine( loadxmltest(url) );
}
// Update is called once per frame
void Update () {
}
public XmlNode SelectNodeFromXmlDocument( XmlDocument XmlDoc, string NodeName )
{
return XmlDoc.SelectSingleNode( NodeName );
}
private IEnumerator loadxmltest( string Path )
{
WWW getDate = new WWW( Path );
yield return getDate;
if( getDate.error != null )
{
Debug.Log( "WWWError: " + getDate.error );
}
else
{
XmlDocument xmlDoc = new XmlDocument();
string strXml = System.Text.Encoding.UTF8.GetString( getDate.bytes, 0, getDate.bytes.Length );
xmlDoc.LoadXml( strXml.Replace( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "" ) );
XmlNodeList nodeList = null;
try
{
nodeList = SelectNodeFromXmlDocument( xmlDoc, "xml" ).ChildNodes;
}
catch { }
foreach( XmlNode xn in nodeList )
{
if( xn.Name == "item" )
{
foreach( XmlNode item in xn.ChildNodes )
{
switch( item.Name )
{
case "id":
if( !string.IsNullOrEmpty( item.InnerText ) )
{
Debug.Log( "id" + item.InnerText );
}
break;
case "name":
if( !string.IsNullOrEmpty( item.InnerText ) )
{
Debug.Log( "name" + item.InnerText );
}
break;
case "name_id":
if( !string.IsNullOrEmpty( item.InnerText ) )
{
4000
Debug.Log( "name_id" + item.InnerText );
}
break;
case "time_state":
if( !string.IsNullOrEmpty( item.InnerText ) )
{
Debug.Log( "time_state" + item.InnerText );
}
break;
case "desp":
if( !string.IsNullOrEmpty( item.InnerText ) )
{
Debug.Log( "desp" + item.InnerText );
}
break;
}
}
}
}
}
}
}
using UnityEngine;
using System.IO;
using System.Xml;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System;
public class loadxml : MonoBehaviour {
#if UNITY_PC
public string url;
#elif UNITY_ANDROID
private string url =(Application.streamingAssetsPath + "/android/xml/fbxt.xml" ).Trim(); //3w读取数据路径
#endif
// Use this for initialization
void Start () {
Debug.Log( url );
StartCoroutine( loadxmltest(url) );
}
// Update is called once per frame
void Update () {
}
public XmlNode SelectNodeFromXmlDocument( XmlDocument XmlDoc, string NodeName )
{
return XmlDoc.SelectSingleNode( NodeName );
}
private IEnumerator loadxmltest( string Path )
{
WWW getDate = new WWW( Path );
yield return getDate;
if( getDate.error != null )
{
Debug.Log( "WWWError: " + getDate.error );
}
else
{
XmlDocument xmlDoc = new XmlDocument();
string strXml = System.Text.Encoding.UTF8.GetString( getDate.bytes, 0, getDate.bytes.Length );
xmlDoc.LoadXml( strXml.Replace( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "" ) );
XmlNodeList nodeList = null;
try
{
nodeList = SelectNodeFromXmlDocument( xmlDoc, "xml" ).ChildNodes;
}
catch { }
foreach( XmlNode xn in nodeList )
{
if( xn.Name == "item" )
{
foreach( XmlNode item in xn.ChildNodes )
{
switch( item.Name )
{
case "id":
if( !string.IsNullOrEmpty( item.InnerText ) )
{
Debug.Log( "id" + item.InnerText );
}
break;
case "name":
if( !string.IsNullOrEmpty( item.InnerText ) )
{
Debug.Log( "name" + item.InnerText );
}
break;
case "name_id":
if( !string.IsNullOrEmpty( item.InnerText ) )
{
4000
Debug.Log( "name_id" + item.InnerText );
}
break;
case "time_state":
if( !string.IsNullOrEmpty( item.InnerText ) )
{
Debug.Log( "time_state" + item.InnerText );
}
break;
case "desp":
if( !string.IsNullOrEmpty( item.InnerText ) )
{
Debug.Log( "desp" + item.InnerText );
}
break;
}
}
}
}
}
}
}
相关文章推荐
- Unity 场景保存,上传服务器,在下载更新场景 IO读取图片和WWW读取图片比较
- 关于Unity读取XML的简单学习
- Unity 读取xml 文件
- 【Unity&XML】读取XML文件内容
- Unity 读取xml 文件
- Unity Android及PC 外部Xml读取
- 安卓文件存在但是unity中通过www读取失败
- unity 中使用 WWW 类读取 StreamingAssets 目录的路径问题
- 关于Unity的几种XML读取方式
- Unity资源处理机制(Assets/WWW/AssetBundle/...)读取和加载资源方式详解
- unity PC和android不同平台读取xml文件比较
- unity中xml读取
- unity www读取本地视频文件和外部视频文件 播放视频动画和视频声音
- unity读取xml
- unity 读取xml 信息
- unity的XML读取
- unity Android 打包后读取 xml 文件
- Unity读取XML
- Unity中手机端WWW读取图片并显示
- Unity快捷读取XML、JSON文件