如何使用ElementMaker向元素添加属性? [英] How do I add attributes to elements with ElementMaker?

查看:370
本文介绍了如何使用ElementMaker向元素添加属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须生成如下的XML,

I have to generate an XML as below,

<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
<serviceConfiguration xmlns="http://blah.com/serviceConfiguration">
  <node name="node1">
    <hostName>host1</hostName>
    <networkInterface name="eth0">
      <ipv4Address>192.168.1.3</ipv4Address>
      <ipv6Address>2a00:4a00:a000:11a0::a4f:3</ipv6Address>
      <domainName>asdf.net</domainName>
      <ipv4Netmask>255.255.255.0</ipv4Netmask>
      <ipv6Netmask>ffff:ffff:ffff:ffff::</ipv6Netmask>
    </networkInterface>
    <userAccount>
      <uid>root</uid>
      <uidNumber>0</uidNumber>
      <gidNumber>0</gidNumber>
      <homeDirectory>/root</homeDirectory>
      <publicKey>
        <key/>
        <algorithm>RSA</algorithm>
      </publicKey>
      </userAccount>
  </node>
</serviceConfiguration>

我一直在尝试的代码(如下所示)可以使一切正常,但是我无法为节点和网络接口设置属性值. 我需要<node name="node1">而不是<node><networkInterface name="eth0">而不是<networkInterface>.我尝试在节点和网络接口的括号中添加属性,但是python似乎不接受.

The code I have been trying (below) generates all things fine but I am not able to set attributes value for node and network interface. I need <node name="node1"> instead of <node> and <networkInterface name="eth0"> instead of <networkInterface>. I tried adding attributes in bracket for node and network interface but python doesn't seem to take it.

ElementMaker不接受传递给head的属性.什么是合适的语法来做到这一点?如何实现?

ElementMaker does not take attributes passed to head. What would be the appropriate syntax to do that? How is it possible to achieve?

代码:

from lxml import etree
from lxml.builder import ElementMaker

E = ElementMaker(namespace="http://blah.com/serviceConfiguration", nsmap={None: "http://blah.com/serviceConfiguration"})

SC = E.serviceConfiguration
NODE = E.node
HN = E.hostName
NI = E.networkInterface
I4 = E.ipv4Address
I6 = E.ipv6Address
DN = E.domainName
I4N = E.ipv4Netmask
I6N = E.ipv6Netmask
UA = E.userAccount
UI = E.uid
UIN = E.uidNumber
GIN = E.gidNumber
HD = E.homeDirectory
PK = E.publicKey
K = E.key
A = E.algorithm

my_doc = SC(
      NODE(
        HN('host1'),
        NI(
          I4('ipv4Address'),
          I6('ipv6Address'),
          DN('domainName'),
          I4N('ipv4Netmask'),
          I6N('ipv6Netmask')
          ),
        UA(
          UI('uid'),
          UIN('uidNumber'),
          GIN('gidNumber'),
          HD('homeDirectory'),
          PK(
            K('key'),
            A('algorithm')
            )
          )
        )
      )

print(etree.tostring(my_doc, encoding="UTF-8", standalone="yes", pretty_print=True))

推荐答案

在子元素之后将属性添加为关键字参数:

Add the attributes as keyword arguments after the subelements:

my_doc = SC(
    NODE(
        HN('host1'),
        NI(
            I4('ipv4Address'),
            I6('ipv6Address'),
            DN('domainName'),
            I4N('ipv4Netmask'),
            I6N('ipv6Netmask'),
            name="eth0"),
        UA(
            UI('uid'),
            UIN('uidNumber'),
            GIN('gidNumber'),
            HD('homeDirectory'),
            PK(
                K('key'),
                A('algorithm')
            )
          ),
          name="node1")
)

或通过字典提供属性:

my_doc = SC(
    NODE({'name': 'node1'},
        HN('host1'),
        NI(
            I4('ipv4Address'),
            I6('ipv6Address'),
            DN('domainName'),
            I4N('ipv4Netmask'),
            I6N('ipv6Netmask'),
            name="eth0"),
        UA(
            UI('uid'),
            UIN('uidNumber'),
            GIN('gidNumber'),
            HD('homeDirectory'),
            PK(
                K('key'),
                A('algorithm')
            )
        )
     )
)

这篇关于如何使用ElementMaker向元素添加属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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