js解析xml数据


js解析xml数据

xml

1
2
3
<tileset firstgid="1" name="taikonglouti" tilewidth="36" tileheight="36" tilecount="39">
<image source="taikonglouti.png" width="108" height="468"/>
</tileset>

js 解析

1
2
3
4
5
6
7
8
9
10
11
12
13
14
let url = Common.s_remote_resource_url + "resources/tiledMap/bg/map1.tmx";
cc.loader.load(url, (err, assets) => {
if(err != null){
cc.error(url, err);
return;
}
let parser=new DOMParser();
let xmlDoc=parser.parseFromString(assets,"text/xml");
let image = xmlDoc.getElementsByTagName("image"); // 获取image标签对象数组
url = Common.s_remote_resource_url + "resources/tiledMap/bg/" + image[0].getAttribute("source");// 获取image对象的source属性
cc.loader.load(url, (err, texture) => {
this.m_bgSprite.spriteFrame = new cc.SpriteFrame(texture);
});
});