简体   繁体   English

使用 python 编辑 XML

[英]Editing XML with python

I am trying to edit this XML using xml.dom.minidom in python, but am not sure how to get to the values I need to change.我正在尝试在 python 中使用 xml.dom.minidom 编辑此 XML,但不确定如何获取我需要更改的值。

I access this chunk by doc.getElementsByTagName()我通过 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>

Now, within here I would like to change "customGetCapabilities" value to "true" and specify a url in "pathToCustomGetCapabilitiesFiles".现在,在这里,我想将“customGetCapabilities”值更改为“true”并在“pathToCustomGetCapabilitiesFiles”中指定一个 url。

Can someone teach me how to parse through to those values please?有人可以教我如何解析这些值吗? Thank you谢谢

The following is one way you could do this with minidom.以下是您可以使用 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM