Python解析xml配置文件,生成cs代碼

最近項目用的配置表是xml文件,每次要手動添加兩個cs代碼文件,很浪費時間,所以寫了一個代碼生成工具,以下是代碼

原始xml配置文件

OutfitAffix.xml:

<root version="2366">
	<item _id="1" type="1" name="青銅" outfitType="[102,501]" affixAttr="{'201':6000}" needAttrAdd="{'101':2}" />
	<item _id="2" type="1" name="鐵" outfitType="[501]" affixAttr="{'201':4000,'403':4000}" needAttrAdd="{'101':2}" />
	<item _id="3" type="1" name="鋼" outfitType="[501,602]" affixAttr="{'206':6000}" needAttrAdd="{'101':2}" />
	<item _id="4" type="1" name="銀" outfitType="[102]" affixAttr="{'206':4000,'403':4000}" needAttrAdd="{'101':2}" />
	<item _id="5" type="1" name="金" outfitType="[201,301,401]" affixAttr="{'15':6000}" needAttrAdd="{'101':2}" />
	<item _id="6" type="1" name="白金" outfitType="[201]" affixAttr="{'15':4000,'17':4000}" needAttrAdd="{'101':2}" />
	<item _id="7" type="1" name="隕鐵" outfitType="[201]" affixAttr="{'15':4000,'18':4000}" needAttrAdd="{'101':2}" />
	<item _id="8" type="1" name="怪異" outfitType="[201]" affixAttr="{'15':4000,'19':4000}" needAttrAdd="{'101':2}" />
	<item _id="9" type="1" name="怪異" outfitType="[201]" affixAttr="{'15':4000,'20':4000}" needAttrAdd="{'101':2}" />
	<item _id="10" type="1" name="目光銳利" outfitType="[301]" affixAttr="{'15':4000,'0':4000}" needAttrAdd="{'101':2}" />
	<item _id="11" type="1" name="雪" outfitType="[301,401]" affixAttr="{'15':4000,'32':4000}" needAttrAdd="{'101':2}" />
	<item _id="12" type="1" name="顫抖" outfitType="[301,401]" affixAttr="{'306':6000}" needAttrAdd="{'101':2}" />
	<item _id="13" type="1" name="北風" outfitType="[301,401]" affixAttr="{'306':4000,'0':4000}" needAttrAdd="{'101':2}" />
	<item _id="14" type="1" name="寒冬" outfitType="[601]" affixAttr="{'306':4000,'307':4000}" needAttrAdd="{'101':2}" />
	<item _id="15" type="1" name="烈火" outfitType="[601]" affixAttr="{'306':4000,'308':4000}" needAttrAdd="{'101':2}" />
	<item _id="16" type="1" name="悶燒" outfitType="[601]" affixAttr="{'306':4000,'309':4000}" needAttrAdd="{'101':2}" />
	<item _id="17" type="1" name="煙霧" outfitType="[601]" affixAttr="{'306':4000,'310':4000}" needAttrAdd="{'101':2}" />
	<item _id="18" type="1" name="燃燒" outfitType="[102]" affixAttr="{'207':6000}" needAttrAdd="{'101':2}" />
	<item _id="19" type="1" name="濃縮" outfitType="[102]" affixAttr="{'208':6000}" needAttrAdd="{'101':2}" />
	<item _id="20" type="1" name="靜態" outfitType="[102]" affixAttr="{'209':6000}" needAttrAdd="{'101':2}" />
	<item _id="21" type="1" name="發光" outfitType="[102]" affixAttr="{'210':6000}" needAttrAdd="{'101':2}" />
	<item _id="22" type="1" name="嗡叫" outfitType="[102]" affixAttr="{'211':4000,'403':4000}" needAttrAdd="{'101':2}" />
	<item _id="23" type="1" name="彎曲" outfitType="[102]" affixAttr="{'211':4000,'206':4000}" needAttrAdd="{'101':2}" />
	<item _id="24" type="1" name="震撼" outfitType="[201,602]" affixAttr="{'212':6000}" needAttrAdd="{'101':2}" />
	<item _id="25" type="1" name="腐敗" outfitType="[201,602]" affixAttr="{'212':4000,'21':4000}" needAttrAdd="{'101':2}" />
	<item _id="26" type="1" name="腐爛" outfitType="[201,602]" affixAttr="{'212':4000,'22':4000}" needAttrAdd="{'101':2}" />
	<item _id="27" type="1" name="腐蝕" outfitType="[201,602]" affixAttr="{'212':4000,'23':4000}" needAttrAdd="{'101':2}" />
	<item _id="28" type="1" name="劇毒" outfitType="[201,602]" affixAttr="{'212':4000,'24':4000}" needAttrAdd="{'101':2}" />
	<item _id="29" type="1" name="瘟疫" outfitType="[501]" affixAttr="{'403':6000}" needAttrAdd="{'101':2}" />
	<item _id="30" type="1" name="鋸齒" outfitType="[301,401]" affixAttr="{'0':6000}" needAttrAdd="{'101':2}" />
	<item _id="31" type="1" name="致命" outfitType="[301,401]" affixAttr="{'0':4000,'15':4000}" needAttrAdd="{'101':2}" />
	<item _id="32" type="1" name="惡毒" outfitType="[101]" affixAttr="{'201':6000}" needAttrAdd="{'102':2}" />
	<item _id="33" type="1" name="殘酷" outfitType="[501]" affixAttr="{'201':4000,'33':4000}" needAttrAdd="{'102':2}" />
	<item _id="34" type="1" name="穩重" outfitType="[602]" affixAttr="{'206':6000}" needAttrAdd="{'102':2}" />
	<item _id="35" type="1" name="野蠻" outfitType="[101]" affixAttr="{'206':4000,'33':4000}" needAttrAdd="{'102':2}" />
	<item _id="36" type="1" name="絕情" outfitType="[101,102,501]" affixAttr="{'206':4000,'207':4000}" needAttrAdd="{'102':2}" />
	<item _id="37" type="1" name="兇暴" outfitType="[101,102,501]" affixAttr="{'206':4000,'208':4000}" needAttrAdd="{'102':2}" />
	<item _id="38" type="1" name="殘忍" outfitType="[101,102,501]" affixAttr="{'206':4000,'209':4000}" needAttrAdd="{'102':2}" />
	<item _id="39" type="1" name="幻影" outfitType="[101,102,501]" affixAttr="{'206':4000,'210':4000}" needAttrAdd="{'102':2}" />
	<item _id="40" type="1" name="愚人" outfitType="[201,301,401]" affixAttr="{'15':6000}" needAttrAdd="{'102':2}" />
	<item _id="41" type="1" name="磨碎" outfitType="[201]" affixAttr="{'15':4000,'17':4000}" needAttrAdd="{'102':2}" />
	<item _id="42" type="1" name="尖銳" outfitType="[201]" affixAttr="{'15':4000,'18':4000}" needAttrAdd="{'102':2}" />
	<item _id="43" type="1" name="精良" outfitType="[201]" affixAttr="{'15':4000,'19':4000}" needAttrAdd="{'102':2}" />
	<item _id="44" type="1" name="戰士" outfitType="[201]" affixAttr="{'15':4000,'20':4000}" needAttrAdd="{'102':2}" />
	<item _id="45" type="1" name="士兵" outfitType="[301]" affixAttr="{'15':4000,'0':4000}" needAttrAdd="{'102':2}" />
	<item _id="46" type="1" name="騎士" outfitType="[601]" affixAttr="{'306':4000,'307':4000}" needAttrAdd="{'102':2}" />
	<item _id="47" type="1" name="君王" outfitType="[601]" affixAttr="{'306':4000,'308':4000}" needAttrAdd="{'102':2}" />
	<item _id="48" type="1" name="國王" outfitType="[601]" affixAttr="{'306':4000,'309':4000}" needAttrAdd="{'102':2}" />
	<item _id="49" type="1" name="大師" outfitType="[601]" affixAttr="{'306':4000,'310':4000}" needAttrAdd="{'102':2}" />
	<item _id="50" type="1" name="宗師" outfitType="[101]" affixAttr="{'207':6000}" needAttrAdd="{'102':2}" />
	<item _id="51" type="1" name="月精靈" outfitType="[101]" affixAttr="{'208':6000}" needAttrAdd="{'102':2}" />
	<item _id="52" type="1" name="田園" outfitType="[101]" affixAttr="{'209':6000}" needAttrAdd="{'102':2}" />
	<item _id="53" type="1" name="超自然" outfitType="[101]" affixAttr="{'210':6000}" needAttrAdd="{'102':2}" />
	<item _id="54" type="1" name="星間" outfitType="[101]" affixAttr="{'211':4000,'33':4000}" needAttrAdd="{'102':2}" />
	<item _id="55" type="1" name="極樂" outfitType="[101]" affixAttr="{'211':4000,'206':4000}" needAttrAdd="{'102':2}" />
	<item _id="56" type="1" name="天體" outfitType="[201,602]" affixAttr="{'212':6000}" needAttrAdd="{'102':2}" />
	<item _id="57" type="1" name="淨化" outfitType="[201,602]" affixAttr="{'212':4000,'21':4000}" needAttrAdd="{'102':2}" />
	<item _id="58" type="1" name="純潔" outfitType="[201,602]" affixAttr="{'212':4000,'22':4000}" needAttrAdd="{'102':2}" />
	<item _id="59" type="1" name="神聖" outfitType="[201,602]" affixAttr="{'212':4000,'23':4000}" needAttrAdd="{'102':2}" />
	<item _id="60" type="1" name="聖潔" outfitType="[201,602]" affixAttr="{'212':4000,'24':4000}" needAttrAdd="{'102':2}" />
	<item _id="61" type="1" name="神性" outfitType="[301,401]" affixAttr="{'32':6000}" needAttrAdd="{'102':2}" />
	<item _id="62" type="1" name="結實" outfitType="[301,401]" affixAttr="{'32':4000,'0':4000}" needAttrAdd="{'102':2}" />
	<item _id="63" type="1" name="強壯" outfitType="[301,401]" affixAttr="{'32':4000,'15':4000}" needAttrAdd="{'102':2}" />
	<item _id="64" type="1" name="光榮" outfitType="[501]" affixAttr="{'33':6000}" needAttrAdd="{'102':2}" />
	<item _id="65" type="1" name="祝福" outfitType="[501]" affixAttr="{'403':6000}" needAttrAdd="{'102':2}" />
	<item _id="66" type="1" name="崇高" outfitType="[301,401]" affixAttr="{'0':6000}" needAttrAdd="{'102':2}" />
	<item _id="67" type="1" name="神聖" outfitType="[201]" affixAttr="{'21':6000}" needAttrAdd="{'103':2}" />
	<item _id="68" type="1" name="神" outfitType="[201]" affixAttr="{'22':6000}" needAttrAdd="{'103':2}" />
	<item _id="69" type="1" name="忠誠" outfitType="[201]" affixAttr="{'23':6000}" needAttrAdd="{'103':2}" />
	<item _id="70" type="1" name="蜥蜴" outfitType="[201]" affixAttr="{'24':6000}" needAttrAdd="{'103':2}" />
	<item _id="71" type="1" name="蛇" outfitType="[501]" affixAttr="{'207':6000}" needAttrAdd="{'103':2}" />
	<item _id="72" type="1" name="海蛇" outfitType="[501]" affixAttr="{'208':6000}" needAttrAdd="{'103':2}" />
	<item _id="73" type="1" name="蜉蝣" outfitType="[501]" affixAttr="{'209':6000}" needAttrAdd="{'103':2}" />
	<item _id="74" type="1" name="龍" outfitType="[501]" affixAttr="{'210':6000}" needAttrAdd="{'103':2}" />
	<item _id="75" type="1" name="維特" outfitType="[602]" affixAttr="{'207':4000,'212':4000}" needAttrAdd="{'103':2}" />
	<item _id="76" type="1" name="巨飛龍" outfitType="[602]" affixAttr="{'208':4000,'212':4000}" needAttrAdd="{'103':2}" />
	<item _id="77" type="1" name="巴哈姆特" outfitType="[602]" affixAttr="{'209':4000,'212':4000}" needAttrAdd="{'103':2}" />
	<item _id="78" type="1" name="記憶" outfitType="[602]" affixAttr="{'210':4000,'212':4000}" needAttrAdd="{'103':2}" />
	<item _id="79" type="1" name="微光" outfitType="[201,602]" affixAttr="{'212':6000}" needAttrAdd="{'103':2}" />
	<item _id="80" type="1" name="發光" outfitType="[201,602]" affixAttr="{'218':6000}" needAttrAdd="{'103':2}" />
	<item _id="81" type="1" name="棗紅" outfitType="[301,401]" affixAttr="{'317':6000}" needAttrAdd="{'103':2}" />
	<item _id="82" type="1" name="赤褐" outfitType="[201]" affixAttr="{'17':6000}" needAttrAdd="{'103':2}" />
	<item _id="83" type="1" name="深紅" outfitType="[201]" affixAttr="{'18':6000}" needAttrAdd="{'103':2}" />
	<item _id="84" type="1" name="紅寶石" outfitType="[201]" affixAttr="{'19':6000}" needAttrAdd="{'103':2}" />
	<item _id="85" type="1" name="紅柑" outfitType="[201]" affixAttr="{'20':6000}" needAttrAdd="{'103':2}" />
	<item _id="86" type="1" name="赭黃" outfitType="[301,401]" affixAttr="{'318':6000}" needAttrAdd="{'103':2}" />
	<item _id="87" type="1" name="珊瑚" outfitType="[301]" affixAttr="{'318':4000,'312':4000}" needAttrAdd="{'103':2}" />
	<item _id="88" type="1" name="琥珀" outfitType="[401]" affixAttr="{'318':4000,'0':4000}" needAttrAdd="{'103':2}" />
	<item _id="89" type="1" name="海藍" outfitType="[301,401]" affixAttr="{'307':6000}" needAttrAdd="{'103':2}" />
	<item _id="90" type="1" name="銘綠" outfitType="[301,401]" affixAttr="{'308':6000}" needAttrAdd="{'103':2}" />
	<item _id="91" type="1" name="翠綠" outfitType="[301,401]" affixAttr="{'309':6000}" needAttrAdd="{'103':2}" />
	<item _id="92" type="1" name="祖母綠" outfitType="[301,401]" affixAttr="{'310':6000}" needAttrAdd="{'103':2}" />
	<item _id="93" type="1" name="閃耀" outfitType="[301]" affixAttr="{'307':4000,'312':4000}" needAttrAdd="{'103':2}" />
	<item _id="94" type="1" name="彩虹" outfitType="[301]" affixAttr="{'308':4000,'312':4000}" needAttrAdd="{'103':2}" />
	<item _id="95" type="1" name="閃閃發光" outfitType="[301]" affixAttr="{'309':4000,'312':4000}" needAttrAdd="{'103':2}" />
	<item _id="96" type="1" name="棱鏡" outfitType="[301]" affixAttr="{'310':4000,'312':4000}" needAttrAdd="{'103':2}" />
	<item _id="97" type="1" name="多彩" outfitType="[401]" affixAttr="{'307':4000,'0':4000}" needAttrAdd="{'103':2}" />
	<item _id="98" type="1" name="力量" outfitType="[401]" affixAttr="{'308':4000,'0':4000}" needAttrAdd="{'103':2}" />
	<item _id="99" type="1" name="武力" outfitType="[401]" affixAttr="{'309':4000,'0':4000}" needAttrAdd="{'103':2}" />
	<item _id="100" type="1" name="公牛" outfitType="[401]" affixAttr="{'310':4000,'0':4000}" needAttrAdd="{'103':2}" />
	<item _id="101" type="1" name="巨人" outfitType="[601]" affixAttr="{'307':4000,'218':4000}" needAttrAdd="{'103':2}" />
	<item _id="102" type="1" name="泰坦" outfitType="[601]" affixAttr="{'308':4000,'218':4000}" needAttrAdd="{'103':2}" />
	<item _id="103" type="1" name="擎天神" outfitType="[601]" affixAttr="{'309':4000,'218':4000}" needAttrAdd="{'103':2}" />
	<item _id="104" type="1" name="敏捷" outfitType="[601]" affixAttr="{'310':4000,'218':4000}" needAttrAdd="{'103':2}" />
	<item _id="105" type="2" name="技能" outfitType="[102,602]" affixAttr="{'101':7500}" needAttrAdd="{'101':2}" />
	<item _id="106" type="2" name="準確" outfitType="[201,602]" affixAttr="{'101':5000,'104':5000}" needAttrAdd="{'101':2}" />
	<item _id="107" type="2" name="精密" outfitType="[102,501]" affixAttr="{'201':5000,'21':5000}" needAttrAdd="{'101':2}" />
	<item _id="108" type="2" name="完美" outfitType="[102,501]" affixAttr="{'201':5000,'22':5000}" needAttrAdd="{'101':2}" />
	<item _id="109" type="2" name="涅磐" outfitType="[102,501]" affixAttr="{'201':5000,'23':5000}" needAttrAdd="{'101':2}" />
	<item _id="110" type="2" name="能源" outfitType="[102,501]" affixAttr="{'201':5000,'24':5000}" needAttrAdd="{'101':2}" />
	<item _id="111" type="2" name="思想" outfitType="[102,501]" affixAttr="{'201':5000,'33':5000}" needAttrAdd="{'101':2}" />
	<item _id="112" type="2" name="才華" outfitType="[102]" affixAttr="{'211':5000,'201':5000}" needAttrAdd="{'101':2}" />
	<item _id="113" type="2" name="幻術" outfitType="[201]" affixAttr="{'211':5000,'21':5000}" needAttrAdd="{'101':2}" />
	<item _id="114" type="2" name="法術" outfitType="[201]" affixAttr="{'211':5000,'22':5000}" needAttrAdd="{'101':2}" />
	<item _id="115" type="2" name="啓發" outfitType="[201]" affixAttr="{'211':5000,'23':5000}" needAttrAdd="{'101':2}" />
	<item _id="116" type="2" name="豺狼" outfitType="[201]" affixAttr="{'211':5000,'24':5000}" needAttrAdd="{'101':2}" />
	<item _id="117" type="2" name="狐狸" outfitType="[301,501,401]" affixAttr="{'15':5000,'17':5000}" needAttrAdd="{'101':2}" />
	<item _id="118" type="2" name="狂狼" outfitType="[301,501,401]" affixAttr="{'15':5000,'18':5000}" needAttrAdd="{'101':2}" />
	<item _id="119" type="2" name="猛虎" outfitType="[301,501,401]" affixAttr="{'15':5000,'19':5000}" needAttrAdd="{'101':2}" />
	<item _id="120" type="2" name="猛獁" outfitType="[301,501,401]" affixAttr="{'15':5000,'20':5000}" needAttrAdd="{'101':2}" />
	<item _id="121" type="2" name="太陽神" outfitType="[101,201,602]" affixAttr="{'102':7500}" needAttrAdd="{'102':2}" />
	<item _id="122" type="2" name="烏賊" outfitType="[201,602]" affixAttr="{'102':5000,'104':5000}" needAttrAdd="{'102':2}" />
	<item _id="123" type="2" name="巨鯨" outfitType="[101]" affixAttr="{'201':5000,'21':5000}" needAttrAdd="{'102':2}" />
	<item _id="124" type="2" name="光明" outfitType="[101]" affixAttr="{'201':5000,'22':5000}" needAttrAdd="{'102':2}" />
	<item _id="125" type="2" name="光彩" outfitType="[101]" affixAttr="{'201':5000,'23':5000}" needAttrAdd="{'102':2}" />
	<item _id="126" type="2" name="太陽" outfitType="[101]" affixAttr="{'201':5000,'24':5000}" needAttrAdd="{'102':2}" />
	<item _id="127" type="2" name="價值" outfitType="[101]" affixAttr="{'201':5000,'33':5000}" needAttrAdd="{'102':2}" />
	<item _id="128" type="2" name="測量" outfitType="[501]" affixAttr="{'21':5000,'201':5000}" needAttrAdd="{'102':2}" />
	<item _id="129" type="2" name="精靈" outfitType="[501]" affixAttr="{'22':5000,'201':5000}" needAttrAdd="{'102':2}" />
	<item _id="130" type="2" name="表現" outfitType="[501]" affixAttr="{'23':5000,'201':5000}" needAttrAdd="{'102':2}" />
	<item _id="131" type="2" name="超凡" outfitType="[501]" affixAttr="{'24':5000,'201':5000}" needAttrAdd="{'102':2}" />
	<item _id="132" type="2" name="工匠" outfitType="[101]" affixAttr="{'211':5000,'403':5000}" needAttrAdd="{'102':2}" />
	<item _id="133" type="2" name="品質" outfitType="[201]" affixAttr="{'211':5000,'21':5000}" needAttrAdd="{'102':2}" />
	<item _id="134" type="2" name="殘廢" outfitType="[201]" affixAttr="{'211':5000,'22':5000}" needAttrAdd="{'102':2}" />
	<item _id="135" type="2" name="屠殺" outfitType="[201]" affixAttr="{'211':5000,'23':5000}" needAttrAdd="{'102':2}" />
	<item _id="136" type="2" name="凝血" outfitType="[201]" affixAttr="{'211':5000,'24':5000}" needAttrAdd="{'102':2}" />
	<item _id="137" type="2" name="屠殺" outfitType="[301,401,601]" affixAttr="{'15':5000,'17':5000}" needAttrAdd="{'102':2}" />
	<item _id="138" type="2" name="殘殺" outfitType="[301,401,601]" affixAttr="{'15':5000,'18':5000}" needAttrAdd="{'102':2}" />
	<item _id="139" type="2" name="屠殺" outfitType="[301,401,601]" affixAttr="{'15':5000,'19':5000}" needAttrAdd="{'102':2}" />
	<item _id="140" type="2" name="吸精" outfitType="[301,401,601]" affixAttr="{'15':5000,'20':5000}" needAttrAdd="{'102':2}" />
	<item _id="141" type="2" name="火焰" outfitType="[501]" affixAttr="{'17':5000,'15':5000}" needAttrAdd="{'102':2}" />
	<item _id="142" type="2" name="烈火" outfitType="[501]" affixAttr="{'18':5000,'15':5000}" needAttrAdd="{'102':2}" />
	<item _id="143" type="2" name="燃燒" outfitType="[501]" affixAttr="{'19':5000,'15':5000}" needAttrAdd="{'102':2}" />
	<item _id="144" type="2" name="熾炎" outfitType="[501]" affixAttr="{'20':5000,'15':5000}" needAttrAdd="{'102':2}" />
	<item _id="145" type="2" name="霜凍" outfitType="[301,401]" affixAttr="{'317':5000,'15':5000}" needAttrAdd="{'102':2}" />
	<item _id="146" type="2" name="冰刺" outfitType="[301,401]" affixAttr="{'32':5000,'317':5000}" needAttrAdd="{'102':2}" />
	<item _id="147" type="2" name="冰河" outfitType="[201,602]" affixAttr="{'103':5000,'104':5000}" needAttrAdd="{'103':2}" />
	<item _id="148" type="2" name="嚴冬" outfitType="[201,602]" affixAttr="{'103':7500}" needAttrAdd="{'103':2}" />
	<item _id="149" type="2" name="衝擊" outfitType="[501]" affixAttr="{'21':7500}" needAttrAdd="{'103':2}" />
	<item _id="150" type="2" name="閃電" outfitType="[501]" affixAttr="{'22':7500}" needAttrAdd="{'103':2}" />
	<item _id="151" type="2" name="雷電" outfitType="[501]" affixAttr="{'23':7500}" needAttrAdd="{'103':2}" />
	<item _id="152" type="2" name="暴風" outfitType="[501]" affixAttr="{'24':7500}" needAttrAdd="{'103':2}" />
	<item _id="153" type="2" name="光明" outfitType="[201]" affixAttr="{'217':5000,'21':5000}" needAttrAdd="{'103':2}" />
	<item _id="154" type="2" name="毒素" outfitType="[201]" affixAttr="{'217':5000,'22':5000}" needAttrAdd="{'103':2}" />
	<item _id="155" type="2" name="瘟疫" outfitType="[201]" affixAttr="{'217':5000,'23':5000}" needAttrAdd="{'103':2}" />
	<item _id="156" type="2" name="炭疽" outfitType="[201]" affixAttr="{'217':5000,'24':5000}" needAttrAdd="{'103':2}" />
	<item _id="157" type="2" name="醫治" outfitType="[301,401,601]" affixAttr="{'17':5000,'317':5000}" needAttrAdd="{'103':2}" />
	<item _id="158" type="2" name="演進" outfitType="[501]" affixAttr="{'17':7500}" needAttrAdd="{'103':2}" />
	<item _id="159" type="2" name="反抗" outfitType="[301,401,601]" affixAttr="{'18':5000,'317':5000}" needAttrAdd="{'103':2}" />
	<item _id="160" type="2" name="妖巫" outfitType="[501]" affixAttr="{'18':7500}" needAttrAdd="{'103':2}" />
	<item _id="161" type="2" name="蝗蟲" outfitType="[301,401,601]" affixAttr="{'19':5000,'317':5000}" needAttrAdd="{'103':2}" />
	<item _id="162" type="2" name="八目鰻" outfitType="[501]" affixAttr="{'19':7500}" needAttrAdd="{'103':2}" />
	<item _id="163" type="2" name="蝙蝠" outfitType="[301,401,601]" affixAttr="{'20':5000,'317':5000}" needAttrAdd="{'103':2}" />
	<item _id="164" type="2" name="死靈" outfitType="[501]" affixAttr="{'20':7500}" needAttrAdd="{'103':2}" />
