开发者

Get a json dictionary from xml file [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

easiest way to parse xml in python

I need to parse a file which looks like an xml file but have no XML declaration.

Here is an example of XML file:

<connection name="name_1">
  <parameter name="user" value="user_value_1"/>
  <parameter name="password" value="psw_1"/>
</connection>
<connection name="name_2">
  <parameter name="user" value="user_value_2"/>
  <parameter name="password" value="psw_2"/>
</connection>

<connection name="name_n">
  <parameter name="user" value="user_value_n"/>
  <parameter name="password" value="psw_n"/>
</connection>

My question is, which libraries can I use to parse the current file?

In practice, given the current file how can obtain the output:

{开发者_运维百科"connection names":["name_1","name_2",…,"name_n"]}

Thanks,

Antonio


Your XML is invalid and won't be parsed properly, as it doesn't contain a main element. Here's a valid version:

<connections>
  <connection name="name_1">
    <parameter name="user" value="user_value_1"/>
    <parameter name="password" value="psw_1"/>
  </connection>

  <connection name="name_2">
    <parameter name="user" value="user_value_2"/>
    <parameter name="password" value="psw_2"/>
  </connection>

  <connection name="name_n">
    <parameter name="user" value="user_value_n"/>
    <parameter name="password" value="psw_n"/>
  </connection>
</connections>

You can use minidom to parse it. Yes it's kinda slow for lots of elements, but I can't help but use something which feels just like JavaScript:

from xml.dom.minidom import parseString

document = parseString('''
<?xml version="1.0"?>
<connections>
  <connection name="name_1">
    <parameter name="user" value="user_value_1"/>
    <parameter name="password" value="psw_1"/>
  </connection>

  <connection name="name_2">
    <parameter name="user" value="user_value_2"/>
    <parameter name="password" value="psw_2"/>
  </connection>

  <connection name="name_n">
    <parameter name="user" value="user_value_n"/>
    <parameter name="password" value="psw_n"/>
  </connection>
</connections>''')

names = {'connection names': []}

for connection in document.getElementsByTagName('connection'):
  names['connection names'].append(connection.getAttribute('name'))

print names

And the output is:

 {'connection names': [u'name_1', u'name_2', u'name_n']}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