Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6024053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:05:55+00:00 2026-05-23T04:05:55+00:00

I have a simple XML document I’m trying to read in with Python DOM

  • 0

I have a simple XML document I’m trying to read in with Python DOM (see below):

XML File:

<?xml version="1.0" encoding="utf-8"?>
<HeaderLookup>
    <Header>
        <Reserved>2</Reserved>
        <CPU>1</CPU>
        <Flag>1</Flag>
        <VQI>12</VQI>
        <Group_ID>16</Group_ID>
        <DI>2</DI>
        <DE>1</DE>
        <ACOSS>5</ACOSS>
        <RGH>8</RGH>
    </Header>
</HeaderLookup>

Python Code:

from xml.dom import minidom

xml_file = open("test.xml")
xmlroot = minidom.parse(xml_file).documentElement
xml_file.close()

for item in xmlroot.getElementsByTagName("Header")[0].childNodes:
    print item

Result:

<DOM Text node "u'\n\t\t'">
<DOM Element: Reserved at 0x28d2828>
<DOM Text node "u'\n\t\t'">
<DOM Element: CPU at 0x28d28c8>
<DOM Text node "u'\n\t\t'">
<DOM Element: Flag at 0x28d2968>
<DOM Text node "u'\n\t\t'">
<DOM Element: VQI at 0x28d2a08>
<DOM Text node "u'\n\t\t'">
<DOM Element: Group_ID at 0x28d2ad0>
<DOM Text node "u'\n\t\t'">
<DOM Element: DI at 0x28d2b70>
<DOM Text node "u'\n\t\t'">
<DOM Element: DE at 0x28d2c10>
<DOM Text node "u'\n\t\t'">
<DOM Element: ACOSS at 0x28d2cb0>
<DOM Text node "u'\n\t\t'">
<DOM Element: RGH at 0x28d2d50>
<DOM Text node "u'\n\t'">

The result should be 9 Child Nodes (Reserved, CPU, Flag, VQI, Group_ID, DI, DE, ACOSS, and RGH), but for some reason it is returning a list of 19 nodes with 10 of them being whitespace (why is this even being considered a node in the first place?!). Can anyone tell me if there’s a way to get the XML parser to not include whitespace nodes?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T04:05:55+00:00Added an answer on May 23, 2026 at 4:05 am

    Whitespace is significant in XML, but check out ElementTree, which has a different API for processing XML than the DOM.

    Example

    from xml.etree import ElementTree as et
    
    data = '''\
    <?xml version="1.0" encoding="utf-8"?>
    <HeaderLookup>
        <Header>
            <Reserved>2</Reserved>
            <CPU>1</CPU>
            <Flag>1</Flag>
            <VQI>12</VQI>
            <Group_ID>16</Group_ID>
            <DI>2</DI>
            <DE>1</DE>
            <ACOSS>5</ACOSS>
            <RGH>8</RGH>
        </Header>
    </HeaderLookup>
    '''
    
    tree = et.fromstring(data)
    for n in tree.find('Header'):
        print n.tag,'=',n.text
    

    Output

    Reserved = 2
    CPU = 1
    Flag = 1
    VQI = 12
    Group_ID = 16
    DI = 2
    DE = 1
    ACOSS = 5
    RGH = 8
    

    Example (extending previous code)

    The whitespace is still present, but it is in .tail attributes. tail is the text node that follows an element (between the end of one element and the start of the next), while text is the text node between the start/end tag of an element.

    def dump(e):
        print '<%s>' % e.tag
        print 'text =',repr(e.text)
        for n in e:
            dump(n)
        print '</%s>' % e.tag
        print 'tail =',repr(e.tail)
    
    dump(tree)
    

    Output

    <HeaderLookup>
    text = '\n    '
    <Header>
    text = '\n        '
    <Reserved>
    text = '2'
    </Reserved>
    tail = '\n        '
    <CPU>
    text = '1'
    </CPU>
    tail = '\n        '
    <Flag>
    text = '1'
    </Flag>
    tail = '\n        '
    <VQI>
    text = '12'
    </VQI>
    tail = '\n        '
    <Group_ID>
    text = '16'
    </Group_ID>
    tail = '\n        '
    <DI>
    text = '2'
    </DI>
    tail = '\n        '
    <DE>
    text = '1'
    </DE>
    tail = '\n        '
    <ACOSS>
    text = '5'
    </ACOSS>
    tail = '\n        '
    <RGH>
    text = '8'
    </RGH>
    tail = '\n    '
    </Header>
    tail = '\n'
    </HeaderLookup>
    tail = None
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some sort of ... xml document as below : <file src=136090000-136100000> <member
I have a simple but huge xml file like below. I want to parse
I have a simple xml document that looks like the following snippet. I need
Situation: I have a simple XML document that contains image information. I need to
I have a simple doc.xml file which contains a single root element with a
I've written a simple XML Document that I am trying to transform with an
I'm trying to unmarshal a simple xml document from a public api from Convio.
I have a simple xml node inside an xml document in C# that I
I have a simple XML document <abc:MyForm xmlns:abc='http://myform.com'> <abc:Forms> <def:Form1 xmlns:def='http://decform.com'> .... </def:Form1> <ghi:Form2
I have a very simple XML document that I've retrieved from a larger parent.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.