</root>

然後是代碼生成工具代碼

XmlCodeGenerator.py:

# -*- coding: utf-8 -*-
import xml.etree.ElementTree as et
import io

# 表名稱
xmlName = "PotentialTarget"
# xml文件目錄
xmlDataPath = "E://NewWork/Trunk/Assets/Resources/XML/CN/"
# cs代碼導出目錄
outputDataPath = "C://Users/PC/Desktop/"

tree = et.parse(xmlDataPath + xmlName + ".xml")
root = tree.getroot()
node = root.find('item')
numList = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
typeDict = {}

for k, v in node.items():
    if v == "":
        typeDict[k] = "string"
        continue

    char1 = v[0]
    if char1 in numList:
        typeDict[k] = "int"
    elif char1 == '[':
        char2 = v[1]
        if char2 in numList:
            typeDict[k] = "numList"
        else:
            typeDict[k] = "strList"
    elif char1 == '{':
        isNum = False
        for i in range(len(v)):
            if v[i] == ':':
                if i+1 < len(v):
                    isNum = v[i+1] in numList
        typeDict[k] = "numDict" if isNum else "strDict"
    else:
        typeDict[k] = "string"

# print(typeDict)

file = open(outputDataPath + 'X' + xmlName + ".cs", 'w+')
writeStr = "using System.Collections;\nusing System.Collections.Generic;\nusing UnityEngine;\n\n"

