python sax error "junk after document element"

Posted by user293487 on Stack Overflow See other posts from Stack Overflow or by user293487
Published on 2010-04-04T15:22:07Z Indexed on 2010/04/04 15:33 UTC
Read the original article Hit count: 286

Filed under:
|

Hi, I use python sax to parse xml file. The xml file is actually a combination of multiple xml files. It looks like as follows:

<row name="abc" age="40" body="blalalala..." creationdate="03/10/10" />
<row name="bcd" age="50" body="blalalala..." creationdate="03/10/09" />

My python code is in the following. It show "junk after document element" error. Any good idea to solve this problem. Thanks.

from xml.sax.handler import ContentHandler
from xml.sax import make_parser,SAXException
import sys

class PostHandler (ContentHandler):
    def __init__(self):
        self.find = 0
        self.buffer = ''
        self.mapping={}
    def startElement(self,name,attrs):
        if name == 'row':
             self.find = 1
             self.body = attrs["body"]
             print attrs["body"]
    def character(self,data):
        if self.find==1:
             self.buffer+=data
    def endElement(self,name):
        if self.find == 1:
             self.mapping[self.body] = self.buffer
             print self.mapping
parser = make_parser()
handler = PostHandler()
parser.setContentHandler(handler)
try:
    parser.parse(open("2.xml"))
except SAXException:

© Stack Overflow or respective owner

Related posts about python

Related posts about sax