The following is the part of the XML code source:
<FacilitySite>
.
.
.
<Program>
.
.
.
<ProgramProfileElectronicAddress>
<ElectronicAddressText>http://example.com</ElectronicAddressText>
<ElectronicAddressTypeName>URL</ElectronicAddressTypeName> </ProgramProfileElectronicAddress>
</Program>
</FacilitySite>
<FacilitySite>
.
.
.
<Program>
.
.
.
<ProgramProfileElectronicAddress>
<ElectronicAddressText>http://example.com</ElectronicAddressText>
<ElectronicAddressTypeName>URL</ElectronicAddressTypeName> </ProgramProfileElectronicAddress>
</Program>
<Program>
.
.
.
<ProgramProfileElectronicAddress>
<ElectronicAddressText>http://example.com</ElectronicAddressText>
<ElectronicAddressTypeName>URL</ElectronicAddressTypeName> </ProgramProfileElectronicAddress>
</Program>
</FacilitySite>
<FacilitySite>
.
.
.
</FacilitySite>
.
.
.
In the above xml code, assume 10 FacilitySite tags,there may be no program tag in some of them, but may exist multiple program tags in the rest. I want to convert the above code to the following xml code using xml.dom (i.e., I want to remove the program tag with its child tags except the ElectronicAddressText tag.
Also, I want to add the URL tag to the content of the ElectronicAddressText tag.):
<FacilitySite>
.
.
.
<ElectronicAddressText>
<URL>http://example.com</URL>
</ElectronicAddressText>
</FacilitySite>
<FacilitySite>
.
.
.
<ElectronicAddressText>
<URL>http://example.com</URL>
</ElectronicAddressText>
<ElectronicAddressText>
<URL>http://example.com</URL>
</ElectronicAddressText>
</FacilitySite>
<FacilitySite>
.
.
.
<ElectronicAddressText>
<URL>http://example.com</URL>
</ElectronicAddressText>
</FacilitySite><FacilitySite>
.
.
.
</FacilitySite>
.
.
.
I tried to run the following code but I faced with AttributeError: 'NodeList' object has no attribute 'nodeType'
from xml.dom.minidom import parse
import xml.dom.minidom
# Open XML document using minidom parser
DOMTree = xml.dom.minidom.parse("example.xml")
EPAs = DOMTree.getElementsByTagName("FacilitySite")
for EPA in EPAs:
program_nodes = EPA.getElementsByTagName("Program")
for program_node in program_nodes:
ElectronicAddressText_node = program_node.getElementsByTagName("ElectronicAddressText")
parent = program_node.parentNode
parent.replaceChild(ElectronicAddressText_node, program_node)
print(EPA.toxml())
question from:
https://stackoverflow.com/questions/65940838/replacing-xml-parent-tag-with-some-of-its-child-tags-using-xml-dom-in-python 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…