# Init val
writeStr += "public class X" + xmlName + " : XSuper\n{\n\tpublic override string _id { get; protected set; }\n"
writeStr += "\tpublic int id { get; private set; }\n"
for k, v in typeDict.items():
    if k == '_id':
        continue
    if v == "int":
        writeStr += "\tpublic int " + k + " { get; private set; }\n"
    elif v == "string":
        writeStr += "\tpublic string " + k + " { get; private set; }\n"
    elif v == "numList":
        writeStr += "\n\tpublic string " + k + " = string.Empty;\n"
        writeStr += "\tpublic List<int> " + k + "List { get; private set; }\n"
    elif v == "strList":
        writeStr += "\n\tpublic string " + k + " = string.Empty;\n"
        writeStr += "\tpublic List<string> " + k + "List { get; private set; }\n"
    elif v == "numDict":
        writeStr += "\n\tpublic string " + k + " { get; private set; }\n"
        writeStr += "\tpublic Dictionary<string, int> " + k + "Dict { get; private set; }\n"
    elif v == "strDict":
        writeStr += "\n\tpublic string " + k + " { get; private set; }\n"
        writeStr += "\tpublic Dictionary<string, string> " + k + "Dict { get; private set; }\n"

# Init From Xml
writeStr += '\n\tpublic override void InitFromXml(XmlReadTool tool, int iCount)\n\t{\n\t\t_id = tool.GetAttribute("_id", "", iCount);\n'
for k, v in typeDict.items():
    if k == '_id':
        continue
    if v == "int":
        writeStr += "\t\t" + k + ' = tool.GetAttribute("' + k + '", 0, iCount);\n'
    else:
        writeStr += "\t\t" + k + ' = tool.GetAttribute("' + k + '", "", iCount);\n'

