LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Python XML won't work. (https://www.linuxquestions.org/questions/programming-9/python-xml-won%27t-work-230632/)

Travis86 09-14-2004 05:13 PM

Python XML won't work.
 
I am trying to get pyXML to work and am having no luck. I could tell that it was installed but not working, so I tried to install it again. This didn't do anything. Also, I am doing this on OS X.

I have made two programs by more or less copying from Python and XML (the O'Reilly book), but they don't work. And I have tried a program from http://www.rexx.com/~dkuhlman/pyxmlfaq.html, but that didn't work either. Hopefully someone will be able to figure out what is going on.

Code:

import xml.dom.pulldom
from xml.dom.pulldom import parse

xmlhandle = open("example.xml")
xmlfile = parse(xmlhandle)

print xmlfile
print xmlfile.firstChild

Gives the message:

Code:

<xml.dom.pulldom.DOMEventStream instance at 0x4ae648>
Traceback (most recent call last):
  File "xmltest.py", line 9, in ?
    print xmlfile.firstChild
AttributeError: DOMEventStream instance has no attribute 'firstChild'

This:
Code:

import xml.dom.minidom

print xml.dom.minidom.parseString("<pete>re</pete>")
print xml.dom.minidom.parse(open("example.xml"))

Gives the message:
Code:

Traceback (most recent call last):
  File "xmltest.py", line 4, in ?
    print xml.dom.minidom.parse(open("example.xml"))
  File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/site-packages/_xmlplus/dom/minidom.py", line 1908, in parse
    return expatbuilder.parse(file)
  File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/site-packages/_xmlplus/dom/expatbuilder.py", line 928, in parse
    result = builder.parseFile(file)
  File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/site-packages/_xmlplus/dom/expatbuilder.py", line 207, in parseFile
    parser.Parse(buffer, 0)
  File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/site-packages/_xmlplus/dom/expatbuilder.py", line 437, in element_decl_handler
    assert info._model is None
AssertionError

The program from http://www.rexx.com/~dkuhlman/pyxmlfaq.html, which I am just running because it's supposed to work is:
Code:

import sys
from xml.dom import minidom, Node

def showNode(node):
    if node.nodeType == Node.ELEMENT_NODE:
        print 'Element name: %s' % node.nodeName
        for (name, value) in node.attributes.items():
            print '    Attr -- Name: %s  Value: %s' % (name, value)
        if node.attributes.get('ID') is not None:
            print '    ID: %s' % node.attributes.get('ID').value

def main():
    root = minidom.parse(sys.argv[1])
    node = root.firstChild
    showNode(node)
    for child in node.childNodes:
        showNode(child)

if __name__ == '__main__':
    main()

When run with xmltest2.py example.xml gives the message:
Code:

File "xmltest2.py", line 20, in ?
    main()
  File "xmltest2.py", line 13, in main
    root = minidom.parse(sys.argv[1])
  File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/site-packages/_xmlplus/dom/minidom.py", line 1908, in parse
    return expatbuilder.parse(file)
  File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/site-packages/_xmlplus/dom/expatbuilder.py", line 924, in parse
    result = builder.parseFile(fp)
  File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/site-packages/_xmlplus/dom/expatbuilder.py", line 207, in parseFile
    parser.Parse(buffer, 0)
  File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/site-packages/_xmlplus/dom/expatbuilder.py", line 437, in element_decl_handler
    assert info._model is None
AssertionError



All times are GMT -5. The time now is 02:55 PM.