1

I have a xml file named order-service-api-inbound-sample.xml

who's file path is /home/bs-086/Django/mh-portal/master/portal/portal/endpoints/testdata/order-service-api-inbound-sample.xml

<?xml version="1.0" encoding="UTF-8"?>
<purchaseOrder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="file:/C:/Users/COMP/Desktop/purchase_order_inbound_schema.xsd">
    <customer>
        <customer-name>jubydull</customer-name>
        <customer-address1>Bangladesh</customer-address1>
        <customer-address2>dhaka</customer-address2>
        <customer-city>dhaka</customer-city>
        <customer-state></customer-state>
        <customer-zip>1205</customer-zip>
        <customer-country>Bangladesh</customer-country>
    </customer>
</purchaseOrder>

I have a model class named models.py who's file path is /home/bs-086/Django/mh-portal/master/portal/portal/endpoints/models.py

from __future__ import unicode_literals
from django.db import models

class Customer(models.Model):
    name = models.CharField(max_length=120)
    address1 = models.CharField(max_length=120)
    address2 = models.CharField(max_length=120)
    city = models.CharField(max_length=50)
    state = models.CharField(max_length=50)
    zip = models.IntegerField()
    country = models.CharField(max_length=50)

    def __str__(self):
        return self.name

Now I wanna save xml file value to my models. How I can I do that ? I am using postgresql database.

2
  • lot's of different possibilities, write a custom desrializer, transform into the standard django dumpdata format with xslt. Load the xml into a dom and create objects from that. Load directly into postgresql with it's xml functions, .... Commented Sep 15, 2016 at 12:06
  • give me any kind of resource. I am new Django @e4c5 Commented Sep 15, 2016 at 12:38

1 Answer 1

1

It depends on you!

  1. If you want to do it from a view you can create a function in your view and run it when the view was rendered.
  2. Import the file in a form and parse the XML file then store the data to your database table. same as number 1 but the function should run in form.py
  3. If you want to do it automatically, for example fill your database from XML file without any view or form. In this case you can put your function in models.py and after creating all models run the function!! I know, This way is a little weird!!

you can use xml.etree.ElementTree library that it is in python libraries by default. Don't forget to add it to INSTALLED_APPS list in setting.py

This is the function you can use:

import xml.etree.ElementTree as ET

def saveXML2db():
   file_dir = '<PATH_TO_YOUR_XML_FILE>'
   purchaseOrder =  ET.parse(file_dir)
   customers = purchaseOrder.findall("customer")
   for customer in customers:
       name = customer.find("customer -name").text
       address1 = customer.find("customer-address1").text
       address2 = customer.find("customer-address2").text
       city = customer.find("customer-city").text
       state = customer.find("customer-state").text
       zip2 = customer.find("customer-zip").text
       zip = int(zip2)
       country = customer.find("customer-country").text
       x = Customer.objects.create(name=name, address1=address1, address2=address2, city=city, state=state, zip=zip, country=country)
       x.save()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.