# Init Property
writeStr += "\t}\n\n\tpublic override void InitProperty()\n\t{\n"
for k, v in typeDict.items():
    if k == '_id':
        writeStr += "\t\tid = int.Parse(_id);\n"
    if v == "numDict":
        writeStr += "\t\t" + k + 'Dict = JReadTool.Instance.ParseJsonToObject<Dictionary<string, int>>(' + k + ');\n'
    elif v == "strDict":
        writeStr += "\t\t" + k + 'Dict = JReadTool.Instance.ParseJsonToObject<Dictionary<string, string>>(' + k + ');\n'
    elif v == "numList":
        writeStr += "\t\tif (!string.IsNullOrEmpty(" + k + "))\n\t\t{\n\t\t\t" + k + "List = JReadTool.Instance.ParseJsonToObject<List<int>>(" + k + ");\n\t\t}\n"
    elif v == "strList":
        writeStr += "\t\tif (!string.IsNullOrEmpty(" + k + "))\n\t\t{\n\t\t\t" + k + "List = JReadTool.Instance.ParseJsonToObject<List<string>>(" + k + ");\n\t\t}\n"

# Parse Asset Xml
writeStr += "\t}\n\n\tpublic void ParseAssetXml(AssetItem_X" + xmlName + " vAssetData)\n\t{\n"
for k, v in typeDict.items():
    if v == "numDict" or v == "strDict":
        writeStr += "\t\t" + k + "Dict = vAssetData.Deserialize_" + k + "Dict();\n"
    elif v == "numList" or v == "strList":
        writeStr += "\t\t" + k + "List = vAssetData." + k + "List;\n"
    else:
        writeStr += "\t\t" + k + " = vAssetData." + k + ";\n"
        if k == "_id":
            writeStr += "\t\t" + "id = vAssetData.id;\n"
