使用 python 编辑 XML



我正在尝试使用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>

现在,我想在这里将"customGetCapabilities"值更改为"true",并在"pathToCustomGetCapabilitiesFiles"中指定一个URL。

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

以下是您可以使用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

相关内容

  • 没有找到相关文章

最新更新