Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

replacing xml parent tag with some of its child tags using xml.dom in python

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...