1
私は以下のようにXMLを生成する必要が
、ElementMakerで要素に属性を追加するにはどうすればよいですか?
<?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>
私は(下記)しようとしているコードは、罰金すべてのものを生成しますが、私は、ノードおよびネットワーク・インタフェースのための属性値を設定することはできませんよ。 <networkInterface>
の代わりに<node>
と<networkInterface name="eth0">
の代わりに<node name="node1">
が必要です。私はノードとネットワークインターフェイスのために角括弧内に属性を追加しようとしましたが、Pythonはそれをとらえていません。
ElementMakerはheadに渡される属性を使用しません。それを行うための適切な構文は何でしょうか?どのように達成することが可能ですか?
コード:
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))