Search code examples
pythonxmlpython-3.xminidom

Get Element Text Using xml mini dom python


i am trying to get the text of an element using mini dom, in the following code , i have also tried getText() Method as suggested here , but i am unable to get the desired output, following is my code. I dont get the Text value from the element i am trying to work on.

import xml.dom.minidom

doc = xml.dom.minidom.parse("DL_INVOICE_DETAIL_TCB.xml")
results = doc.getElementsByTagName("G_TRANSACTIONS")
def getText(nodelist):
    rc = []
    for node in nodelist:
        if node.nodeType == node.TEXT_NODE:
            rc.append(node.data)
    return ''.join(rc)
for result in results:
    for element in result.getElementsByTagName("INVOICE_NUMBER"):
        print(element.nodeType)
        print(element.nodeValue)

Following is my XML sample

<LIST_G_TRANSACTIONS>
    <G_TRANSACTIONS>
        <INVOICE_NUMBER>31002</INVOICE_NUMBER>
        <TRANSACTION_CLASS>Invoice</TRANSACTION_CLASS>
    </G_TRANSACTIONS>
</LIST_G_TRANSACTIONS>

I am using the following


Solution

  • A minidom based answer

    from xml.dom import minidom
    
    xml = """\
    <LIST_G_TRANSACTIONS>
        <G_TRANSACTIONS>
            <INVOICE_NUMBER>31002</INVOICE_NUMBER>
            <TRANSACTION_CLASS>Invoice1</TRANSACTION_CLASS>
        </G_TRANSACTIONS>
        <G_TRANSACTIONS>
            <INVOICE_NUMBER>31006</INVOICE_NUMBER>
            <TRANSACTION_CLASS>Invoice2</TRANSACTION_CLASS>
        </G_TRANSACTIONS>    
    </LIST_G_TRANSACTIONS>"""
    
    dom = minidom.parseString(xml)
    invoice_numbers = [int(x.firstChild.data) for x in dom.getElementsByTagName("INVOICE_NUMBER")]
    print(invoice_numbers)
    

    output

    [31002, 31006]