如果childnode的childnode包含特定值,则删除XML节点

时间:2015-10-16 12:39:54

标签: python xml dom

我需要过滤某个值的XML文件,如果该节点包含此值,则应删除该节点。

<?xml version="1.0" encoding="utf-8" ?>
<ogr:FeatureCollection
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://ogr.maptools.org/ TZwards.xsd"
 xmlns:ogr="http://ogr.maptools.org/"
 xmlns:gml="http://www.opengis.net/gml">
    <gml:boundedBy></gml:boundedBy>                   
    <gml:featureMember>
        <ogr:TZwards fid="F0">
            <ogr:Region_Nam>TARGET</ogr:Region_Nam>
            <ogr:District_N>Kondoa</ogr:District_N>
            <ogr:Ward_Name>Bumbuta</ogr:Ward_Name>
        </ogr:TZwards>
    </gml:featureMember>
    <gml:featureMember>
        <ogr:TZwards fid="F1">
            <ogr:Region_Nam>REMOVE</ogr:Region_Nam>
            <ogr:District_N>Kondoa</ogr:District_N>
            <ogr:Ward_Name>Pahi</ogr:Ward_Name>
         </ogr:TZwards>
    </gml:featureMember>
</ogr:FeatureCollection>

如果<gml:featureMember>包含<ogr:Region_Nam>,则Python脚本应保留TARGET节点,并删除所有其他节点。

from xml.dom import minidom
import xml.etree.ElementTree as ET

tree = ET.parse('input.xml').getroot()

removeList = list()
for child in tree.iter('gml:featureMember'):
    if child.tag == 'ogr:TZwards':
        name = child.find('ogr:Region_Nam').text
        if (name == 'TARGET'):
            removeList.append(child)

for tag in removeList:
    parent = tree.find('ogr:TZwards')
    parent.remove(tag)

out = ET.ElementTree(tree)
out.write(outputfilepath)

期望的输出:

<?xml version="1.0" encoding="utf-8" ?>
<ogr:FeatureCollection>
    <gml:boundedBy></gml:boundedBy>                   
    <gml:featureMember>
        <ogr:TZwards fid="F0">
            <ogr:Region_Nam>TARGET</ogr:Region_Nam>
            <ogr:District_N>Kondoa</ogr:District_N>
            <ogr:Ward_Name>Bumbuta</ogr:Ward_Name>
        </ogr:TZwards>
    </gml:featureMember>
</ogr:FeatureCollection>

我的输出仍包含所有节点..

1 个答案:

答案 0 :(得分:1)

您需要在python代码中声明名称空间:

from xml.dom import minidom
import xml.etree.ElementTree as ET

tree = ET.parse('/tmp/input.xml').getroot()
namespaces = {'gml': 'http://www.opengis.net/gml', 'ogr':'http://ogr.maptools.org/'}
for child in tree.findall('gml:featureMember', namespaces=namespaces):
    if len(child.find('ogr:TZwards', namespaces=namespaces)):
        name = child.find('ogr:TZwards', namespaces=namespaces).find('ogr:Region_Nam', namespaces=namespaces).text
        if name != 'TARGET':
            tree.remove(child)

out = ET.ElementTree(tree)
out.write("/tmp/out.xml")