如何将XML转换为Python对象?

时间:2009-01-06 22:47:15

标签: python xml

我需要加载XML文件并将内容转换为面向对象的Python结构。我想接受这个:

<main>
    <object1 attr="name">content</object>
</main>

把它变成这样的东西:

main
main.object1 = "content"
main.object1.attr = "name"

XML数据将具有比这更复杂的结构,我无法对元素名称进行硬编码。解析时需要收集属性名称并将其用作对象属性。

如何将XML数据转换为Python对象?

7 个答案:

答案 0 :(得分:46)

值得关注lxml.objectify

xml = """<main>
<object1 attr="name">content</object1>
<object1 attr="foo">contenbar</object1>
<test>me</test>
</main>"""

from lxml import objectify

main = objectify.fromstring(xml)
main.object1[0]             # content
main.object1[1]             # contenbar
main.object1[0].get("attr") # name
main.test                   # me

或者反过来构建xml结构:

item = objectify.Element("item")
item.title = "Best of python"
item.price = 17.98
item.price.set("currency", "EUR")

order = objectify.Element("order")
order.append(item)
order.item.quantity = 3
order.price = sum(item.price * item.quantity for item in order.item)

import lxml.etree
print(lxml.etree.tostring(order, pretty_print=True))

输出:

<order>
  <item>
    <title>Best of python</title>
    <price currency="EUR">17.98</price>
    <quantity>3</quantity>
  </item>
  <price>53.94</price>
</order>

答案 1 :(得分:9)

我今天不止一次推荐这个,但请尝试Beautiful Soup(easy_install BeautifulSoup)。

from BeautifulSoup import BeautifulSoup

xml = """
<main>
    <object attr="name">content</object>
</main>
"""

soup = BeautifulSoup(xml)
# look in the main node for object's with attr=name, optionally look up attrs with regex
my_objects = soup.main.findAll("object", attrs={'attr':'name'})
for my_object in my_objects:
    # this will print a list of the contents of the tag
    print my_object.contents
    # if only text is inside the tag you can use this
    # print tag.string

答案 2 :(得分:4)

David Mertz gnosis.xml.objectify似乎会为你做这件事。文档有点难以获得,但有一些IBM文章,包括this one

from gnosis.xml import objectify

xml = "<root><nodes><node>node 1</node><node>node 2</node></nodes></root>"
root = objectify.make_instance(xml)

print root.nodes.node[0].PCDATA # node 1
print root.nodes.node[1].PCDATA # node 2

以这种方式从对象创建xml是另一回事。

答案 3 :(得分:1)

答案 4 :(得分:1)

#@Stephen: 
#"can't hardcode the element names, so I need to collect them 
#at parse and use them somehow as the object names."

#I don't think thats possible. Instead you can do this. 
#this will help you getting any object with a required name.

import BeautifulSoup


class Coll(object):
    """A class which can hold your Foo clas objects 
    and retrieve them easily when you want
    abstracting the storage and retrieval logic
    """
    def __init__(self):
        self.foos={}        

    def add(self, fooobj):
        self.foos[fooobj.name]=fooobj

    def get(self, name):
        return self.foos[name]

class Foo(object):
    """The required class
    """
    def __init__(self, name, attr1=None, attr2=None):
        self.name=name
        self.attr1=attr1
        self.attr2=attr2

s="""<main>
         <object name="somename">
             <attr name="attr1">value1</attr>
             <attr name="attr2">value2</attr>
         </object>
         <object name="someothername">
             <attr name="attr1">value3</attr>
             <attr name="attr2">value4</attr>
         </object>
     </main>
"""

soup=BeautifulSoup.BeautifulSoup(s)


bars=Coll()
for each in soup.findAll('object'):
    bar=Foo(each['name'])
    attrs=each.findAll('attr')
    for attr in attrs:
        setattr(bar, attr['name'], attr.renderContents())
    bars.add(bar)


#retrieve objects by name
print bars.get('somename').__dict__

print '\n\n', bars.get('someothername').__dict__

输出

{'attr2': 'value2', 'name': u'somename', 'attr1': 'value1'}


{'attr2': 'value4', 'name': u'someothername', 'attr1': 'value3'}

答案 5 :(得分:0)

python有三种常见的XML解析器:xml.dom.minidom,elementree和BeautifulSoup。

IMO,BeautifulSoup是迄今为止最好的。

http://www.crummy.com/software/BeautifulSoup/

答案 6 :(得分:-1)

如果搜索代码生成器无法使用,您可以自己编写使用XML作为输入并以您选择的语言输出对象。

这并不是非常困难,但是Parse XML,Generate Code,Compile / Execute Script的三步过程确实使调试变得更加困难。