writeStr += "\t}\n\n}"

file.write(writeStr)
file.close()

file = open(outputDataPath + 'AssetRoot_X' + xmlName + ".cs", 'w+')
writeStr = "using UnityEngine;\nusing System.Collections;\nusing System;\nusing System.Collections.Generic;\n\n"

writeStr += "public class AssetRoot_X" + xmlName + " : IAssetRoot_Base\n{\n\t[HideInInspector]\n"
writeStr += "\tpublic List<AssetItem_X" + xmlName + "> Itemlist = new List<AssetItem_X" + xmlName + ">();\n}\n\n"
writeStr += "[Serializable]\npublic class AssetItem_X" + xmlName + '\n{\n\t[XObject("_id")]\n\tpublic string _id;\n\tpublic int id;\n\n'

# Init Val
for k, v in typeDict.items():
    if k == '_id':
        continue
    if v == "int":
        writeStr += '\t[XObject("' + k + '")]\n\tpublic int ' + k + ';\n\n'
    elif v == "string":
        writeStr += '\t[XObject("' + k + '")]\n\tpublic string ' + k + ';\n\n'
    else:
        writeStr += '\t[XObject("' + k + '")]\n\tpublic string ' + k + ';\n'
        if v == "numList":
            writeStr += "\tpublic List<int> " + k + "List = null;\n\n"
        elif v == "strList":
            writeStr += "\tpublic List<string> " + k + "List = null;\n\n"
        elif v == "numDict":
            writeStr += "\tpublic List<string> " + k + "Dict_Key = null;\n\tpublic List<int> " + k + "Dict_Value = null;\n\n"
        elif v == "strDict":
            writeStr += "\tpublic List<string> " + k + "Dict_Key = null;\n\tpublic List<string> " + k + "Dict_Value = null;\n\n"

