1
 <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 Answers 2

3
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")
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.