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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ben Edwards (lists)

    #1

    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.firstChi ld

    print grammerNode.tox ml()

    But it only returns:

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

    The next line is then

    grammerNode.chi ldNodes

    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>




  • Sion Arrowsmith

    #2
    Re: Possible error in 'dive into Python' book, help!

    In article <mailman.8658.1 154093256.27775 .python-list@python.org >,
    Ben Edwards (lists) <lists@videonet work.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.firstChi ld
    >
    >print grammerNode.tox ml()
    >
    >But it only returns:
    >
    ><!DOCTYPE grammar
    PUBLIC '-//diveintopython. org//DTD Kant Generator Pro v1.0//EN'
    'kgp.dtd'>
    >
    >The next line is then
    >
    >grammerNode.ch ildNodes
    >
    >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.chi ldNodes is naturally empty. The <grammareleme nt is
    the *second* child of the xmldoc, so of course isn't xmldoc.firstChi ld.
    Probably what you want is:

    grammarNode = xmldoc.document Element

    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.childN odes
    [<xml.dom.minido m.DocumentType instance at 0x4053640c>, <DOM Element: grammar at 0x40536f0c>]
    >>>
    --
    \S -- siona@chiark.gr eenend.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

    Comment

    Working...