lxml[.objectify] documentElement 标签名称
我正在接收 XML 格式的数据包,每个数据包都有一个特定的 documentRoot 标记,并且我想根据根标记名称委托专门的方法来处理这些数据包。这与 xml.dom.minidom 一起工作,如下所示:
dom = minidom.parseString(the_data)
root = dom.documentElement
deleg = getattr(self,'elem_' + str(root.tagName))
deleg(dom)
但是,我想通过使用更Pythonic的 lxml.objectify 来简化事情(在代码的其他部分,而不是这里)。
问题是我不知道如何使用 lxml 获取“root.tagName”,最好严格使用 lxml.objectify。有什么想法吗?
I'm receiving data packets in XML format, each with a specific documentRoot tag, and I'd like to delegate specialized methods to take care of those packets, based on the root tag name. This worked with xml.dom.minidom, something like this:
dom = minidom.parseString(the_data)
root = dom.documentElement
deleg = getattr(self,'elem_' + str(root.tagName))
deleg(dom)
However, I want to simplify the things (in other parts of the code, not here) by using the more pythonic lxml.objectify.
The problem is I don't know how to get "root.tagName" with lxml, preferably strictly lxml.objectify. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 lxml 文档 和 dir()built_in 的帮助下,我成功地生成了这个:
看起来你需要类似的东西
With the help of the lxml docs and the dir() built_in, I managed to produce this:
Looks like you need something like
FWIW 在 Amara Bindery 中,您可以执行以下操作:
并且您还可以获得 Pythonic API,例如: < code>doc.html.head.title = u"更改 HTML 文档标题"
FWIW in Amara Bindery you can do something like:
And you get a Pythonic API as well, e.g.:
doc.html.head.title = u"Change HTML document title"