Python xml.dom.minidom

下面是cocosStudio UI编辑器做的一个示意图,意图就是拼一个地图路径,然后通过导出路径信息,在代码中动态加载场景关卡信息。


这是导出的.csd文件内容,其实就是一个.xml文件

<GameFile>
  <PropertyGroup Name="Test" Type="Layer" ID="eea0b9fc-7278-4fba-9acc-90e9fa87d8b7" Version="3.10.0.0" />
  <Content ctype="GameProjectContent">
    <Content>
      <Animation Duration="0" Speed="1.0000" />
      <ObjectData Name="Layer" Tag="435" ctype="GameLayerObjectData">
        <Size X="1280.0000" Y="720.0000" />
        <Children>
          <AbstractNodeData Name="lv_1001" ActionTag="-1286897279" Tag="436" IconVisible="False" LeftMargin="118.7037" RightMargin="1081.2963" TopMargin="282.0772" BottomMargin="368.9228" LeftEage="26" RightEage="26" TopEage="22" BottomEage="22" Scale9OriginX="26" Scale9OriginY="22" Scale9Width="28" Scale9Height="25" ctype="ImageViewObjectData">
            <Size X="80.0000" Y="69.0000" />
            <AnchorPoint ScaleX="0.5000" ScaleY="0.5000" />
            <Position X="158.7037" Y="403.4228" />
            <Scale ScaleX="1.0000" ScaleY="1.0000" />
            <CColor A="255" R="255" G="255" B="255" />
            <PrePosition X="0.1240" Y="0.5603" />
            <PreSize X="0.0625" Y="0.0958" />
            <FileData Type="MarkedSubImage" Path="Textures/Map/pic_platform_gray.png" Plist="Textures/map_image.plist" />
          </AbstractNodeData>
          <AbstractNodeData Name="lv_1002" ActionTag="-1839859294" Tag="437" IconVisible="False" LeftMargin="321.1236" RightMargin="878.8763" TopMargin="460.4391" BottomMargin="190.5609" LeftEage="26" RightEage="26" TopEage="22" BottomEage="22" Scale9OriginX="26" Scale9OriginY="22" Scale9Width="28" Scale9Height="25" ctype="ImageViewObjectData">
            <Size X="80.0000" Y="69.0000" />
            <AnchorPoint ScaleX="0.5000" ScaleY="0.5000" />
            <Position X="361.1236" Y="225.0609" />
            <Scale ScaleX="1.0000" ScaleY="1.0000" />
            <CColor A="255" R="255" G="255" B="255" />
            <PrePosition X="0.2821" Y="0.3126" />
            <PreSize X="0.0625" Y="0.0958" />
            <FileData Type="MarkedSubImage" Path="Textures/Map/pic_platform_gray.png" Plist="Textures/map_image.plist" />
          </AbstractNodeData>
          <AbstractNodeData Name="lv_1003" ActionTag="-1906350308" Tag="438" IconVisible="False" LeftMargin="543.2164" RightMargin="656.7836" TopMargin="258.3470" BottomMargin="392.6530" LeftEage="26" RightEage="26" TopEage="22" BottomEage="22" Scale9OriginX="26" Scale9OriginY="22" Scale9Width="28" Scale9Height="25" ctype="ImageViewObjectData">
            <Size X="80.0000" Y="69.0000" />
            <AnchorPoint ScaleX="0.5000" ScaleY="0.5000" />
            <Position X="583.2164" Y="427.1530" />
            <Scale ScaleX="1.0000" ScaleY="1.0000" />
            <CColor A="255" R="255" G="255" B="255" />
            <PrePosition X="0.4556" Y="0.5933" />
            <PreSize X="0.0625" Y="0.0958" />
            <FileData Type="MarkedSubImage" Path="Textures/Map/pic_platform_gray.png" Plist="Textures/map_image.plist" />
          </AbstractNodeData>
          <AbstractNodeData Name="lv_1004" ActionTag="-1959952692" Tag="439" IconVisible="False" LeftMargin="791.5454" RightMargin="408.4546" TopMargin="443.2685" BottomMargin="207.7315" LeftEage="26" RightEage="26" TopEage="22" BottomEage="22" Scale9OriginX="26" Scale9OriginY="22" Scale9Width="28" Scale9Height="25" ctype="ImageViewObjectData">
            <Size X="80.0000" Y="69.0000" />
            <AnchorPoint ScaleX="0.5000" ScaleY="0.5000" />
            <Position X="831.5454" Y="242.2315" />
            <Scale ScaleX="1.0000" ScaleY="1.0000" />
            <CColor A="255" R="255" G="255" B="255" />
            <PrePosition X="0.6496" Y="0.3364" />
            <PreSize X="0.0625" Y="0.0958" />
            <FileData Type="MarkedSubImage" Path="Textures/Map/pic_platform_gray.png" Plist="Textures/map_image.plist" />
          </AbstractNodeData>
          <AbstractNodeData Name="lv_1005" ActionTag="-589426814" Tag="440" IconVisible="False" LeftMargin="1042.0551" RightMargin="157.9449" TopMargin="269.6014" BottomMargin="381.3986" LeftEage="26" RightEage="26" TopEage="22" BottomEage="22" Scale9OriginX="26" Scale9OriginY="22" Scale9Width="28" Scale9Height="25" ctype="ImageViewObjectData">
            <Size X="80.0000" Y="69.0000" />
            <AnchorPoint ScaleX="0.5000" ScaleY="0.5000" />
            <Position X="1082.0551" Y="415.8986" />
            <Scale ScaleX="1.0000" ScaleY="1.0000" />
            <CColor A="255" R="255" G="255" B="255" />
            <PrePosition X="0.8454" Y="0.5776" />
            <PreSize X="0.0625" Y="0.0958" />
            <FileData Type="MarkedSubImage" Path="Textures/Map/pic_platform_gray.png" Plist="Textures/map_image.plist" />
          </AbstractNodeData>
        </Children>
      </ObjectData>
    </Content>
  </Content>
