Python-如何使用lxml.objectify多次附加相同的XML元素 [英] Python - How to append the same XML element multiple times with lxml.objectify

查看:86
本文介绍了Python-如何使用lxml.objectify多次附加相同的XML元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用lxml.objectify

I have the following XML that I am trying to recreate with the lxml.objectify package

<file>
  <customers>
    <customer>
        <phone>
            <type>home</type>
            <number>555-555-5555</number>
        </phone>
        <phone>
            <type>cell</type>
            <number>999-999-9999</number>
        </phone>
        <phone>
            <type>home</type>
            <number>111-111-1111</number>
        </phone>
    </customer>
   </customers>
</file>

我不知道如何多次创建phone元素.基本上,我有以下无效代码:

I can't figure out how to create the phone element multiple times. Basically, I have the following non-working code:

    # create phone element 1
    root.customers.customer.phone = ""
    root.customers.customer.phone.type = data_dict['PRIMARY PHONE1']
    root.customers.customer.phone.number = data_dict['PRIMARY PHONE TYPE 1']

    # create phone element 2
    root.customers.customer.phone = ""
    root.customers.customer.phone.type = data_dict['PRIMARY PHONE2']
    root.customers.customer.phone.number = data_dict['PRIMARY PHONE TYPE 2']

    # create phone element 3
    root.customers.customer.phone = ""
    root.customers.customer.phone.type = data_dict['PRIMARY PHONE3']
    root.customers.customer.phone.number = data_dict['PRIMARY PHONE TYPE 3']

当然,在生成的XML中仅输出一部分电话信息.有人有什么想法吗?

Which of course only outputs one section of phone information in the resulting XML. Does anyone have any ideas?

推荐答案

您应创建 objectify.Element 对象,并将它们添加为 root.customers 的子代.

You should create objectify.Element objects, and add them as children of root.customers.

例如,可以这样插入两个电话号码:

For example, inserting two phone numbers can be done like this:

phone = objectify.Element('phone')
phone.type = data_dict['PRIMARY PHONE1']
phone.number = data_dict['PRIMARY PHONE TYPE 1']
root.customers.customer.append(phone)

phone = objectify.Element('phone')
phone.type = data_dict['PRIMARY PHONE2']
phone.number = data_dict['PRIMARY PHONE TYPE 2']
root.customers.customer.append(phone)

如果在将xml转换回字符串时在这些元素上获得不必要的属性,请使用 objectify.deannotate(root,xsi_nil = True,cleanup_namespaces = True).有关 objectify.deannotate 的确切参数,请参见 lxml的对象化文档.

If you get unnecessary attributes on these elements when transforming the xml back to a string, use objectify.deannotate(root, xsi_nil=True, cleanup_namespaces=True). See lxml's objectify documentation for exact parameters of objectify.deannotate.

((如果您使用的是旧版本的lxml,其中不包含 cleanup_namespaces 关键字参数,请执行以下操作:

(If you're using an older version of lxml, which doesn't include the cleanup_namespaces keyword argument, do this instead:

from lxml import etree
# ...
objectify.deannotate(root, xsi_nil=True)
etree.cleanup_namespaces(root)

)

这篇关于Python-如何使用lxml.objectify多次附加相同的XML元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