# Serializable Dict && Deserialize Dict
for k, v in typeDict.items():
    dictType = ""
    if v == "numDict":
        dictType = "int"
    elif v == "strDict":
        dictType = "string"

    if dictType != "":
        # Serializable
        writeStr += "\tprivate void Serializable_" + k + "Dict(Dictionary<string, " + dictType + "> vDict)\n\t{\n\t\tif (vDict == null) return;\n"
        writeStr += "\t\tif (" + k + "Dict_Key == null) " + k + "Dict_Key = new List<string>(vDict.Keys);\n"
        writeStr += "\t\tif (" + k + "Dict_Value == null) " + k + "Dict_Value = new List<" + dictType + ">(vDict.Values);\n\t}\n\n"
        # Deserialize
        writeStr += "\tpublic Dictionary<string, " + dictType + "> Deserialize_" + k + "Dict()\n\t{\n"
        writeStr += "\t\tif (" + k + "Dict_Key == null) return null;\n\t\tif (" + k + "Dict_Key.Count <= 0) return null;\n"
        writeStr += "\t\tDictionary<string, " + dictType + "> vReData = new Dictionary<string, " + dictType + ">();\n"
        writeStr += "\t\tfor (int iCount = 0; iCount < " + k + "Dict_Key.Count; iCount++)\n\t\t{\n"
        writeStr += "\t\t\tvReData.Add(" + k + "Dict_Key[iCount], " + k + "Dict_Value[iCount]);\n\t\t}\n\t\treturn vReData;\n\t}\n\n"

