user15150876
user15150876

Reputation: 31

Read XML on Python

I'm using minidom for read XML document. I need find the "Member" tag name and if the attributes start with "W" or "F" I need read the text between "MultilanguajeTex" tag name.

It's possible?

I try this:

   mydoc = minidom.parse('DB.xml')
   items = mydoc.getElementsByTagName('Member')


   for elem in items:

    
    valor = elem.attributes['Name'].value
    
    
    if (valor[0] == "F" or valor[0] == "W") and len(valor)<4:
        print(len(valor))
        print(valor)
        texto = elem.SelectNodes('MultiLanguageText')
        print(texto.localName)

XML text:

<Member Name="F00" Datatype="Bool">
<Comment>
    <MultiLanguageText Lang="en-GB">
        BottomSidePanels_Feeding_Zone_41 Fa 00. Material Jam. Material too much time going out Fw
    </MultiLanguageText>
</Comment>        

Thanks!

Upvotes: 1

Views: 65

Answers (1)

Alexandra Dudkina
Alexandra Dudkina

Reputation: 4462

Using minidom it could be done as follows:

from xml.dom.minidom import parseString

xml = '''<root>
    <Member Name="F00" Datatype="Bool">
        <Comment>
            <MultiLanguageText Lang="en-GB">
                BottomSidePanels_Feeding_Zone_41 Fa 00. Material Jam. Material too much time going out Fw
            </MultiLanguageText>
        </Comment>
    </Member>
</root>'''

root = parseString(xml)
items = root.getElementsByTagName('Member')

for elem in items:
    valor = elem.attributes['Name'].value
    if (valor[0] == "F" or valor[0] == "W") and len(valor) < 4:
        texts = elem.getElementsByTagName('MultiLanguageText')
        for text in texts:
          print(text.firstChild.nodeValue)

A bit simpler it could be achieved using lxml as it allows usage of XPath expressions:

import lxml.etree as etree

root = etree.fromstring(xml)
text_elements = root.xpath('.//Member[starts-with(@Name, "F") or starts-with(@Name, "W")]//MultiLanguageText')
for text_element in text_elements:
    print(text_element.text.strip())

Upvotes: 1

Related Questions