Python xml转换json配置

自定义格式的xml配置表转换成json格式

  • 因为学习需要,找到一个游戏采用的全是xml配置表。但是现在自己需要用json格式,所以学习转换了一下。故记录一下。

  • 要被转换的格式为:

<Mashbits>
  <setting x="5" y="5" star="12">setting>
  <object x="1" y="4" color="GRAY" isColored="true" isBlocked="true" isFreeze="false" isInverted="false" isCrashed="false" movement="NONE" id="-1">NONEobject>
  <object x="3" y="4" color="GRAY" isColored="true" isBlocked="true" isFreeze="false" isInverted="false" isCrashed="false" movement="NONE" id="-1">NONEobject>
  <object x="2" y="2" color="BLUE" isColored="true" isBlocked="false" isFreeze="false" isInverted="true" isCrashed="false" movement="NONE" id="-1">NONEobject>
  <object x="0" y="0" color="BLUE" isColored="false" isBlocked="false" isFreeze="false" isInverted="false" isCrashed="false" movement="NONE" id="-1">RBobject>
  <object x="4" y="0" color="PINK" isColored="false" isBlocked="false" isFreeze="false" isInverted="false" isCrashed="false" movement="NONE" id="-1">RPobject>
  <object x="4" y="4" color="BLUE" isColored="false" isBlocked="false" isFreeze="false" isInverted="false" isCrashed="false" movement="NONE" id="-1">MBobject>
  <object x="0" y="4" color="PINK" isColored="false" isBlocked="false" isFreeze="false" isInverted="false" isCrashed="false" movement="NONE" id="-1">MPobject>
  <object x="3" y="0" color="GRAY" isColored="true" isBlocked="false" isFreeze="false" isInverted="false" isCrashed="false" movement="VERTICAL" id="1">NONEobject>
  <object x="0" y="2" color="GRAY" isColored="false" isBlocked="false" isFreeze="false" isInverted="false" isCrashed="false" movement="NONE" id="-1">OBobject>
  <object x="4" y="2" color="GRAY" isColored="false" isBlocked="false" isFreeze="false" isInverted="false" isCrashed="false" movement="NONE" id="-1">OBobject>
  <object x="1" y="0" color="GRAY" isColored="true" isBlocked="false" isFreeze="false" isInverted="false" isCrashed="false" movement="VERTICAL" id="1">NONEobject>
Mashbits>

2.需要转换成json格式为:

Python xml转换json配置_第1张图片

python 代码 我用的是python27 按目录下批量装换

# coding: utf-8
import json
import os
import sys
import time 
from xml.dom.minidom import parse
import xml
import xml.etree.ElementTree as ET
import re

 
#全局唯一标识
unique_id = 1
suffix = ".bin"
def Log(txt):
	pass
	#print(txt)


# def readXml():
# 	domTree = parse("./level1.xml")  
# 	root = ET.fromstring(domTree.toxml())
# 	for action in root.iter("object"): 
# 		for result in action.iter("object"):
# 			res_name = result.get("name")
# 			res_text = result.text              # 获取标签文本信息 
# 			print res_text
		
		 
 
def getXmlData(file_name):
	level = 1 #节点的深度从1开始
	result_list = []
	root = ET.parse(file_name).getroot() 
	walkData(root, level, result_list)
	return result_list	

#遍历所有的节点
def walkData(root_node, level, result_list):
	global unique_id  
	temp_list =[unique_id, level, root_node.tag, root_node.attrib,root_node.text] 
	result_list.append(temp_list)
	unique_id += 1
	 
	#遍历每个子节点
	children_node = root_node.getchildren()
	if len(children_node) == 0:
		return
	for child in children_node:
		walkData(child, level + 1, result_list)
	return




#读取文件
def findFile(path,outPath):
	global suffix
	for name in os.listdir( path ): 
		if os.path.isdir( os.path.join(path, name) ): 
			findFile(os.path.join(path, name),outPath )
		else:
			portion = os.path.splitext(name)
			#读取指定后缀文件
			if portion[1] == suffix: 
				fileName = re.findall("Level_[0-9]+",portion[0]) 
				readXml(os.path.join(path, name),os.path.join(outPath,fileName[0]+".json"))


def readXml(path,outName):
	R = getXmlData(path) 
	jsons = {
     }
	objs =[]
	for x in R: 
		level = x[1]
		if level  == 2:
			if x[2] == "object":
				objs.append( x[3])
				x[3].update({
     "text":x[4]})
			else:
				jsons.update({
     x[2] : x[3]}) 

	jsons.update({
     "object":objs}) 
	f = open( outName, 'w+' )
	f.write(json.dumps(jsons))
	f.close() 
				 


if __name__ == "__main__":  
	


	#输处目录
	outPath = os.path.join( os.getcwd(), "json" )
	if not os.path.isdir( outPath ):
		os.mkdir( outPath )

	#读取目录
	path =  os.path.join(os.getcwd(),"xml")
	findFile(path,outPath)
	

具体工程上传csdn了。不需要积分

你可能感兴趣的:(Python)