使用python编辑XML

时间:2017-01-31 20:22:25

标签: python xml minidom

我正在尝试使用python中的xml.dom.minidom编辑此XML,但我不确定如何获取我需要更改的值。

我通过doc.getElementsByTagName()

访问此块
           <SVCExtension xsi:type="typens:SVCExtension">
              <Enabled>true</Enabled>
              <Info xsi:type="typens:PropertySet">
                 <PropertyArray xsi:type="typens:ArrayOfPropertySetProperty">
                    <PropertySetProperty xsi:type="typens:PropertySetProperty">
                       <Key>WebEnabled</Key>
                       <Value xsi:type="xs:string">true</Value>
                    </PropertySetProperty>
                    <PropertySetProperty xsi:type="typens:PropertySetProperty">
                       <Key>WebCapabilities</Key>
                       <Value xsi:type="xs:string">GetCapabilities,GetMap,GetFeatureInfo,GetStyles,GetLegendGraphic,GetSchemaExtension</Value>
                    </PropertySetProperty>
                 </PropertyArray>
              </Info>
              <Props xsi:type="typens:PropertySet">
                 <PropertyArray xsi:type="typens:ArrayOfPropertySetProperty">
                    <PropertySetProperty xsi:type="typens:PropertySetProperty">
                       <Key>onlineResource</Key>
                       <Value xsi:type="xs:string">*censored url*</Value>
                    </PropertySetProperty>
                    <PropertySetProperty xsi:type="typens:PropertySetProperty">
                       <Key>customGetCapabilities</Key>
                       <Value xsi:type="xs:string">false</Value>
                    </PropertySetProperty>
                    <PropertySetProperty xsi:type="typens:PropertySetProperty">
                       <Key>pathToCustomGetCapabilitiesFiles</Key>
                       <Value xsi:type="xs:string" />
                    </PropertySetProperty>
                    <PropertySetProperty xsi:type="typens:PropertySetProperty">
                       <Key>pathToCustomSLDFile</Key>
                       <Value xsi:type="xs:string" />
                    </PropertySetProperty>
                    <PropertySetProperty xsi:type="typens:PropertySetProperty">
                       <Key>inheritLayerNames</Key>
                       <Value xsi:type="xs:string">false</Value>
                    </PropertySetProperty>
                    <PropertySetProperty xsi:type="typens:PropertySetProperty">
                       <Key>name</Key>
                       <Value xsi:type="xs:string">WMS</Value>
                    </PropertySetProperty>
                    <PropertySetProperty xsi:type="typens:PropertySetProperty">
                       <Key>title</Key>
                       <Value xsi:type="xs:string" />
                    </PropertySetProperty>
                    <PropertySetProperty xsi:type="typens:PropertySetProperty">
                       <Key>abstract</Key>
                       <Value xsi:type="xs:string" />
                    </PropertySetProperty>
                    <PropertySetProperty xsi:type="typens:PropertySetProperty">
                       <Key>keyword</Key>
                       <Value xsi:type="xs:string" />
                    </PropertySetProperty>
                    <PropertySetProperty xsi:type="typens:PropertySetProperty">
                       <Key>contactPerson</Key>
                       <Value xsi:type="xs:string" />
                    </PropertySetProperty>
                    <PropertySetProperty xsi:type="typens:PropertySetProperty">
                       <Key>contactPosition</Key>
                       <Value xsi:type="xs:string" />
                    </PropertySetProperty>
                    <PropertySetProperty xsi:type="typens:PropertySetProperty">
                       <Key>contactOrganization</Key>
                       <Value xsi:type="xs:string" />
                    </PropertySetProperty>
                    <PropertySetProperty xsi:type="typens:PropertySetProperty">
                       <Key>address</Key>
                       <Value xsi:type="xs:string" />
                    </PropertySetProperty>
                    <PropertySetProperty xsi:type="typens:PropertySetProperty">
                       <Key>addressType</Key>
                       <Value xsi:type="xs:string" />
                    </PropertySetProperty>
                    <PropertySetProperty xsi:type="typens:PropertySetProperty">
                       <Key>city</Key>
                       <Value xsi:type="xs:string" />
                    </PropertySetProperty>
                    <PropertySetProperty xsi:type="typens:PropertySetProperty">
                       <Key>stateOrProvince</Key>
                       <Value xsi:type="xs:string" />
                    </PropertySetProperty>
                    <PropertySetProperty xsi:type="typens:PropertySetProperty">
                       <Key>postCode</Key>
                       <Value xsi:type="xs:string" />
                    </PropertySetProperty>
                    <PropertySetProperty xsi:type="typens:PropertySetProperty">
                       <Key>country</Key>
                       <Value xsi:type="xs:string" />
                    </PropertySetProperty>
                    <PropertySetProperty xsi:type="typens:PropertySetProperty">
                       <Key>contactVoiceTelephone</Key>
                       <Value xsi:type="xs:string" />
                    </PropertySetProperty>
                    <PropertySetProperty xsi:type="typens:PropertySetProperty">
                       <Key>contactFacsimileTelephone</Key>
                       <Value xsi:type="xs:string" />
                    </PropertySetProperty>
                    <PropertySetProperty xsi:type="typens:PropertySetProperty">
                       <Key>contactElectronicMailAddress</Key>
                       <Value xsi:type="xs:string" />
                    </PropertySetProperty>
                    <PropertySetProperty xsi:type="typens:PropertySetProperty">
                       <Key>fees</Key>
                       <Value xsi:type="xs:string" />
                    </PropertySetProperty>
                    <PropertySetProperty xsi:type="typens:PropertySetProperty">
                       <Key>accessConstraints</Key>
                       <Value xsi:type="xs:string" />
                    </PropertySetProperty>
                 </PropertyArray>
              </Props>
              <TypeName>WMSServer</TypeName>
           </SVCExtension>

现在,在这里,我想改变&#34; customGetCapabilities&#34;价值为&#34;真&#34;并在&#34; pathToCustomGetCapabilitiesFiles&#34;中指定一个网址。

有人可以教我如何解析这些价值观吗?谢谢

1 个答案:

答案 0 :(得分:0)

以下是使用minidom执行此操作的一种方法。

props = doc.getElementsByTagName('PropertySetProperty')
for prop in props:
    if prop.hasChildNodes():
        key = prop.getElementsByTagName('Key')[0]
        if key.firstChild.nodeValue == 'customGetCapabilities':
            value = key.parentNode.getElementsByTagName("Value")[0]
            value.firstChild.nodeValue = 'true'

        if key.firstChild.nodeValue == 'pathToCustomGetCapabilitiesFiles':
            path_url = key.parentNode.getElementsByTagName('Value')[0]
            path_url.appendChild(doc.createTextNode('http://someurl/'))
            break