37,718
社区成员
发帖
与我相关
我的任务
分享
<?xml version="1.0" encoding="utf-8"?>
<config desc="Line8Center" line="8">
<stations>
<ip base="192.168.8" />
<station name="市光路站" />
<station name="嫩江路站" />
<station name="翔殷路站" />
<station name="黄兴公园站" />
<station name="延吉中路站" />
<station name="黄兴路站" />
<station name="江浦路站" />
<station name="鞍山新村站" />
<station name="四平路站" />
<station name="曲阳路站" />
<station name="虹口足球场站" />
<station name="西藏北路站" />
<station name="中兴路站" />
<station name="曲阜路站" />
<station name="人民广场站" />
<station name="大世界站" />
<station name="老西门站" />
<station name="陆家浜路站" />
<station name="西藏南路站" />
<station name="周家渡路站" />
<station name="耀华路站" />
<station name="成山路站" />
<station name="杨思站" />
<station name="济阳路站" />
<station name="凌兆新村" />
<station name="芦恒路站" />
<station name="浦江镇站" />
<station name="江月路站" />
<station name="浦江世博家园" />
<station name="航天公园" />
<station name="车辆段" is_park="True" />
<station name="停车场" is_park="True" />
</stations>
<boxes>
<box name="防灾调" id="1" port="9" in="3" rec="8" />
<box name="总调" id="2" port="7" in="1" rec="6" />
<box name="列调" id="3" port="8" in="2" rec="7" />
</boxes>
<pis remote="192.168.8.223:1001" />
</config>
import elementtree.ElementTree as ET
def attr_gbk(elem, name):
return elem.attrib.get(name).encode("GBK")
def attr_int(elem, name):
return int(elem.attrib.get(name))
def attr_bool(elem, name):
return bool(elem.attrib.get(name))
def attr(elem, *args):
if len(args) > 1:
result = []
for name in args:
result.append(attr(elem, name))
return result
[name] = args
value = elem.attrib.get(name)
if not value:
return value
elif isinstance(value, unicode):
return value.encode("GBK")
elif value.isdigit():
return int(value)
elif value.lower() in ["true", "false"]:
return value.lower() == "true"
else:
return value
def fix_attribs(elem):
dict_ = {}
for key in elem.attrib:
dict_.update({key: attr(elem, key)})
return dict_
def build_dict(elem):
if elem is not None:
dict_ = {}
for subelem in elem:
if subelem.tag in dict_:
if not isinstance(dict_[subelem.tag], list):
dict_[subelem.tag] = [dict_[subelem.tag]]
dict_[subelem.tag].append(build_dict(subelem))
else:
dict_.update({subelem.tag: build_dict(subelem)})
if subelem.text and subelem.text.strip():
dict_.update({subelem.tag: {"_text": subelem.text}})
dict_.update(fix_attribs(elem))
return dict_
else:
return fix_attribs(elem)
import dictxml
tree = dictxml.ET.parse("1.xml")
root = tree.getroot()
print dictxml.build_dict(root)
xml = '''<?xml version="1.0" encoding="utf-8"?>
<Conf>
<DBconf>
<DB>
<Desc>abc </Desc>
<ConnStr>DB_192.168.3.1 </ConnStr>
<UserName>system </UserName>
<PassWd>aa </PassWd>
</DB>
<DB>
<Desc>bb </Desc>
<ConnStr>ORA9i_192.168.3.1 </ConnStr>
<UserName>system </UserName>
<PassWd>system </PassWd>
</DB>
<DB>
<Desc>ddd </Desc>
<ConnStr>ORA9i_192.168.3.1 </ConnStr>
<UserName>system </UserName>
<PassWd>system </PassWd>
</DB>
</DBconf>
<Otherconf>
<a>aaa </a>
<c>aaa </c>
</Otherconf>
</Conf>
'''
from xml.dom import minidom
def get_childern(node):
return filter(lambda n: isinstance(n, minidom.Element), node.childNodes)
doc = minidom.parseString(xml)
root = doc.childNodes[0]
DBconf, Otherconf = get_childern(root)
for DB in get_childern(DBconf):
for n in get_childern(DB):
print n.tagName, n.firstChild.data
print
for n in get_childern(Otherconf):
print n.tagName, n.firstChild.data