简体   繁体   中英

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.

I access this chunk by 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".

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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