简体   繁体   中英

Replacing the value of some parameters in the XML files in python

I have a script that would pick a parameter "0.A99EBD7X" (in this case) from a list and open an XML file that contains the parameter. I need to keep the first mention of the parameter and replace the other occurrences with "removed".

The XML format is the following:

<ANDConfig:AlphanumericDisplayConfiguration xmlns:ANDConfig="xxxxxn" xSystemID="NoId" Version="1.0" Name="NACL025A-Ver-1_1" Description="AOCS Submode Set" Sortable="false" ColumnsReorganisable="false" kindOfDisplay="MAT">
<Display xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ANDConfig:MatrixDisplay">
<Column Name="Parameter" Tooltip="The short name of the parameter" Width="103" Field="NAME"/>
<Column Name="Description" Tooltip="The description for this parameter" Width="251" Field="DESCRIPTION"/>
<Column Name="Value" Tooltip="The current parameter value" Width="129" Field="VALUE"/>
<Column Name="Expected" Tooltip="The expected parameter value" Width="129" Field="VALUE"/>
<Column Name="Unit" Tooltip="The unit of the parameter" Width="42" Field="UNIT"/>
<Column Name="Validity" Width="101" Field="VALIDITY"/>
<Column Name="OOL" Field="ALARM"/>
<Column Name="Sample Time" Tooltip="The time of the latest sample" Width="134" Field="TIMESTAMP"/>
...
...
</Row>
<Row>
<MatrixElement xsi:type="ANDConfig:MatrixParameter" ParameterId="0.A99EBD7X" Field="NAME"/>
<MatrixElement xsi:type="ANDConfig:MatrixParameter" ParameterId="0.A99EBD7X" Field="DESCRIPTION"/>
<MatrixElement xsi:type="ANDConfig:MatrixParameter" ParameterId="0.A99EBD7X"/>
<MatrixElement xsi:type="ANDConfig:MatrixLabel" Text=" == NONE "/>
<MatrixElement xsi:type="ANDConfig:MatrixParameter" ParameterId="0.A99EBD7X" Field="UNIT"/>
<MatrixElement xsi:type="ANDConfig:MatrixParameter" ParameterId="0.A99EBD7X" Field="VALIDITY"/>
<MatrixElement xsi:type="ANDConfig:MatrixParameter" ParameterId="0.A99EBD7X" Field="ALARM"/>
<MatrixElement xsi:type="ANDConfig:MatrixParameter" ParameterId="0.A99EBD7X" Field="TIMESTAMP"/>

....
....

There are multiple row elements also to define the font/colour/size and cell design of the output AlphaNumericDisplay used by the tool I am using, but also rows elements with many other parameters.

The final content of the Row element should look like:

<Row>
<MatrixElement xsi:type="ANDConfig:MatrixParameter" ParameterId="0.A99EBD7X" Field="NAME"/>
<MatrixElement xsi:type="ANDConfig:MatrixParameter" ParameterId="Removed" Field="DESCRIPTION"/>
</Row>

I tried to work with ElementTree but I am not used to working with xml files.

You can use BeautifulSoup for identifying the tags and its elements using xml parser

sample.xml This is the sample xml

<Row>
  <MatrixElement xsi:type="ANDConfig:MatrixParameter" ParameterId="0.A99EBD7X" Field="NAME"/>
  <MatrixElement xsi:type="ANDConfig:MatrixParameter" ParameterId="0.A99EBD7X" Field="DESCRIPTION"/>
  <MatrixElement xsi:type="ANDConfig:MatrixParameter" ParameterId="0.A99EBD7X"/>
  <MatrixElement xsi:type="ANDConfig:MatrixLabel" Text=" == NONE "/>
  <MatrixElement xsi:type="ANDConfig:MatrixParameter" ParameterId="0.A99EBD7X" Field="UNIT"/>
  <MatrixElement xsi:type="ANDConfig:MatrixParameter" ParameterId="0.A99EBD7X" Field="VALIDITY"/>
  <MatrixElement xsi:type="ANDConfig:MatrixParameter" ParameterId="0.A99EBD7X" Field="ALARM"/>
  <MatrixElement xsi:type="ANDConfig:MatrixParameter" ParameterId="0.A99EBD7X" Field="TIMESTAMP"/>
</Row>

main.py

from bs4 import BeautifulSoup as bs

fd= open('sample.xml','r')
xml_file = fd.read()
sp = bs(xml_file,'xml')

count = 0
for tag in sp.find_all('MatrixElement'): # find all elements of MatrixElement
  
  if(tag.has_attr('ParameterId')):
    if(count!=0):
      tag['ParameterId'] = "removed"
    else:
      count = count + 1
      
f_output= open("sample.xml", "wb")
f_output.write(sp.prettify('utf-8'))

Updated output in sample.xml

    <?xml version="1.0" encoding="utf-8"?> 
    <Row>
      <MatrixElement Field="NAME" ParameterId="0.A99EBD7X" type="ANDConfig:MatrixParameter"/>
      <MatrixElement Field="DESCRIPTION" ParameterId="removed" type="ANDConfig:MatrixParameter"/>
      <MatrixElement ParameterId="removed" type="ANDConfig:MatrixParameter"/>
      <MatrixElement Text=" == NONE " type="ANDConfig:MatrixLabel"/>
      <MatrixElement Field="UNIT" ParameterId="removed" type="ANDConfig:MatrixParameter"/>
      <MatrixElement Field="VALIDITY" ParameterId="removed" type="ANDConfig:MatrixParameter"/>
      <MatrixElement Field="ALARM" ParameterId="removed" type="ANDConfig:MatrixParameter"/>
      <MatrixElement Field="TIMESTAMP" ParameterId="removed" type="ANDConfig:MatrixParameter"/>
 </Row>

Note

  • Parse the xml file and Read the content of the file
  • Find all the MatrixElement tags and Check for ParameterId attribute
  • If its the first tag don't change the value, else change the value
  • Write back the updated values to the file.

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