Python 使用 ElementTree 将新项目插入 XML sub 子元素的更简单方法
Python easier way to insert new items into XML sub sub elements using ElementTree
我正在编辑一个配置文件并想将新的子元素添加到结构中。我遇到的问题是,在配置文件中,子元素具有相同的名称,而且我还没有找到使用 .find 命令查找插入位置索引的方法。配置文件的示例在这里:
<?xml version="1.0" encoding="utf-8"?>
<ConfigurationFile>
<Configuration ItemName="Parent1">
<Category ItemName="Parent2">
<Category ItemName="Parent3">
<Category ItemName="ChildLevelToAdd">
<Option ItemName="x" ValueType="6">5</Option>
<Option ItemName="z" ValueType="6">0</Option>
<Category ItemName="ChildCategory">
<Option ItemName="a" ValueType="1"></Option>
<Option ItemName="b" ValueType="2"></Option>
<Option ItemName="c" ValueType="3">
</Option>
</Category>
</Category>
</Category>
</Category>
我正在尝试添加一个新的 "ChildToAdd" 类别以及属于该类别的所有内容。
我试过用下面的代码来索引,但是好像只能定位到第一个子元素:
a = root.find('Configuration') #It is only possible to find sub-elements of the root
b = root.find('Category') #These generate a none type
我发现插入新类别元素的最佳方法是使用:
root[0][0][0].insert(0,ET.Element("Category", ItemName = "NewChild"))
然后当我想向这个新创建的类别添加选项时,我使用:
root[0][0][0][0].insert(0,ET.Element("NewOption", ItemName = "NewVal", ValueType ="NewType"))
这似乎是一种非常不整洁的方式。我想知道是否有任何更简洁的方法来查找和指定新子元素(类别)及其未来子元素(选项)的插入点?
使用 .//element_name
形式的 XPath 表达式在上下文节点(本例中为根元素)内的任何位置查找元素。例如,以下应该 return <Category>
元素的第一个实例,在 root
中的任何位置:
root.find('.//Category')
您还可以使用 XPath 通过其属性值查找元素。例如,下面应该 return <Category>
元素 where ItemName
属性值等于 "Parent3" :
root.find('.//Category[@ItemName="Parent3"]')
ElementTree 支持 here.
中记录的一些其他 XPath 语法
我还建议将您的新元素存储在一个变量中,这样您以后就可以向其中添加子元素,而无需再次查询 XML :
c = ET.Element("Category", ItemName = "NewChild")
c.insert(0,ET.Element("NewOption", ItemName = "NewVal", ValueType ="NewType"))
p = root.find('.//Category[@ItemName="Parent3"]')
p.insert(0, c)
我正在编辑一个配置文件并想将新的子元素添加到结构中。我遇到的问题是,在配置文件中,子元素具有相同的名称,而且我还没有找到使用 .find 命令查找插入位置索引的方法。配置文件的示例在这里:
<?xml version="1.0" encoding="utf-8"?>
<ConfigurationFile>
<Configuration ItemName="Parent1">
<Category ItemName="Parent2">
<Category ItemName="Parent3">
<Category ItemName="ChildLevelToAdd">
<Option ItemName="x" ValueType="6">5</Option>
<Option ItemName="z" ValueType="6">0</Option>
<Category ItemName="ChildCategory">
<Option ItemName="a" ValueType="1"></Option>
<Option ItemName="b" ValueType="2"></Option>
<Option ItemName="c" ValueType="3">
</Option>
</Category>
</Category>
</Category>
</Category>
我正在尝试添加一个新的 "ChildToAdd" 类别以及属于该类别的所有内容。
我试过用下面的代码来索引,但是好像只能定位到第一个子元素:
a = root.find('Configuration') #It is only possible to find sub-elements of the root
b = root.find('Category') #These generate a none type
我发现插入新类别元素的最佳方法是使用:
root[0][0][0].insert(0,ET.Element("Category", ItemName = "NewChild"))
然后当我想向这个新创建的类别添加选项时,我使用:
root[0][0][0][0].insert(0,ET.Element("NewOption", ItemName = "NewVal", ValueType ="NewType"))
这似乎是一种非常不整洁的方式。我想知道是否有任何更简洁的方法来查找和指定新子元素(类别)及其未来子元素(选项)的插入点?
使用 .//element_name
形式的 XPath 表达式在上下文节点(本例中为根元素)内的任何位置查找元素。例如,以下应该 return <Category>
元素的第一个实例,在 root
中的任何位置:
root.find('.//Category')
您还可以使用 XPath 通过其属性值查找元素。例如,下面应该 return <Category>
元素 where ItemName
属性值等于 "Parent3" :
root.find('.//Category[@ItemName="Parent3"]')
ElementTree 支持 here.
中记录的一些其他 XPath 语法我还建议将您的新元素存储在一个变量中,这样您以后就可以向其中添加子元素,而无需再次查询 XML :
c = ET.Element("Category", ItemName = "NewChild")
c.insert(0,ET.Element("NewOption", ItemName = "NewVal", ValueType ="NewType"))
p = root.find('.//Category[@ItemName="Parent3"]')
p.insert(0, c)