ElementTree problem saving images

22 views
Skip to first unread message

Nuno Vieira

unread,
Oct 24, 2019, 3:28:00 PM10/24/19
to Django users
Hi,

i receive an xml file and i am parsing the values and sving them to database.
XML Example:
<property>...</property>
<property>...</property>
<property>...</property>
<property>
<id>9896</id>
<date>2019-07-11 13:12:09</date>
<ref>BC30401</ref>
<price>895000</price>
<currency>EUR</currency>
<price_freq>sale</price_freq>
<part_ownership>0</part_ownership>
<leasehold>0</leasehold>
<new_build>0</new_build>
<surface_area>
<built>915</built>
</surface_area>
<type>Villa</type>
<location>
<latitude>37.134720</latitude>
<longitude>-8.020436</longitude>
</location>
<location_detail>optional location detail</location_detail>
<town>Vilamoura</town>
<province>Algarve</province>
<country>Portugal</country>
<beds>4</beds>
<baths>3</baths>
<pool>1</pool>
<images>
<image id="11">
</image>

It´s all working ok but with images its a litle confused because i can only save the first and the last image. If a property have 12 pictures i can only save the picture nr 1 and picture nr 12, if the property have 8 pictures i can only save picture nr1 and picture nr 8. It´s weird!

My code to get the pictures url:
img             = props.find('images')

                        for child in img:

                                if child.get('id') == '1':
                                       img_main_url = child.find('url').text
                               
                               if child.get('id') == '2':
                                       
                                       img_1_url = child.find('url').text
                               else:
                                       img_1_url = None
                               if child.get('id') == '3':
                                       img_2_url = child.find('url').text
                               else:
                                       img_2_url = None
                               if child.get('id') == '4':
                                       img_3_url = child.find('url').text
                               else:
                                       img_3_url = None
                               if child.get('id') == '5':
                                       img_4_url = child.find('url').text
                               else:
                                       img_4_url = None


I could use a litle help from someone more experienced in django.

Thanks for your help

Arthur Antoine

unread,
Oct 25, 2019, 12:01:45 AM10/25/19
to Django users
The image urls are being set to None on every iteration where the id doesn't match. Try setting all of the urls to None before the loop, then remove the else blocks.

Jody Fitzpatrick

unread,
Oct 25, 2019, 12:01:45 AM10/25/19
to Django users
Would something like XMLTODICT help?


import xmltodict  # Third Party
data
= """<xml>"""
parsed_data
=xmltodict.parse(data)

# do your data thing here...


Nuno Vieira

unread,
Nov 6, 2019, 11:13:03 AM11/6/19
to Django users
Thanks for your feedback.

The problem is if i set all the urls to None before, and a property have only 6 images of 20, on database saves the 6 images of this property and saves also the other image columns with images from a different property, but when i use the else blocks instead of setting all to None before the loop, it only saves the first (nr 1) and the last image (nr 6) of the property.
I have no idea why this behavior.

I really need some help.
Thanks.

Integr@te System

unread,
Nov 6, 2019, 12:37:00 PM11/6/19
to django...@googlegroups.com
Hi Issuer,

Plz select options :
1. Follow someone else convert xml to dict, and then process forward.
2. See this document python DOM XML doc, especial in 20.6.2.3 section

see nodelist.item(i) and __getitem__() method.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/249cb5ad-668e-4fe9-aa54-f55c9e3a8ae6%40googlegroups.com.

Nuno Vieira

unread,
Nov 7, 2019, 3:16:15 PM11/7/19
to Django users
Hi,

thanks for your help.

i solved the situation like this:
img             = props.find('images')
                     
                        photos = []
                       for child in img[:len(img)]:
                       
                                photos.append(child[0].text)
                                             
                        try:
                               img_1_url       = photos[1]
                       except IndexError:
                               img_1_url       = None
                       try:
                               img_2_url       = photos[2]
                       except IndexError:
                               img_2_url       = None

Just an example, but like this, for each property i could attached the correct number of images.

Thanks again for all of you that tried to help me!

Best regards
Reply all
Reply to author
Forward
0 new messages