使用etree python解析xml时出错

时间:2014-02-19 12:39:11

标签: python xml xml-parsing elementtree

我有xml,有6042行数据。

我正在通过以下代码解析它,

import urllib2
import lxml.etree as ET
print 'Started Execution here'
url="url goes here"
xmldata = urllib2.urlopen(url).read()
root = ET.fromstring(xmldata)
print 'Done'
print root
for xin in root:
    print xin.tag, xin.attrib

有时会成功运行,但有时会显示以下错误,

1. lxml.etree.XMLSyntaxError: Opening and ending tag mismatch: ClinicID line 54 and location, line 54, column 60016

2. httplib.IncompleteRead: IncompleteRead(964 bytes read)

澄清第一个错误:我已下载XmlValidator,只是将我的xml复制到此应用程序中。并且它没有显示任何错误。它清楚地表明xml完全没有错误。

那么我在做什么错了?

任何帮助将不胜感激。

这是我的xml格式

<locations>
    <location>
        <LocationID>A</LocationID>
        <ClinicID>111</ClinicID>
        <buildingType>A Type</buildingType>
        <address>Address</address>
        <address2>>Address 1</address2>
        <city>City 1</city>
        <state>State 1</state>
        <zip>Zip 1</zip>
    </location>
    <location>
        <LocationID>A</LocationID>
        <ClinicID>111</ClinicID>
        <buildingType>A Type</buildingType>
        <address>>Address</address>
        <address2>>Address</address2>
        <city>City 1</city>
        <state>State 1</state>
        <zip>Zip 1</zip>
    </location>
    .   
    .
    .
    .
    .
    .
    <location>
        <LocationID>A</LocationID>
        <ClinicID>111</ClinicID>
        <buildingType>A Type</buildingType>
        <address>>Address</address>
        <address2>>Address</address2>
        <city>City 1</city>
        <state>State 1</state>
        <zip>Zip 1</zip>
    </location>


</locations>

更新

我已下载xml文件并尝试在本地打开它。它没有任何错误。那么为什么有时它不能使用urllib2.urlopen(url).read()?任何限制?

0 个答案:

没有答案