</GameFile>

其实大多数信息是冗余的,对于我们来说,我们只要关卡的名字(即ID, 这里我的命名为 lv_ + ID)和坐标(x , y),我们需要导出这张地图上的信息以便我们在程序中使用

上面的内容其实是想让大家有个直观的印象,下面废话不多说,上干货:

#!/usr/bin/env python
#coding=utf-8

import xml.dom.minidom
import codecs
import json
import os

def coordinateExporter(filePath, jsonPath):
    #data = {}
    data = []
    for path in filePath:
        levels = []
        dom = xml.dom.minidom.parse(path)
        root = dom.documentElement
        nodes = root.getElementsByTagName('AbstractNodeData')
        for node in nodes:
            name = node.getAttribute('Name')
            position = node.getElementsByTagName('Position')[0]
            x = position.getAttribute('X')
            y = position.getAttribute('Y')
            if name.find('_') != -1:
                _, nodeId = name.split('_')
                if name.startswith('lv_'):
                    levels.append({'id':nodeId, 'x':float(x), 'y':float(y)})
        content = {'levels':levels}
        fileName, _ = os.path.splitext(os.path.basename(path))
        print fileName
        data.append(content)
        #data[fileName] = content

    fp = codecs.open(jsonPath, 'w', 'utf-8')
    json.dump(data, fp, ensure_ascii=False, indent=2, separators=(',', ': '), sort_keys=True)
    fp.close()


if __name__ == '__main__':
    coordinateExporter(['Test.csd',], 'coordinates.json')
    #print json.dumps({'4':5, '6':7}, sort_keys = True, indent=2, separators=(',', ':'))

这样做的结果是:在工程路径下生成了一个名为coordinates.json的文件,其内容是:

[
  {
    "levels": [
      {
        "id": "1001",
        "x": 158.7037,
        "y": 403.4228
      },
      {
        "id": "1002",
        "x": 361.1236,
        "y": 225.0609
      },
      {
        "id": "1003",
        "x": 583.2164,
        "y": 427.153
      },
      {
        "id": "1004",
        "x": 831.5454,
        "y": 242.2315
      },
      {
        "id": "1005",
        "x": 1082.0551,
        "y": 415.8986
      }
    ]
  }
]

通过对 .json 文件进行解析,就可以为我们程序所用了!


©️2020 CSDN 皮肤主题: 创作都市 设计师:CSDN官方博客 返回首页