我编码有问题,所以这可能很简单,但我卡住了。
我正在尝试解析通过电子邮件发送给的XML文件
App Engine
的新接收邮件功能。首先,我只是将XML粘贴到消息正文中,并使用CElementTree对其进行了良好的解析。然后我改为使用附件,用CElementTree解析它会产生以下错误:
前几行看起来是这样的:
<?xml version="1.0" standalone="yes"?>
<gpx xmlns="http://www.topografix.com/GPX/1/0" version="1.0" creator="TopoFusion 2.85" xmlns:TopoFusion="http://www.TopoFusion.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd http://www.TopoFusion.com http://www.TopoFusion.com/topofusion.xsd">
<name><![CDATA[Pacific Crest Trail section K hike 4]]></name><desc><![CDATA[Pacific Crest Trail section K hike 4. Five Lakes to Old Highway 40 near Donner. As described in Day Hikes on the PCT California edition by George & Patricia Semb. See pages 150-152 for access and exit trailheads. GPS data provided by the USFS]]></desc><author><![CDATA[MikeOnTheTrail]]></author><email><![CDATA[michaelonthetrail@yahoo.com]]></email><url><![CDATA[http://www.pcta.org]]></url>
<urlname><![CDATA[Pacific Crest Trail Association Homepage]]></urlname>
<time>2006-07-08T02:16:05Z</time>
GPX
gpxcontent = message.bodies(content_type='text/plain')
for x in gpxcontent:
gpxcontent = x[1].decode()
for event, elem in ET.iterparse(StringIO.StringIO(gpxcontent), events=("start", "start-ns")):
如果我使用Gmail将其作为附件附加到邮件中。然后按如下方式提取:
if isinstance(message.attachments, tuple):
attachments = [message.attachments]
gpxcontent = attachments[0][3].decode()
for event, elem in ET.iterparse(StringIO.StringIO(gpxcontent), events=("start", "start-ns")):
我理解上面的错误。第3行第10列似乎是开始![CDATA在第三行。