【发布时间】:2016-02-08 20:32:40
【问题描述】:
我想从xml.etree.ElementTree 向Element 的某个实例添加一个属性,以便将numpy.ndarray 等卫星数据存储到某些树的节点中。这可能吗,因为该对象没有公开__dict__ 和__slot__?
我知道我可以使用get/set方法来填充节点属性,但是如果它包含str以外的其他对象,则无法序列化为XML。
Traceback (most recent call last):
File "latexer.py", line 576, in <module>
main(sys.argv)
File "latexer.py", line 565, in main
r.dump(**k)
File "latexer.py", line 406, in dump
code = self.__fileFormats[ufformat]()
File "latexer.py", line 416, in getXML
rawstr = ET.tostring(self._document, encoding='utf-8', method='xml')
File "C:\Python33\lib\xml\etree\ElementTree.py", line 1171, in tostring
ElementTree(element).write(stream, encoding, method=method)
File "C:\Python33\lib\xml\etree\ElementTree.py", line 828, in write
serialize(write, self._root, qnames, namespaces)
File "C:\Python33\lib\xml\etree\ElementTree.py", line 990, in _serialize_xml
_serialize_xml(write, e, qnames, None)
File "C:\Python33\lib\xml\etree\ElementTree.py", line 990, in _serialize_xml
_serialize_xml(write, e, qnames, None)
File "C:\Python33\lib\xml\etree\ElementTree.py", line 990, in _serialize_xml
_serialize_xml(write, e, qnames, None)
File "C:\Python33\lib\xml\etree\ElementTree.py", line 983, in _serialize_xml
v = _escape_attrib(v)
File "C:\Python33\lib\xml\etree\ElementTree.py", line 1139, in _escape_attrib
_raise_serialization_error(text)
File "C:\Python33\lib\xml\etree\ElementTree.py", line 1105, in _raise_serialization_error
"cannot serialize %r (type %s)" % (text, type(text).__name__)
TypeError: cannot serialize 1 (type int)
有没有办法添加bool或integer等属性,然后将其序列化为XML?
【问题讨论】:
-
你试过
https://docs.python.org/2/library/xml.etree.elementtree.html#xml.etree.ElementTree.Element属性attrib吗? -
@AnthonyKong 谢谢你强调它,我想不知何故(也许是 XML 的阴暗面)它可能只是存储在这本字典中的文本。
-
@AnthonyKong 我已经编辑了我的问题,因为我仍然卡在某个点上。