# Run Parse
writeStr += "\tpublic void RunParse()\n\t{\n\t\tid = int.Parse(_id);\n\n"
for k, v in typeDict.items():
    dictType = ""
    listType = ""
    if v == "numDict":
        dictType = "int"
    elif v == "strDict":
        dictType = "string"
    elif v == "numList":
        listType = "int"
    elif v == "strList":
        listType = "string"

    if dictType != "":
        writeStr += "\t\tif (!string.IsNullOrEmpty(" + k + "))\n\t\t{\n\t\t\t"
        writeStr += "Serializable_" + k + "Dict" + "(JReadTool.Instance.ParseJsonToObject<Dictionary<string, " + dictType + ">>(" + k + "));\n"
        writeStr += "\t\t\t" + k + " = string.Empty;\n\t\t}\n\n"
    elif listType != "":
        writeStr += "\t\tif (!string.IsNullOrEmpty(" + k + "))\n\t\t{\n\t\t\t"
        writeStr += k + "List = JReadTool.Instance.ParseJsonToObject<List<" + listType + ">>(" + k + ");\n"
        writeStr += "\t\t\t" + k + " = string.Empty;\n\t\t}\n\n"
writeStr += "\t}\n\n}"

file.write(writeStr)
file.close()

最後是生成的兩個cs代碼

