使用 lxml 库的 objectify API 为元素设置值,将自动检测到的 pytype 分配给该元素和所需的命名空间, 默认情况下。
例如设置根元素:
root = objectify.Element('root')
print(etree.tostring(root, pretty_print=True).decode('utf-8'))
输出:
<root xmlns:py="http://codespeak.net/lxml/objectify/pytype"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" py:pytype="TREE"/>
或为子元素设置一个值:
child = objectify.SubElement(root, 'child')
root.child = 'value'
print(etree.tostring(root, pretty_print=True).decode('utf-8'))
输出:
<root xmlns:py="http://codespeak.net/lxml/objectify/pytype"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" py:pytype="TREE">
<child py:pytype="str">value</child>
</root>
即使使用 ObjectPath 的 setattr:
path = objectify.ObjectPath('root.vader.son')
path.setattr(root, 'Luke')
print(etree.tostring(root, pretty_print=True).decode('utf-8'))
输出:
<root xmlns:py="http://codespeak.net/lxml/objectify/pytype"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" py:pytype="TREE">
<child py:pytype="str">value</child>
<vader>
<son py:pytype="str">Luke</son>
</vader>
</root>
有一些解决方案可以在元素创建后删除 pytype 及其命名空间,使用 deannotate() 函数(例如 When using lxml, can the XML be rendered without namespace attributes? 、 Remove "xmlns:py..." with lxml.objectify )。没有任何解决方案可以从一开始就创建没有 pytype 及其 namespace 的元素。关于如何做到这一点有什么想法吗?
请您参考如下方法:
在lxml.objectify中,有两种元素:树元素,由Element工厂创建,数据元素,由DataElement
<创建< code>
工厂或特定数据类,例如
StringElement、
IntElement(有关更多信息,请参阅
here)。一种解决方案可能是清空特定元素的命名空间和
_pytype 参数,方法是将其分配给空字符串,并且从不使用文字的直接赋值。要从文字创建元素,您必须使用 DataElement 工厂。请注意,如果您有任何特定的命名空间,则必须将您的命名空间映射分配给 nsmap 参数,而不是空字符串。但是有一个问题。如果你想创建一个树元素,将
nsmap 和
_pytype 设置为空字符串, namespace 和 pytype 不会被删除。我不知道为什么。因此,此解决方案仅适用于数据元素。
创建<>
这就是您尝试构建的树的代码:
root = objectify.Element('root', nsmap='', _pytype='')
# sub elements do not need nsmap or _pytype to be emptied
child = objectify.SubElement(root, 'child')
root.child = objectify.DataElement('value', nsmap='', _pytype='')
path = objectify.ObjectPath('root.vader.son')
path.setattr(root, objectify.DataElement('Luke', nsmap='', _pytype=''))
print(etree.tostring(root, pretty_print=True).decode('utf-8'))
哪些输出:
<root xmlns:py="http://codespeak.net/lxml/objectify/pytype" py:pytype="">
<child>value</child>
<vader>
<son>Luke</son>
</vader>
</root>
不是我们想要的!
解决方案在于变通方法,使用 ElementMaker 工厂。
# Create your ElementMaker factory, without annotations.
E = objectify.ElementMaker(annotate=False)
# If you have any namespaces you want to use, assign them to the nsmap
# parameter and assign the default namespace to the namespace parameter.
# E = objectify.ElementMaker(annotate=False, namespace=namespace, nsmap=nsmap)
root = E.root()
print(etree.tostring(root, pretty_print=True))
这个输出:
<root/>
树元素引入的命名空间和pytype问题已经解决。现在我们可以分配子元素或数据元素:
objectify.SubElement(root, 'child')
root.child = objectify.DataElement('value', nsmap='', _pytype='')
print(etree.tostring(root, pretty_print=True).decode('utf-8'))
哪些输出:
<root>
<child>value</child>
</root>
一个使用 setattr() 方法的例子是:
root = E.root()
path = objectify.ObjectPath('root.vader.son')
path.setattr(root, objectify.DataElement('Luke', nsmap='', _pytype=''))
# mysteriously, the below line works the same as the above line:
# path.setattr(root, E.whatevername('Luke'))
print(etree.tostring(root, pretty_print=True).decode('utf-8'))
其输出是:
<root>
<vader>
<son>Luke</son>
</vader>
</root>
