I have the following structure
<root>
<data>
<config>
CONFIGURATION
<config>
</data>
</root>
Run Code Online (Sandbox Code Playgroud)
使用 Python 的 ElementTree 模块,我想添加一个父元素来<config>标记为
<root>
<data>
<type>
<config>
CONFIGURATION
<config>
</type>
</data>
</root>
Run Code Online (Sandbox Code Playgroud)
此外,xml 文件可能在其他地方有其他配置标签,但我只对出现在数据标签下的那些感兴趣。
当我尝试在Python中导入elementtree时,我可以得到这个错误吗?
我没有得到任何模块:
ImportError: No module named elementtree.ElementTree
Run Code Online (Sandbox Code Playgroud) 我有类似结构的 xml:
<terms>
<entry ID="1">
<language ID="1">en</language>
<term>user</term>
<state>text</state>
<use>text</use>
<definition ID="1">text</definition>
<subdefinition ID="1">text</subdefinition>
<definition-source>text</definition-source>
<source ID="1">text</source>
<circle>text</circle>
</entry>
Run Code Online (Sandbox Code Playgroud)
在这种情况下,父元素和子元素都包含属性 ID。有没有办法,如何从包含属性 ID 的树中查找所有元素并将值更改为 0 或删除它?
我试图使用 XPath 来做到这一点,但是当存在很深的层次结构并且任何元素都可以具有此属性时,这很困难。另一种方法是将其作为字符串处理,但是有没有办法在 ElementTree 中做到这一点?
我有一个包含<p>元素和尾巴的元素。
例如:
<p>Content that I want to keep</p>content that I want removed
我注意到需要删除的内容在尾部,我认为有一种简单的方法可以使用 etree 从元素中删除尾部。
我怎么能做到这一点?