unity3d使用Mono.Xml读取xml

unity3d读取xml有好几种方式,最简单是直接利用System.Xml读取xml,但是项目打包会比较大,增加了1M的资源占用。另外两个是利用其他轻量级xml库来实现,如Mono.Xml、XMLParser。Mono.Xml是c#写的,XMLParser是js写的。文章主要说明Mono.Xml的用法。

为什么不建议使用System.Xml,unity的解释如下:

When building a player (Desktop, Android or iOS) it is important to not depend on System.dll or System.Xml.dll. Unity does not include System.dll or System.Xml.dll in the players installation. That means, if you want to use Xml or some Generic containers which live in System.dll then the required dlls will be included in the players. This usually adds 1mb to the download size, obviously this is not very good for the distribution of your players and you should really avoid it. If you need to parse some Xml files, you can use a smaller xml library like this one Mono.Xml.zip. While most Generic containers are contained in mscorlib, Stack<> and few others are in System.dll. So you really want to avoid those.

首先,定义一个xml文件,如下:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ROOT>
	<table wave="1" level="1" name="John"/>
	<table wave="2" level="1" name="Lucy"/>
</ROOT>

把Mono.Xml加进unity3d项目。下载地址:http://download.csdn.net/detail/cwqcwk1/7105071

unity3d利用Mono.xml读取xml的代码如下:

using UnityEngine;
using System.Collections;

using Mono.Xml;
using System.IO;

using System.Security;

public class XmlLorder {

	public void Read()
	{
		SecurityParser SP = new SecurityParser();

		// 假设xml文件路径为 Resources/test.xml
		string xmlPath = "test";

		SP.LoadXml(Resources.Load( xmlPath ).ToString());
		
		SecurityElement SE = SP.ToXml();

		foreach (SecurityElement child in SE.Children)
		{
			//比对下是否使自己所需要得节点
			if(child.Tag == "table")
			{
				//获得节点得属性
				string wave = child.Attribute("wave");
				string level = child.Attribute("level");
				string name = child.Attribute("name");
				Debug.Log("wave:" + wave + " level:" + level + " name:" + name);
			}
			
		}

		
	}

}

顺带提一下unity3d使用XMLParser读取xml:

XMLParser xmlparse = new XMLParser();
XMLNode node = xmlparse.Parse(xmldata.text);

XMLNodeList list = node.GetNodeList("ROOT>0>table");
for (int i = 0; i < list.Count; i++)
{
  
	string wave = node.GetValue("ROOT>0>table>" + i + ">@wave");
	string level = node.GetValue("ROOT>0>table>" + i + ">@level");
	string wait = node.GetValue("ROOT>0>table>" + i + ">@wait");

}

XMLParser下载地址:http://download.csdn.net/detail/cwqcwk1/7104625

参考:

http://blog.csdn.net/mycwq/article/details/19813779

http://docs.unity3d.com/Documentation/Manual/ReducingFilesize.html

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
好的,我明白你的问题。以下是一个基于Spring Boot的异步程序,用于上传Word文档、匹配关键词并统计其出现次数。 首先,我们需要在pom.xml文件中添加以下依赖: ```xml <!-- Spring Boot 异步支持 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> <!-- Apache POI 用于读取 Word 文档 --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.0</version> </dependency> ``` 接下来,创建一个用于上传文件的控制器: ```java @RestController public class FileController { // 上传文件(异步方式) @PostMapping("/upload") public Mono<Map<String, Integer>> uploadFile(@RequestParam("file") MultipartFile file, @RequestParam("keyword") String keyword) { // 创建一个 Mono,表示异步操作结果 return Mono.create(monoSink -> { try { // 读取 Word 文档 XWPFDocument doc = new XWPFDocument(file.getInputStream()); // 统计关键词出现次数 int count = 0; for (XWPFParagraph para : doc.getParagraphs()) { String text = para.getText(); if (text.contains(keyword)) { count++; } } // 生成统计结果 Map<String, Integer> result = new HashMap<>(); result.put("count", count); // 设置异步操作结果并通知 Mono monoSink.success(result); } catch (IOException e) { // 如果发生异常,设置错误信息并通知 Mono monoSink.error(e); } }); } } ``` 在这个控制器中,我们使用了`@PostMapping`注解将方法映射为一个POST请求处理方法,并且使用了`@RequestParam`注解获取上传的文件和关键词参数。在方法体中,我们首先创建了一个`Mono`对象表示异步操作结果,然后使用`XWPFDocument`类读取Word文档,并通过遍历文档中的段落统计关键词出现次数。最后,将统计结果转换为一个Map对象,并将它设为异步操作结果,通知Mono对象。 最后,我们需要在Spring Boot的配置类中启用异步支持: ```java @EnableAsync @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 现在,我们可以通过发送POST请求来测试这个控制器: ```shell $ curl -F 'file=@/path/to/sample.docx' -F 'keyword=Spring' http://localhost:8080/upload {"count":2} ``` 上面的请求将sample.docx文件上传到控制器,并指定'keyword'参数匹配‘Spring’关键词。控制器返回一个JSON对象,其中包含了关键词在文档中出现的次数。
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值