1
votes
 <PacketHeader>   
     <HeaderField>
        <name>number</name>
        <dataType>int</dataType>
     </HeaderField>
 </PacketHeader>

This is my small XML file and I want to extract out the text which is within the name tag.

Here is my code snippet:-

from xml.dom import minidom
from xml.dom.minidom import parse

xmldoc = minidom.parse('sample.xml')
packetHeader = xmldoc.getElementsByTagName("PacketHeader")
headerField = packetHeader.getElementsByTagName("HeaderField")
for field in headerField:
    getFieldName = field.getElementsByTagName("name")
    print getFieldName

But I am getting the location but not the text.

2

2 Answers

3
votes
from xml.dom import minidom
from xml.dom.minidom import parse

xmldoc = minidom.parse('sample.xml')

# find the name element, if found return a list, get the first element
name_element = xmldoc.getElementsByTagName("name")[0]

# this will be a text node that contains the actual text
text_node = name_element.childNodes[0]

# get text
print text_node.data

Please check this.

Update

BTW i suggest you ElementTree, Below is the code snippet using ElementTree which is doing samething as the above minidom code

import elementtree.ElementTree as ET

tree = ET.parse("sample.xml")

# the tree root is the toplevel `PacketHeader` element
print tree.findtext("HeaderField/name")
0
votes

A small variant of the accepted and correct answer above is:

from xml.dom import minidom


xmldoc = minidom.parse('fichier.xml')
name_element = xmldoc.getElementsByTagName('name')[0]
print name_element.childNodes[0].nodeValue

This simply uses nodeValue instead of its alias data