1
回答
用python解析xml的问题

比如解析:

<description><![CDATA[
test
]]></description>

现在用ptyhon解析的时候总是没有数据出来,为什么?

代码片段是:

def parseRessXml(self, xml_file):  
            #body是一个list,需要转成string
            document = ""
            for line in xml_file:
                document = document + line            
            doc = parseString(document)
            pkgs = doc.getElementsByTagName("item")            
            #遍历所有的资源地址
            i = 0;
            for pkg in pkgs:
                try:
                    i = i + 1
                    print '-------------------PARSE HTML (%s)-----------------' % (i)
                    title = pkg.getElementsByTagName("title")
                    title = self.getText(title[0].childNodes)
                    link = pkg.getElementsByTagName("link")
                    link = self.getText(link[0].childNodes)
                    description = pkg.getElementsByTagName("description")
                    description = self.getText(description[0].childNodes)
                    createtime = pkg.getElementsByTagName("pubDate")
                    createtime = self.getText(createtime[0].childNodes)
                    print description
                    break
                    
                except :
                    print "ERROR: PARSE_XMLRESS IS FAIL %s" % (link)
我了个去,description输出总是空,求解!
举报
如来神掌
发帖于6年前 1回/338阅
顶部