使用python创建xml文件的子集



我希望使用python创建一个xml文件的子集进行测试。在下面的示例xml中,我希望提取项目日期为"2015年1月"的所有记录,并仅使用这些记录创建一个新的xml文件。任何帮助都将不胜感激!

<config>
  <item date="January 2015">
    <mode>1</mode>
    <current>1</current>
  </item>
  <item date="February 2016">
    <mode>9</mode>
    <current>100</current>
  </item>
  <item date="January 2015">
    <mode>9</mode>
    <current>100</current>
  </item>
</config> 

您可以使用lxml和XPath表达式:

from lxml import etree
tree = etree.parse(open('data.xml'))
unwanted = tree.xpath("//item[not(@date='January 2015')]")
for node in unwanted:
    node.getparent().remove(node)
with open('filtered.xml', 'w') as outfile:
    outfile.write(etree.tostring(tree, pretty_print=True))
  • 表达式//item将匹配文档中的所有<item />节点
  • CCD_ 4将仅将选择限制为具有等于January 2015的属性date的CCD_
  • 它周围的not()反转该条件,以便使<item />节点从树中删除
  • 然后,通过从其父节点中删除节点,从树中过滤这些节点

有关XPath表达式的更多详细信息,请参阅本XPath教程。


filtered.xml:中的输出

<config>
  <item date="January 2015">
    <mode>1</mode>
    <current>1</current>
  </item>
  <item date="January 2015">
    <mode>9</mode>
    <current>100</current>
  </item>
</config>

最新更新