parser un fichier xml avec xml.sax
BOnjour à tous,
je viens vers vous pour soliciter votre aide sur un parsing de fichier xml... En effet, je me heurte à quelques problématiques, à savoir:
je n'arrive pas à récupérer certainnes informations, commme la description, l'année et les images.
mon fichier xml est le suivant:
Citation:
<?xml version="1.0" encoding="utf-8"?> <xml> <ad>
<id><![CDATA[1]]></id> <title><![CDATA[Jaguar XKR]]></title>
<url><![CDATA[link]]></url> <content><![CDATA[Jaguar XKR
skqlmskmlqskml qpdqsdkqmlkdmlq qdmlkqsmldkdqsklqsml]]></content>
<price currency="US"><![CDATA[4000.00]]></price>
<city><![CDATA[Tokyo]]></city>
<postcode><![CDATA[92200]]></postcode>
<date><![CDATA[2013-10-01]]></date>
<expiration_date><![CDATA[2017-02-27]]></expiration_date>
<year><![CDATA[2008]]></year> <make><![CDATA[Jaguar]]></make>
<model><![CDATA[Xkr]]></model> <fuel><![CDATA[fuel]]></fuel>
<mileage><![CDATA[68000]]></mileage>
<transmission><![CDATA[Automatique]]></transmission>
<power><![CDATA[12]]></power> <pictures>
<picture><picture_url><![CDATA[link1]]></picture_url></picture>
<picture><picture_url><![CDATA[link2]]></picture_url></picture>
</pictures> </ad>
mon code python est le suivant:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| #!/usr/bin/python
import xml.sax
class MovieHandler( xml.sax.ContentHandler ):
def __init__(self):
self.CurrentData = ""
self.title = ""
self.url = ""
self.description = ""
self.price = ""
self.city = ""
self.postcode = ""
self.year = ""
self.date = ""
self.expiration_date = ""
self.brand = ""
self.model = ""
self.fuel = ""
self.mileage = ""
self.transmission = ""
self.power = ""
self.picture = ""
# Call when an element starts
def startElement(self, tag, attributes):
self.CurrentData = tag
# Call when an elements ends
def endElement(self, tag):
if self.CurrentData == "title":
print "Title:", self.title
elif self.CurrentData == "url":
print "Url:", self.url
elif self.CurrentData == "content":
print "Description:", self.description
elif self.CurrentData == "price":
print "price:", self.price
elif self.CurrentData == "city":
print "City:", self.city
elif self.CurrentData == "postcode":
print "Postcode:", self.postcode
elif self.CurrentData == "date":
print "Date:", self.date
elif self.CurrentData == "expiration_date":
print "expiration date:", self.expiration_date
elif self.CurrentData == "year":
print "year:", self.year
'''
elif self.CurrentData == "make":
print "Brand:", self.brand
elif self.CurrentData == "model":
print "Model:", self.model
self.CurrentData = ""
'''
# Call when a character is read
def characters(self, content):
if self.CurrentData == "title":
self.title = content
elif self.CurrentData == "url":
self.url = content
elif self.CurrentData == "content":
self.description = content
elif self.CurrentData == "price":
self.price = content
elif self.CurrentData == "city":
self.city = content
elif self.CurrentData == "postcode":
self.postcode = content
elif self.CurrentData == "date":
self.date = content
elif self.CurrentData == "expiration_date":
self.expiration_date = content
elif self.CurrentData == "year":
self.year = content
'''
elif self.CurrentData == "make":
self.brand = content
elif self.CurrentData == "model":
self.model = content
'''
if ( __name__ == "__main__"):
# create an XMLReader
parser = xml.sax.make_parser()
# turn off namepsaces
parser.setFeature(xml.sax.handler.feature_namespaces, 0)
# override the default ContextHandler
Handler = MovieHandler()
parser.setContentHandler( Handler )
parser.parse("myfile.xml") |
comment puis-je lire ces balises ?
Merci d'avance.