473,396 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Possible error in 'dive into Python' book, help!

I have been going through Dive into Python which up to now has been
excellent. I am now working through Chapter 9, XML Processing. I am 9
pages in (p182) in the 'Parsing XML section. The following code is
supposed to return the whole XML document (I have put ti at the end of
this email):

from xml.dom import minidom

xmldoc =
minidom.parse('/home/ben/diveintopython-5.4/py/kgp/binary.xml')
grammerNode = xmldoc.firstChild

print grammerNode.toxml()

But it only returns:

<!DOCTYPE grammar
PUBLIC '-//diveintopython.org//DTD Kant Generator Pro v1.0//EN'
'kgp.dtd'>

The next line is then

grammerNode.childNodes

And it returns an empty tuples;(

Kind of stuck here as I don't really want to continue. Has anyone any
idea what is going on?

Ben

binary.xml

<?xml version="1.0"?>
<!DOCTYPE grammar PUBLIC "-//diveintopython.org//DTD Kant Generator Pro
v1.0//EN" "kgp.dtd">
<grammar>
<ref id="bit">
<p>0</p>
<p>1</p>
</ref>
<ref id="byte">
<p><xref id="bit"/><xref id="bit"/><xref id="bit"/><xref
id="bit"/><xref id="bit"/><xref id="bit"/><xref id="bit"/><xref
id="bit"/></p>
</ref>
</grammar>


Jul 28 '06 #1
1 1811
In article <ma***************************************@python. org>,
Ben Edwards (lists) <li***@videonetwork.orgwrote:
>I have been going through Dive into Python which up to now has been
excellent. I am now working through Chapter 9, XML Processing. I am 9
pages in (p182) in the 'Parsing XML section. The following code is
supposed to return the whole XML document (I have put ti at the end of
this email):

from xml.dom import minidom

xmldoc =
minidom.parse('/home/ben/diveintopython-5.4/py/kgp/binary.xml')
grammerNode = xmldoc.firstChild

print grammerNode.toxml()

But it only returns:

<!DOCTYPE grammar
PUBLIC '-//diveintopython.org//DTD Kant Generator Pro v1.0//EN'
'kgp.dtd'>

The next line is then

grammerNode.childNodes

And it returns an empty tuples;(

Kind of stuck here as I don't really want to continue. Has anyone any
idea what is going on?
OK, your xmldoc has two child nodes. The first is the <!DOCTYPE ... >
you're getting above. Which of course doesn't have any children, so
grammarNode.childNodes is naturally empty. The <grammarelement is
the *second* child of the xmldoc, so of course isn't xmldoc.firstChild.
Probably what you want is:

grammarNode = xmldoc.documentElement

Dive Into Python's assertion that "A Document always has only one
child node, the root element of the XML document" looks a little
off from where I'm standing:
>>print xmldoc.toxml()
<?xml version="1.0" ?>
<!DOCTYPE grammar
PUBLIC '-//diveintopython.org//DTD Kant Generator Pro v1.0//EN' 'kgp.dtd'>
<grammar>
<ref id="bit">
<p>0</p>
<p>1</p>
</ref>
<ref id="byte">
<p><xref id="bit"/><xref id="bit"/><xref id="bit"/><xref id="bit"/><xref id="bit"/><xref id="bit"/><xref id="bit"/><xref id="bit"/></p>
</ref>
</grammar>
>>xmldoc.childNodes
[<xml.dom.minidom.DocumentType instance at 0x4053640c>, <DOM Element: grammar at 0x40536f0c>]
>>>
--
\S -- si***@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
___ | "Frankly I have no feelings towards penguins one way or the other"
\X/ | -- Arthur C. Clarke
her nu becomež se bera eadward ofdun hlęddre heafdes bęce bump bump bump
Jul 31 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Franz Mueller | last post by:
Hi, which of the following books would you recommend: "Dive into Python" or "Beginning Python: From Novice to Professional"? I'm an experienced C++-programmer who wants to take a look at...
5
by: Casey Hawthorne | last post by:
Since there was talk of if-then-else not being allowed in lambda expressions, the following is from "Dive into Python" The and-or conditional expression trick from page 41 of "Dive into Python"...
3
by: hanumizzle | last post by:
I find Dive Into Python generally an excellent text, and I am not surprised to see people recommending it...but I have noticed a few errors already: ...
0
by: Ben Edwards | last post by:
I have been going through Dive into Python which up to now has been excellent. I am now working through Chapter 9, XML Processing. I am 9 pages in (p182) in the 'Parsing XML section. The...
2
by: Fred C. Dobbs | last post by:
I feel like an idiot. I'm going thru "Dive Into Python" and running the first program - odbchelper.py My output is "pwd=secret;database=master;uid=sa;server=mpilgrim" which has all the...
32
by: Licheng Fang | last post by:
Basically, the problem is this: 'do' Python's NFA regexp engine trys only the first option, and happily rests on that. There's another example: 'oneself' The Python regular expression...
51
by: erikcw | last post by:
DiveIntoPython.org was the first book I read on python, and I really got a lot out of it. I need to start learning Java (to maintain a project I've inherited), and was wondering if anyone knew of...
3
by: james_027 | last post by:
Hi, class UserDict: def __init__(self, dict=None): self.data = {} if dict is not None: self.update(dict) I just don't understant this code, as it is not also mention in the book. the update...
8
by: jonbutler88 | last post by:
Just writing a simple website spider in python, keep getting these errors, not sure what to do. The problem seems to be in the feed() function of htmlparser. Traceback (most recent call last):...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.