xml里面数据的读取
来源:互联网 发布:电子线路画图软件 编辑:程序博客网 时间:2024/05/22 08:02
test.xml内容如下
<?xml version="1.0" encoding="utf-8" ?>
<config>
<Item1 name="custom"> //Item1指的是节点,可以通过判断 node.Name是否是等于“Item1”来判断取它的子元素的值
<ID id="0">美丽环境0</ID> //XmlElement el = (XmlElement)node3;是获得元素,获得xml中ID的属性值是通过 el. GetAttribute("id") 获得它的value通过el .InnerText
<ID id="1">美丽环境1</ID>
<ID id="2">美丽环境2</ID>
<ID id="3">美丽环境3</ID>
<ID id="4">美丽环境4</ID>
<ID id="5">美丽环境5</ID>
<ID id="6">美丽环境6</ID>
<ID id="7">美丽环境7</ID>
<ID id="8">美丽环境8</ID>
<ID id="9">美丽环境9</ID>
<ID id="10">美丽环境10</ID>
<ID id="11">美丽环境11</ID>
</Item1>
<Item2 name="material1">
<ID id="20">可爱动物20</ID>
<ID id="21">可爱动物21</ID>
<ID id="22">可爱动物22</ID>
<ID id="23">可爱动物23</ID>
<ID id="24">可爱动物24</ID>
<ID id="25">可爱动物25</ID>
<ID id="26">可爱动物26</ID>
<ID id="27">可爱动物27</ID>
<ID id="28">可爱动物28</ID>
<ID id="29">可爱动物29</ID>
</Item2>
<Item3 name="material2">
<ID id="30">精彩视频30</ID>
<ID id="31">精彩视频31</ID>
<ID id="32">精彩视频32</ID>
</Item3>
</config>
用脚本读取
using UnityEngine;
using System.Collections;
using System.Xml;
using System.Collections.Generic;
public class MyTest : MonoBehaviour {
public GameObject rotateBtn1;
public GameObject imageBtn;
public GameObject scrollView;
public GameObject scrollView1;
public GameObject scrollView2;
public GameObject btn1;
public GameObject btn2;
public GameObject btn3;
private string id;
private string name;
public struct Test //定义一个结构体
{
public string id;
}
List<Test> testlist = new List<Test>();
List<Test> testlist1 = new List<Test>();
List<Test> testlist2 = new List<Test>();
void Start()
{
UIEventListener.Get(rotateBtn1).onClick = RotateWin1;
UIEventListener.Get(btn1).onClick = OnClickWin1Btn1;
UIEventListener.Get(btn2).onClick = OnClickWin1Btn2;
UIEventListener.Get(btn3).onClick = OnClickWin1Btn3;
LoadXml();
}
void LoadXml()
{
XmlDocument testdoc = new XmlDocument();
testdoc.Load(Application.dataPath + "/test.xml");
XmlNode node = testdoc.SelectSingleNode("config");
foreach (XmlNode node2 in node)
{
if (node2.Name == "Item1")
{
foreach (XmlNode node3 in node2)
{
Test test;
XmlElement el = (XmlElement)node3;
test.id = el.GetAttribute("id");
testlist.Add(test);
}
}
if(node2.Name=="Item2")
{
foreach (XmlNode node4 in node2)
{
Test test;
XmlElement el = (XmlElement)node4;
test.id = el.GetAttribute("id");
testlist1.Add(test);
}
}
if(node2.Name=="Item3")
{
foreach (XmlNode node5 in node2)
{
Test test;
XmlElement el = (XmlElement)node5;
test.id = el.GetAttribute("id");
testlist2.Add(test);
}
}
}
ShowImageList(scrollView,testlist);
ShowImageList(scrollView1, testlist1);
ShowImageList(scrollView2, testlist2);
scrollView1.SetActive(false);
scrollView2.SetActive(false);
}
void ShowImageList(GameObject scrollView,List<Test> list)
{
int column = 3;
int row;
if (list.Count % 3 == 0)
{
row = list.Count / 3;
}
else
{
row = list.Count / 3 + 1;
}
Texture2D[] textures = new Texture2D[list.Count];
for (int i = 0; i < list.Count; i++)
{
id = list[i].id;
textures[i] = Resources.Load(id) as Texture2D;
}
for (int i = 0; i < row; i++)
{
for (int j = 0; j < column; j++)
{
GameObject go = NGUITools.AddChild(scrollView, imageBtn);
go.transform.localPosition = new Vector3(-93 + 82 * j + 3, 61 - i * 54 + 3, 0);
go.AddComponent<BoxCollider>();
go.AddComponent<UIDragScrollView>();
go.AddComponent<UIEventListener>();
go.AddComponent<UIButtonMessage>();
go.transform.GetChild(0).GetComponent<UITexture>().mainTexture = textures[j + i * 3];
if((j + i * 3 == list.Count-1)) return;
}
}
}
void RotateWin1(GameObject btn)
{
rotateBtn1.transform.parent.eulerAngles += new Vector3(0, 0, 180);
}
void OnClickWin1Btn1(GameObject btn)
{
//Debug.Log("window1btn");
scrollView.SetActive(true);
scrollView1.SetActive(false);
scrollView2.SetActive(false);
}
void OnClickWin1Btn2(GameObject btn)
{
scrollView1.SetActive(true);
scrollView2.SetActive(false);
scrollView.SetActive(false);
}
void OnClickWin1Btn3(GameObject btn)
{
scrollView2.SetActive(true);
scrollView.SetActive(false);
scrollView1.SetActive(false);
}
}
- xml里面数据的读取
- C#读取URL里面的XML数据问题
- 读取XML的数据
- 读取xml文件里面的内容
- Unity3D 读取XML文件里面的属性
- 动态读取table里面的数据
- js怎样读取json里面的数据 :
- js怎样读取json里面的数据
- 通过ajax来读取xml里面的数据,以及查询好数据如何写入xml里去 来供ajax读取
- 【Xml配置文件的数据读取】
- Java 读取ClassPathXmlApplicationContext xml 文件里面的bean
- java里面怎么读取web.xml里面的context-param值
- 从plist里面读取数据
- ASP逐条读取ACCESS数据库里面的数据
- 使用JAVA读取EXCEL文件里面的数据
- 使用JAVA读取EXCEL文件里面的数据
- 网站打开首页时如何读取数据库里面的数据
- [javascipt] js循环读取table里面的数据
- Base64编码与解码
- 数据结构实践——循环双链表应用
- 对象数组排序
- Unix编程之文件类型检测
- 推荐!手把手教你使用Git
- xml里面数据的读取
- Template模式-在于模板框架以及和Strategy模式区别
- 数据科学的三个重要经验
- thinkPHP3.2.3使用过程中遇到的问题收集
- 欢迎使用CSDN-markdown编辑器
- android:windowSoftInputMode属性使用
- Python 爬虫 raise self._value IndexError: list index out of range
- 第二周
- android多线程下载模块