xml文件转二进制文件的AIR

简介: 游戏开发中,有很多XML可能是很大的,比如一个任务配置文件,可能就接近2M(当然全部文件放在一个XML里面本身就有问题,比较好的做法就是分等级分隔XML),这样就需要对XML进行压缩。之前把文件压成ZIP包,然后读取ZIP,但现在读取ZIP文件里面的内容,是很卡的,后面改成读取二进制的XML数据,这样感觉不会卡,速度也很快。

游戏开发中,有很多XML可能是很大的,比如一个任务配置文件,可能就接近2M(当然全部文件放在一个XML里面本身就有问题,比较好的做法就是分等级分隔XML),这样就需要对XML进行压缩。之前把文件压成ZIP包,然后读取ZIP,但现在读取ZIP文件里面的内容,是很卡的,后面改成读取二进制的XML数据,这样感觉不会卡,速度也很快。

 

实现思路也比较简单,使用二进制读取文件,然后调用保存。

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
width="900" height="350"
creationComplete="init()"
alwaysInFront="false">

<mx:Script>
<![CDATA[
import mx.controls.Alert;

private var filePath:String;
private var fileBytes:ByteArray;

private function init():void
{
var funArr:Array = [];

this.showStatusBar = true;
this.status = "Copyright©2012 meteoric_cry.net. Powered by Meteoric_cry";

bindBtnsEvent();
}

private function showTopHandler():void
{
if (this.stage.nativeWindow.alwaysInFront)
{
this.stage.nativeWindow.alwaysInFront=false;
showTopBtn.label="前端显示";
}
else
{
this.stage.nativeWindow.alwaysInFront=true;
showTopBtn.label="√前端显示";
}
}

private function bindBtnsEvent():void
{
selectBtn.addEventListener(MouseEvent.CLICK, onSelectHandler);
compressBtn.addEventListener(MouseEvent.CLICK, onCompressHandler);
saveBtn.addEventListener(MouseEvent.CLICK, onSaveHandler);
}

private function getTypeFilter():FileFilter
{
var str:String = "*.xml;";

var filter:FileFilter = new FileFilter("XML("+str+")", str);

return filter;
}

private function onSelectHandler(evt:MouseEvent):void
{
var file:File = new File();
file.addEventListener(Event.SELECT, selectFileCallback);
file.browseForOpen("请选择一个文件", [getTypeFilter()]);
}

private function selectFileCallback(evt:Event):void
{
clear();

var file:File = File(evt.target);
file.removeEventListener(Event.SELECT, selectFileCallback);

filePath = file.nativePath;

xmlPathIpt.htmlText = filePath;
}

private function clear():void
{
xmlPathIpt.htmlText = "";

filePath = null;
fileBytes = null;
}

private function onCompressHandler(evt:MouseEvent):void
{
if (filePath)
{
var file:File = new File(filePath);

if (file.isDirectory == false && file.exists)
{
var fs:FileStream = new FileStream();
fileBytes = new ByteArray();

fs.open(file, FileMode.READ);
fs.position = 0;
fs.readBytes(fileBytes, 0, fs.bytesAvailable);
fs.close();

fileBytes.compress();

Alert.show('文件压缩成功', '温馨提示', Alert.OK);
}
else
{
Alert.show(filePath + "不是一个正确的文件路径", '温馨提示', Alert.OK);
}
}
else
{
Alert.show('请选择要压缩的文件', '温馨提示', Alert.OK);
}
}

private function onSaveHandler(evt:MouseEvent):void
{
if (fileBytes)
{
var fileName:String = new File(filePath).name.replace(/\.xml/, '');

new FileReference().save(fileBytes, fileName + ".swf");

Alert.show("文件" + fileName + ".swf保存成功", '温馨提示', Alert.OK);
}
else
{
Alert.show('请先选择文件并进行压缩', '温馨提示', Alert.OK);
}
}

]]>
</mx:Script>

<mx:Button label="前端显示" right="10" top="3" click="showTopHandler();" id="showTopBtn"/>

<mx:HBox width="100%" horizontalScrollPolicy="off" verticalScrollPolicy="off"
paddingTop="100" paddingLeft="50" paddingRight="10" paddingBottom="10"
horizontalGap="0" verticalAlign="middle">

<mx:Label text="请选择要进行压缩的文件:" />
<mx:TextInput id="xmlPathIpt" width="400" editable="false" paddingLeft="2" paddingRight="2" />

<mx:HBox width="100%" paddingLeft="20">
<mx:Button id="selectBtn" label="选择文件" />
<mx:Button id="compressBtn" label="压缩文件" />
<mx:Button id="saveBtn" label="保存文件" />
</mx:HBox>

</mx:HBox>

</mx:WindowedApplication>

 

点击此处下载AIR>>

目录
相关文章
|
5月前
|
Android开发 开发者
Android自定义View之不得不知道的文件attrs.xml(自定义属性)
本文详细介绍了如何通过自定义 `attrs.xml` 文件实现 Android 自定义 View 的属性配置。以一个包含 TextView 和 ImageView 的 DemoView 为例,讲解了如何使用自定义属性动态改变文字内容和控制图片显示隐藏。同时,通过设置布尔值和点击事件,实现了图片状态的切换功能。代码中展示了如何在构造函数中解析自定义属性,并通过方法 `setSetting0n` 和 `setbackeguang` 实现功能逻辑的优化与封装。此示例帮助开发者更好地理解自定义 View 的开发流程与 attrs.xml 的实际应用。
107 2
Android自定义View之不得不知道的文件attrs.xml(自定义属性)
|
XML 前端开发 Java
讲解SSM的xml文件
本文详细介绍了SSM框架中的xml配置文件,包括springMVC.xml和applicationContext.xml,涉及组件扫描、数据源配置、事务管理、MyBatis集成以及Spring MVC的视图解析器配置。
236 1
|
XML Java 数据格式
Spring5入门到实战------7、IOC容器-Bean管理XML方式(外部属性文件)
这篇文章是Spring5框架的实战教程,主要介绍了如何在Spring的IOC容器中通过XML配置方式使用外部属性文件来管理Bean,特别是数据库连接池的配置。文章详细讲解了创建属性文件、引入属性文件到Spring配置、以及如何使用属性占位符来引用属性文件中的值。
Spring5入门到实战------7、IOC容器-Bean管理XML方式(外部属性文件)
|
XML Java 数据格式
java创建xml文件内容
java创建xml文件内容
119 0
|
XML Java 数据格式
java解析xml文件内容
java解析xml文件内容
139 0
|
11月前
|
Java Maven
maven项目的pom.xml文件常用标签使用介绍
第四届人文,智慧教育与服务管理国际学术会议(HWESM 2025) 2025 4th International Conference on Humanities, Wisdom Education and Service Management
850 8
|
11月前
|
XML Android开发 数据格式
Eclipse 创建 XML 文件
Eclipse 创建 XML 文件
156 2
|
XML JavaScript Java
java与XML文件的读写
java与XML文件的读写
152 3
|
SQL XML Java
mybatis :sqlmapconfig.xml配置 ++++Mapper XML 文件(sql/insert/delete/update/select)(增删改查)用法
当然,这些仅是MyBatis功能的初步介绍。MyBatis还提供了高级特性,如动态SQL、类型处理器、插件等,可以进一步提供对数据库交互的强大支持和灵活性。希望上述内容对您理解MyBatis的基本操作有所帮助。在实际使用中,您可能还需要根据具体的业务要求调整和优化SQL语句和配置。
206 1
|
XML 存储 缓存
C#使用XML文件的详解及示例
C#使用XML文件的详解及示例
475 0

相关课程

更多