I'm trying to use Element tree to locate an element of interest from an XML and remove the entire group (i.e. the parent) from the XML.
import xml.etree.ElementTree as ET
from lxml import etree
copasiML_str= IA.read_copasiML_as_string(model_file) # Reads XML as string
copasiML=ET.fromstring(copasiML_str) # parse XML to etree
for i in copasiML.findall(".//*[@name='ObjectCN']"): # locate element
if '[v18]' in i.attrib['value']: #search for 'v18'
if 'Parameter=V' in i.attrib['value']: #search for 'Parameter=V'
print i.attrib['value'] #Element identified
parent = i.getparent() #gets the parent of identified
copasiML.remove(parent) # This does not work
This code identifies the element and gets the parent of the element I want to remove. Then it gives me an error when I try to remove the element:
ValueError: Element is not a child of this node.
The XML in question is fairly complicated. Here is a snippet:
<ParameterGroup name="FitItem">
<ParameterGroup name="Affected Cross Validation Experiments">
</ParameterGroup>
<ParameterGroup name="Affected Experiments">
<Parameter name="Experiment Key" type="key" value="Experiment_1"/>
<Parameter name="Experiment Key" type="key" value="Experiment_2"/>
<Parameter name="Experiment Key" type="key" value="Experiment_4"/>
</ParameterGroup>
<Parameter name="LowerBound" type="cn" value="1e-06"/>
<Parameter name="ObjectCN" type="cn" value="CN=Root,Model=NoName,Vector=Reactions[V18],ParameterGroup=Parameters,Parameter=V,Reference=Value"/>
<Parameter name="StartValue" type="float" value="0.1852208634119804"/>
<Parameter name="UpperBound" type="cn" value="100"/>
</ParameterGroup>
There are many 'FitItem' parameter groups. I'm trying to locate the one with '[V18]' and 'Parameter=V' and delete the entire FitItem. Would anybody know how to do this?
Thanks