1

I have an xml file, which has a particular set of child lines which should be deleted when the python code is run.

Below shown lines are my xml code.

<?xml version="1.0" encoding="utf-8" ?>
<visualization protocolVersion="10.4.0.0">
  <globalSection/>
  <coreObjectDefinition type="displayDefinition">
    <version type="version" value="10.4.0.0"/>
    <width>1920</width>
    <height>810</height>
    <referenceCheck>2</referenceCheck>
    <defaultBgColor type="colorSet" r="255" g="255" b="255"/>
    <defaultFgColor type="colorSet" r="0" g="0" b="0"/>
    <defaultFont type="font" name="Tahoma" size="16" underline="false" strikethrough="false"/>
    <defaultStroke type="stroke" width="1.0"/>
    <grid type="grid" gridVisible="true" snappingActive="true" verticalSnapInterval="8" horizontalSnapInterval="8" onTop="false">
      <color type="colorSet" r="0" g="0" b="0"/>
    </grid>
    <revisionHistory type="revisionHistory">
      <revision type="revision" who="ADMIN" when="2020.05.03 09:46:15.566 CEST" what="Created" where="CPC-A0668-4138"/>
    </revisionHistory>
    <blinkDelay>500</blinkDelay>
    <mousePassThrough>false</mousePassThrough>
    <visibilityGroup type="componentData">
      <htmlId>2</htmlId>
      <name>Overview</name>
      <description>Always shown</description>
      <minimumZoomEnabled>true</minimumZoomEnabled>
      <minimumZoomFactor>10.0</minimumZoomFactor>
    </visibilityGroup>
    <visibilityGroup type="componentData">
      <htmlId>3</htmlId>
      <name>Rough</name>
      <description>Shown when viewing viewing a large area</description>
      <minimumZoomEnabled>true</minimumZoomEnabled>
      <minimumZoomFactor>25.0</minimumZoomFactor>
    </visibilityGroup>
    <visibilityGroup type="componentData">
      <htmlId>4</htmlId>
      <name>Standard</name>
      <description>Shown when using the default view setting</description>
      <minimumZoomEnabled>true</minimumZoomEnabled>
      <minimumZoomFactor>100.0</minimumZoomFactor>
    </visibilityGroup>
    <visibilityGroup type="componentData">
      <htmlId>5</htmlId>
      <name>Detail</name>
      <description>Shown only when viewing a small area</description>
      <minimumZoomEnabled>true</minimumZoomEnabled>
      <minimumZoomFactor>400.0</minimumZoomFactor>
    </visibilityGroup>
    <visibilityGroup type="componentData">
      <htmlId>6</htmlId>
      <name>Intricacies</name>
      <description>Shown only when viewing a very small area</description>
      <minimumZoomEnabled>true</minimumZoomEnabled>
      <minimumZoomFactor>1000.0</minimumZoomFactor>
    </visibilityGroup>
    <visualizationLayer type="componentData">
      <htmlId>1</htmlId>
      <name>Layer1</name>
    </visualizationLayer>
    <componentCountHint>1</componentCountHint>
    <ellipse type="componentData" x="851.99896" y="300.00006" top="92.000046" bottom="91.99985" left="99.99896" right="100.001526">
      <htmlId>7</htmlId>
      <stroke type="stroke" width="1.0"/>
      <fillPaint type="paint">
        <paint type="colorSet" r="255" g="255" b="255"/>
      </fillPaint>
      **<data type="data">
        <action type="actionConnectTo">
          <property type="property" name="ellipse.visible"/>
          <filter type="filter">
            <value>0.0</value>
          </filter>
          <connection type="connection">
            <direction>1</direction>
            <itemName>AOG.Templates.Alarm</itemName>
            <itemId>2.1.3.0.0.2.1.8</itemId>
          </connection>
        </action>
      </data>**
    </ellipse>
  </coreObjectDefinition>
</visualization>

I want only the below part to be deleted from the entire xml file.

    <data type="data">
       <action type="actionConnectTo">
         <property type="property" name="ellipse.visible"/>
         <filter type="filter">
           <value>0.0</value>
         </filter>
         <connection type="connection">
           <direction>1</direction>
           <itemName>AOG.Templates.Alarm</itemName>
           <itemId>2.1.3.0.0.2.1.8</itemId>
         </connection>
       </action>
     </data>

The below mentioned python code only removes the child section and not the sub child.. Kindly help me out on this

    from xml.etree import ElementTree
    root = ElementTree.parse("test1.xml").getroot()
    b = root.getchildren()[0]

    root.remove(b)
    ElementTree.dump(root)

2 Answers 2

1

Try this.

from simplified_scrapy import SimplifiedDoc,utils,req
html = '''Your xml'''
doc = SimplifiedDoc(html)
data = doc.select('data@type=data') # Get the element
data.repleaceSelf("") # Remove it
print(doc.html) # This is what you want
Sign up to request clarification or add additional context in comments.

Comments

1

Unfortunately, you can't access sub-child of an element using ElementTree. Each node only has "pointers" to the direct children of it. So, in order to access the <data/> node and remove it, you should refer to it from its direct parent node.

I'd do it in this way:

for d in root.findall('coreObjectDefinition'):
    for e in d.findall('ellipse'):
        for f in e.findall('data'):
            e.remove(f)

This library has syntax that allows you to search a tree recursively, so you're able to find the element with root.findall('.//data'). So a shorter version of the above code would be:

for d in root.findall('.//ellipse'):
    for e in d.findall('data'):
        d.remove(e)

1 Comment

"you can't access sub-child of an element using ElementTree". That is not true. You can use elem.find("child/subchild"), for example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.