I have written a small function, which uses ElementTree to parse xml file,but it is throwing the following error “xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 1, column 0”. please find the code below
tree = ElementTree.parse(urllib2.urlopen('http://api.ean.com/ean-services/rs/hotel/v3/list?type=xml&apiKey=czztdaxrhfbusyp685ut6g6v&cid=8123&locale=en_US&city=Dallas%20&stateProvinceCode=TX&countryCode=US&minorRev=12'))
rootElem = tree.getroot()
hotel_list = rootElem.findall("HotelList")
There are multiple problems with the site you are using:
Site you are using somehow doesn’t honour
type=xmlyou are sending as GET arg, instead you will need to send accept header, telling site that you accept XML else it returns JSON dataSite is not accepting content-type
text/xmlso you need to sendapplication/xmlYour
parsecall is correct, it is wrongly mentioned in other answer that it should take data, insteadparsetakes file name or file type objectSo here is the working code
output:
Note I am creating a
Requestobject and passingAcceptheaderbtw if site is returning JSON why you need to parse XML, parsing JSON is simpler and you will get a ready made python object.