XOutfitAffix.cs:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class XOutfitAffix : XSuper
{
	public override string _id { get; protected set; }
	public int type { get; private set; }
	public string name { get; private set; }

	public string outfitType = string.Empty;
	public List<int> outfitTypeList { get; private set; }

	public string affixAttr { get; private set; }
	public Dictionary<string, int> affixAttrDict { get; private set; }

	public string needAttrAdd { get; private set; }
	public Dictionary<string, int> needAttrAddDict { get; private set; }

	public override void InitFromXml(XmlReadTool tool, int iCount)
	{
		_id = tool.GetAttribute("_id", "", iCount);
		type = tool.GetAttribute("type", 0, iCount);
		name = tool.GetAttribute("name", "", iCount);
		outfitType = tool.GetAttribute("outfitType", "", iCount);
		affixAttr = tool.GetAttribute("affixAttr", "", iCount);
		needAttrAdd = tool.GetAttribute("needAttrAdd", "", iCount);
	}

	public override void InitProperty()
	{
		if (!string.IsNullOrEmpty(outfitType))
		{
			outfitTypeList = JReadTool.Instance.ParseJsonToObject<List<int>>(outfitType);
		}
		affixAttrDict = JReadTool.Instance.ParseJsonToObject<Dictionary<string, int>>(affixAttr);
		needAttrAddDict = JReadTool.Instance.ParseJsonToObject<Dictionary<string, int>>(needAttrAdd);
	}

	public void ParseAssetXml(AssetItem_XOutfitAffix vAssetData)
	{
		_id = vAssetData._id;
		type = vAssetData.type;
		name = vAssetData.name;
		outfitTypeList = vAssetData.outfitTypeList;
		affixAttrDict = vAssetData.Deserialize_affixAttrDict();
		needAttrAddDict = vAssetData.Deserialize_needAttrAddDict();
	}

}

 AssetRoot_XOutfitAffix.cs:

using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;

public class AssetRoot_XOutfitAffix : IAssetRoot_Base
{
	[HideInInspector]
	public List<AssetItem_XOutfitAffix> Itemlist = new List<AssetItem_XOutfitAffix>();
}

[Serializable]
public class AssetItem_XOutfitAffix
{
	[XObject("_id")]
	public string _id;
	public int id;

	[XObject("type")]
	public int type;

	[XObject("name")]
	public string name;

	[XObject("outfitType")]
	public string outfitType;
	public List<int> outfitTypeList = null;

	[XObject("affixAttr")]
	public string affixAttr;
	public List<string> affixAttrDict_Key = null;
	public List<int> affixAttrDict_Value = null;

	[XObject("needAttrAdd")]
	public string needAttrAdd;
	public List<string> needAttrAddDict_Key = null;
	public List<int> needAttrAddDict_Value = null;

	private void Serializable_affixAttrDict(Dictionary<string, int> vDict)
	{
		if (vDict == null) return;
		if (affixAttrDict_Key == null) affixAttrDict_Key = new List<string>(vDict.Keys);
		if (affixAttrDict_Value == null) affixAttrDict_Value = new List<int>(vDict.Values);
	}

	public Dictionary<string, int> Deserialize_affixAttrDict()
	{
		if (affixAttrDict_Key == null) return null;
		if (affixAttrDict_Key.Count <= 0) return null;
		Dictionary<string, int> vReData = new Dictionary<string, int>();
		for (int iCount = 0; iCount < affixAttrDict_Key.Count; iCount++)
		{
			vReData.Add(affixAttrDict_Key[iCount], affixAttrDict_Value[iCount]);
		}
		return vReData;
	}

	private void Serializable_needAttrAddDict(Dictionary<string, int> vDict)
	{
		if (vDict == null) return;
		if (needAttrAddDict_Key == null) needAttrAddDict_Key = new List<string>(vDict.Keys);
		if (needAttrAddDict_Value == null) needAttrAddDict_Value = new List<int>(vDict.Values);
	}

	public Dictionary<string, int> Deserialize_needAttrAddDict()
	{
		if (needAttrAddDict_Key == null) return null;
		if (needAttrAddDict_Key.Count <= 0) return null;
		Dictionary<string, int> vReData = new Dictionary<string, int>();
		for (int iCount = 0; iCount < needAttrAddDict_Key.Count; iCount++)
		{
			vReData.Add(needAttrAddDict_Key[iCount], needAttrAddDict_Value[iCount]);
		}
		return vReData;
	}

	public void RunParse()
	{
		id = int.Parse(_id);

		if (!string.IsNullOrEmpty(outfitType))
		{
			outfitTypeList = JReadTool.Instance.ParseJsonToObject<List<int>>(outfitType);
			outfitType = string.Empty;
		}

		if (!string.IsNullOrEmpty(affixAttr))
		{
			Serializable_affixAttrDict(JReadTool.Instance.ParseJsonToObject<Dictionary<string, int>>(affixAttr));
			affixAttr = string.Empty;
		}

		if (!string.IsNullOrEmpty(needAttrAdd))
		{
			Serializable_needAttrAddDict(JReadTool.Instance.ParseJsonToObject<Dictionary<string, int>>(affixAttr));
			needAttrAdd = string.Empty;
		}

	}

}

以上!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章