[Solved] How to convert xml objects to python objects? [closed]


I’d go with using xmltodict:

# -*- coding: utf-8 -*-
import xmltodict

data = """<wordbook>
  <item>
    <name>engrossment</name>
    <phonetic><![CDATA[ɪn'grəʊsmənt]]></phonetic>
    <meaning><![CDATA[n. 正式缮写的文件,专注]]></meaning>
  </item>
  <item>
    <name>graffiti</name>
    <phonetic><![CDATA[ɡrəˈfi:ti:]]></phonetic>
    <meaning><![CDATA[n.在墙上的乱涂乱写(复数形式)]]></meaning>
  </item>
  <item>
    <name>pathology</name>
    <phonetic><![CDATA[pæˈθɔlədʒi:]]></phonetic>
    <meaning><![CDATA[n. 病理(学);〈比喻〉异常状态]]></meaning>
  </item>
</wordbook>"""

data = xmltodict.parse(data, encoding='utf-8')

for item in data['wordbook']['item']:
    print item['name']

prints:

engrossment
graffiti
pathology

You can also use BeautifulSoup or lxml – it’s a matter of taste. The idea is pretty much the same – iterate over item tags and instantiate Item in the loop.

Hope that helps.

2

solved How to convert xml objects to python objects? [closed]