[Tutor] help me please

祝顺民 zhusm@neusoft.com
Wed, 12 Sep 2001 15:16:43 +0800


这是 MIME 格式的具有很多部分消息。

--Boundary_(ID_0oq7fIwnhAjEDPKstES2YQ)
Content-type: text/plain; charset=gb2312
Content-transfer-encoding: QUOTED-PRINTABLE



help me please ,why can't parse the xml on linux.
my os is slcakware -linux-7.0
but the below programme have been executed on the windows 2000.
the programme is cut from the python doc on the www.python.org.
if you can help me .thank u very much.

>>> import xml.dom.minidom
>>>=20
>>> document =3D """\
=2E.. <slideshow>
=2E.. <title>Demo slideshow</title>
=2E.. <slide><title>Slide title</title>
=2E.. <point>This is a demo</point>
=2E.. <point>Of a program for processing slides</point>
=2E.. </slide>
=2E..=20
=2E.. <slide><title>Another demo slide</title>
=2E.. <point>It is important</point>
=2E.. <point>To have more than</point>
=2E.. <point>one slide</point>
=2E.. </slide>
=2E.. </slideshow>
=2E.. """
>>>=20
>>> dom =3D xml.dom.minidom.parseString(document)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/lib/python2.0/xml/dom/minidom.py", line 475, in parseStr=
ing
    return _doparse(pulldom.parseString, args, kwargs)
  File "/usr/lib/python2.0/xml/dom/minidom.py", line 464, in _doparse
    events =3D apply(func, args, kwargs)
  File "/usr/lib/python2.0/xml/dom/pulldom.py", line 237, in parseStr=
ing
    parser =3D xml.sax.make_parser()
  File "/usr/lib/python2.0/xml/sax/__init__.py", line 76, in make_par=
ser
    return _create_parser(parser_name)
  File "/usr/lib/python2.0/xml/sax/__init__.py", line 101, in _create=
_parser
    return drv_module.create_parser()
AttributeError: create_parser
>>>=20
>>> space =3D " "
>>> def getText(nodelist):
=2E..     rc =3D ""
=2E..     for node in nodelist:
=2E..         if node.nodeType =3D=3D node.TEXT_NODE:
=2E..             rc =3D rc + node.data
=2E..     return rc
=2E..=20
>>> def handleSlideshow(slideshow):
=2E..     print "<html>"
=2E..     handleSlideshowTitle(slideshow.getElementsByTagName("title"=
)[0])
=2E..     slides =3D slideshow.getElementsByTagName("slide")
=2E..     handleToc(slides)
=2E..     handleSlides(slides)
=2E..     print "</html>"
=2E..=20
>>> def handleSlides(slides):
=2E..     for slide in slides:
=2E..        handleSlide(slide)
=2E..=20
>>> def handleSlide(slide):
=2E..     handleSlideTitle(slide.getElementsByTagName("title")[0])
=2E..     handlePoints(slide.getElementsByTagName("point"))
=2E..=20
>>> def handleSlideshowTitle(title):
=2E..     print "<title>%s</title>" % getText(title.childNodes)
=2E..=20
>>> def handleSlideTitle(title):
=2E..     print "<h2>%s</h2>" % getText(title.childNodes)
=2E..=20
>>> def handlePoints(points):
=2E..     print "<ul>"
=2E..     for point in points:
=2E..         handlePoint(point)
=2E..     print "</ul>"
=2E..=20
>>> def handlePoint(point):
=2E..     print "<li>%s</li>" % getText(point.childNodes)
=2E..=20
>>> def handleToc(slides):
=2E..     for slide in slides:
=2E..         title =3D slide.getElementsByTagName("title")[0]
=2E..         print "<p>%s</p>" % getText(title.childNodes)
=2E..=20
>>> handleSlideshow(dom)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
NameError: There is no variable named 'dom'


--Boundary_(ID_0oq7fIwnhAjEDPKstES2YQ)
Content-type: text/html; charset=gb2312
Content-transfer-encoding: QUOTED-PRINTABLE

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; charset=3Dgb231=
2">
<META content=3D"MSHTML 6.00.2462.0" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#c0c0c0>
<DIV><BR>&nbsp;</DIV>
<DIV><FONT size=3D2>
<DIV><FONT size=3D2>help me please ,why can't parse the xml on linux.=
</FONT></DIV>
<DIV><FONT size=3D2>my os is slcakware -linux-7.0</FONT></DIV>
<DIV><FONT size=3D2>but the below programme have been executed on the=
 windows=20
2000.</FONT></DIV>
<DIV>the programme is cut from the python doc on the <A=20
href=3D"http://www.python.org">www.python.org</A>.</DIV>
<DIV>if you can help me .thank u very much.</DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>&gt;&gt;&gt; import xml.dom.minidom<BR>&gt;&gt;&g=
t;=20
<BR>&gt;&gt;&gt; document =3D """\<BR>... &lt;slideshow&gt;<BR>...=
=20
&lt;title&gt;Demo slideshow&lt;/title&gt;<BR>... &lt;slide&gt;&lt;tit=
le&gt;Slide=20
title&lt;/title&gt;<BR>... &lt;point&gt;This is a demo&lt;/point&gt;<=
BR>...=20
&lt;point&gt;Of a program for processing slides&lt;/point&gt;<BR>...=
=20
&lt;/slide&gt;<BR>... <BR>... &lt;slide&gt;&lt;title&gt;Another demo=
=20
slide&lt;/title&gt;<BR>... &lt;point&gt;It is important&lt;/point&gt;=
<BR>...=20
&lt;point&gt;To have more than&lt;/point&gt;<BR>... &lt;point&gt;one=
=20
slide&lt;/point&gt;<BR>... &lt;/slide&gt;<BR>... &lt;/slideshow&gt;<B=
R>...=20
"""<BR>&gt;&gt;&gt; <BR>&gt;&gt;&gt; dom =3D=20
xml.dom.minidom.parseString(document)<BR>Traceback (most recent call=
=20
last):<BR>&nbsp; File "&lt;stdin&gt;", line 1, in ?<BR>&nbsp; File=
=20
"/usr/lib/python2.0/xml/dom/minidom.py", line 475, in=20
parseString<BR>&nbsp;&nbsp;&nbsp; return _doparse(pulldom.parseString=
, args,=20
kwargs)<BR>&nbsp; File "/usr/lib/python2.0/xml/dom/minidom.py", line =
464, in=20
_doparse<BR>&nbsp;&nbsp;&nbsp; events =3D apply(func, args, kwargs)<B=
R>&nbsp; File=20
"/usr/lib/python2.0/xml/dom/pulldom.py", line 237, in=20
parseString<BR>&nbsp;&nbsp;&nbsp; parser =3D xml.sax.make_parser()<BR=
>&nbsp; File=20
"/usr/lib/python2.0/xml/sax/__init__.py", line 76, in=20
make_parser<BR>&nbsp;&nbsp;&nbsp; return _create_parser(parser_name)<=
BR>&nbsp;=20
File "/usr/lib/python2.0/xml/sax/__init__.py", line 101, in=20
_create_parser<BR>&nbsp;&nbsp;&nbsp; return=20
drv_module.create_parser()<BR>AttributeError: create_parser<BR>&gt;&g=
t;&gt;=20
<BR>&gt;&gt;&gt; space =3D " "<BR>&gt;&gt;&gt; def=20
getText(nodelist):<BR>...&nbsp;&nbsp;&nbsp;&nbsp; rc =3D=20
""<BR>...&nbsp;&nbsp;&nbsp;&nbsp; for node in=20
nodelist:<BR>...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if=
=20
node.nodeType =3D=3D=20
node.TEXT_NODE:<BR>...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;=20
rc =3D rc + node.data<BR>...&nbsp;&nbsp;&nbsp;&nbsp; return rc<BR>...=
=20
<BR>&gt;&gt;&gt; def handleSlideshow(slideshow):<BR>...&nbsp;&nbsp;&n=
bsp;&nbsp;=20
print "&lt;html&gt;"<BR>...&nbsp;&nbsp;&nbsp;&nbsp;=20
handleSlideshowTitle(slideshow.getElementsByTagName("title")[0])<BR>.=
..&nbsp;&nbsp;&nbsp;&nbsp;=20
slides =3D slideshow.getElementsByTagName("slide")<BR>...&nbsp;&nbsp;=
&nbsp;&nbsp;=20
handleToc(slides)<BR>...&nbsp;&nbsp;&nbsp;&nbsp;=20
handleSlides(slides)<BR>...&nbsp;&nbsp;&nbsp;&nbsp; print "&lt;/html&=
gt;"<BR>...=20
<BR>&gt;&gt;&gt; def handleSlides(slides):<BR>...&nbsp;&nbsp;&nbsp;&n=
bsp; for=20
slide in slides:<BR>...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
=20
handleSlide(slide)<BR>... <BR>&gt;&gt;&gt; def=20
handleSlide(slide):<BR>...&nbsp;&nbsp;&nbsp;&nbsp;=20
handleSlideTitle(slide.getElementsByTagName("title")[0])<BR>...&nbsp;=
&nbsp;&nbsp;&nbsp;=20
handlePoints(slide.getElementsByTagName("point"))<BR>... <BR>&gt;&gt;=
&gt; def=20
handleSlideshowTitle(title):<BR>...&nbsp;&nbsp;&nbsp;&nbsp; print=
=20
"&lt;title&gt;%s&lt;/title&gt;" % getText(title.childNodes)<BR>...=
=20
<BR>&gt;&gt;&gt; def handleSlideTitle(title):<BR>...&nbsp;&nbsp;&nbsp=
;&nbsp;=20
print "&lt;h2&gt;%s&lt;/h2&gt;" % getText(title.childNodes)<BR>...=
=20
<BR>&gt;&gt;&gt; def handlePoints(points):<BR>...&nbsp;&nbsp;&nbsp;&n=
bsp; print=20
"&lt;ul&gt;"<BR>...&nbsp;&nbsp;&nbsp;&nbsp; for point in=20
points:<BR>...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
handlePoint(point)<BR>...&nbsp;&nbsp;&nbsp;&nbsp; print "&lt;/ul&gt;"=
<BR>...=20
<BR>&gt;&gt;&gt; def handlePoint(point):<BR>...&nbsp;&nbsp;&nbsp;&nbs=
p; print=20
"&lt;li&gt;%s&lt;/li&gt;" % getText(point.childNodes)<BR>... <BR>&gt;=
&gt;&gt;=20
def handleToc(slides):<BR>...&nbsp;&nbsp;&nbsp;&nbsp; for slide in=
=20
slides:<BR>...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; title =
=3D=20
slide.getElementsByTagName("title")[0]<BR>...&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;&nbsp;=20
print "&lt;p&gt;%s&lt;/p&gt;" % getText(title.childNodes)<BR>...=20
<BR>&gt;&gt;&gt; handleSlideshow(dom)<BR>Traceback (most recent call=
=20
last):<BR>&nbsp; File "&lt;stdin&gt;", line 1, in ?<BR>NameError: The=
re is no=20
variable named 'dom'</FONT></DIV></FONT></DIV></BODY></HTML>


--Boundary_(ID_0oq7fIwnhAjEDPKstES2YQ)--