{"id":23101,"date":"2023-01-16T07:07:00","date_gmt":"2023-01-16T06:07:00","guid":{"rendered":"https:\/\/expert-only.com\/?p=23101"},"modified":"2023-03-08T10:34:25","modified_gmt":"2023-03-08T09:34:25","slug":"import-xml-document-in-sql-server-python","status":"publish","type":"post","link":"https:\/\/expert-only.com\/en\/python\/import-xml-document-in-sql-server-python\/","title":{"rendered":"How to import an XML document into SQL Server using Python?"},"content":{"rendered":"\n<p>Data is a crucial element in any organization, and with the rapid growth of technology, the amount of data being generated is increasing exponentially. XML data is a popular format for representing structured data in a readable and machine-readable way. However, working with XML data can be challenging, especially when dealing with large datasets. In this tutorial, we will learn how to import XML data from an XML document into a SQL Server database using Python.<\/p>\n\n\n\n<p>SQL Server is a popular relational database management system used to store and manage data in organizations. Python is a powerful programming language with libraries that allow us to easily work with XML data. By combining these two technologies, we can effectively manage large datasets in a SQL Server database using Python.<\/p>\n\n\n\n<p>In this tutorial, we will cover <strong>step-by-step how to import XML data from an XML document into a SQL Server database using Python<\/strong>. We will use the pyodbc library to connect to the SQL Server database, the ElementTree module to parse the XML data, and SQL statements to insert the data into the database.<\/p>\n\n\n\n<p>By the end of this tutorial, you will have a clear understanding of how to import XML data with a simple schema into a SQL Server XML column using Python. Allowing you to effectively manage large datasets and perform complex queries on the data.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 ez-toc-wrap-center counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/expert-only.com\/en\/python\/import-xml-document-in-sql-server-python\/#1-prepare-the-xml-document-to-import\" >1. Prepare the XML document to import<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/expert-only.com\/en\/python\/import-xml-document-in-sql-server-python\/#2-create-the-ms-sql-table-with-an-xml-type-column\" >2. Create the MS SQL table with an XML type column<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/expert-only.com\/en\/python\/import-xml-document-in-sql-server-python\/#3-import-the-2-necessary-python-libraries\" >3. Import the 2 necessary Python libraries<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/expert-only.com\/en\/python\/import-xml-document-in-sql-server-python\/#4-set-up-the-connection-to-the-sql-server-database\" >4. Set up the connection to the SQL Server database<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/expert-only.com\/en\/python\/import-xml-document-in-sql-server-python\/#5-open-the-xml-file-and-get-the-root-element\" >5. Open the XML file and get the root element<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/expert-only.com\/en\/python\/import-xml-document-in-sql-server-python\/#6-loop-through-xml-elements-and-insert-data-into-sql-server\" >6. Loop through XML elements and insert data into SQL Server<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/expert-only.com\/en\/python\/import-xml-document-in-sql-server-python\/#7-close-the-database-connection-to-sql-server\" >7. Close the database connection to SQL Server<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/expert-only.com\/en\/python\/import-xml-document-in-sql-server-python\/#8-complete-python-example-to-import-the-xml-file-into-ms-sql\" >8. Complete Python example to import the XML file into MS SQL<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/expert-only.com\/en\/python\/import-xml-document-in-sql-server-python\/#9-conclusion-on-importing-xml-data-into-a-sql-server-column\" >9. Conclusion on importing XML data into a SQL Server column<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"1-prepare-the-xml-document-to-import\"><\/span>1. Prepare the XML document to import<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Let&#8217;s say we need to import an XML document into a SQL Server table using only Python code. Or at least, calling the SQL code directly from the Python scripts. Let&#8217;s consider this XML document, it corresponds to the employees table used as an example previously. We used it in the other tutorial on <a href=\"https:\/\/expert-only.com\/en\/python\/sql-server-xml-column-to-document-python\/\">how to export an XML column into an XML documents in Python<\/a>.  <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"xml\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?>\n&lt;employees>\n  &lt;employee>\n    &lt;id>1&lt;\/id>\n    &lt;name>John Smith&lt;\/name>\n    &lt;department>Sales&lt;\/department>\n    &lt;salary>50000&lt;\/salary>\n  &lt;\/employee>\n  &lt;employee>\n    &lt;id>2&lt;\/id>\n    &lt;name>Jane Doe&lt;\/name>\n    &lt;department>Marketing&lt;\/department>\n    &lt;salary>60000&lt;\/salary>\n  &lt;\/employee>\n  &lt;employee>\n    &lt;id>3&lt;\/id>\n    &lt;name>Bob Johnson&lt;\/name>\n    &lt;department>IT&lt;\/department>\n    &lt;salary>70000&lt;\/salary>\n  &lt;\/employee>\n  &lt;employee>\n    &lt;id>4&lt;\/id>\n    &lt;name>Alice Wong&lt;\/name>\n    &lt;department>HR&lt;\/department>\n    &lt;salary>55000&lt;\/salary>\n  &lt;\/employee>\n&lt;\/employees>\n<\/pre>\n\n\n\n<p>Copy, paste and save the document into an XML file, called <strong><em>employees.xml<\/em><\/strong>. In this tutorial, the file is stored in this path C:\\data\\employees.xml. To do this I use <a href=\"https:\/\/notepad-plus-plus.org\/downloads\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Notepad++<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"2-create-the-ms-sql-table-with-an-xml-type-column\"><\/span>2. Create the MS SQL table with an XML type column<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In order to import the XML data, you need the same <a href=\"https:\/\/schema.org\/docs\/schemas.html\" target=\"_blank\" rel=\"noreferrer noopener\">XML schema structure<\/a> as a target. To prepare the MS SQL table with an XML type column, we can use the same <strong><em>dbo.employees_xml<\/em><\/strong> table structure used in the previous tutorial. Here&#8217;s the SQL Server code how to create the table using the <em>Employee<\/em> schema. For this step, I recommend connecting to the database instance using SSMS and execute the code to create the table with the XML column using the schema below.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"xml\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">CREATE XML SCHEMA COLLECTION EmployeeSchema AS N'\n&lt;xsd:schema xmlns:xsd=\"http:\/\/www.w3.org\/2001\/XMLSchema\">\n  &lt;xsd:element name=\"Employee\">\n    &lt;xsd:complexType>\n      &lt;xsd:sequence>\n        &lt;xsd:element name=\"ID\" type=\"xsd:integer\"\/>\n        &lt;xsd:element name=\"Name\" type=\"xsd:string\"\/>\n        &lt;xsd:element name=\"Department\" type=\"xsd:string\"\/>\n        &lt;xsd:element name=\"Salary\" type=\"xsd:integer\"\/>\n      &lt;\/xsd:sequence>\n    &lt;\/xsd:complexType>\n  &lt;\/xsd:element>\n&lt;\/xsd:schema>';\nGO\n\nCREATE TABLE dbo.employees_xml (\n   id INT PRIMARY KEY,\n   employee_data xml(EmployeeSchema)\n);\n<\/pre>\n\n\n\n<p>This creates an <em>employees_xml<\/em> table with an ID column and a column named <em>employee_data<\/em> of type XML that uses the <em>EmployeeSchema<\/em> schema. The table is now ready to store XML data. This tutorial somehow explains the exact opposite of this one, using the same structure and database objects:<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-sql-and-it-tutorials wp-block-embed-sql-and-it-tutorials\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"yZpZyq0nKm\"><a href=\"https:\/\/expert-only.com\/en\/uncategorized\/sql-server-xml-column-to-document-python\/\">How to export SQL Server XML column into a document in Python ?<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;How to export SQL Server XML column into a document in Python ?&#8221; &#8212; SQL and IT Tutorials\" src=\"https:\/\/expert-only.com\/en\/uncategorized\/sql-server-xml-column-to-document-python\/embed\/#?secret=ToGbNO3u9g#?secret=yZpZyq0nKm\" data-secret=\"yZpZyq0nKm\" width=\"500\" height=\"282\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"3-import-the-2-necessary-python-libraries\"><\/span>3. Import the 2 necessary Python libraries<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Indeed, we need 2 libraries, the pyodbc and the Elementree one. This script below imports the pyodbc library for connecting to the SQL Server database and the ElementTree module for creating and writing the XML document.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import pyodbc\nimport xml.etree.ElementTree as ET\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"4-set-up-the-connection-to-the-sql-server-database\"><\/span>4. Set up the connection to the SQL Server database<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>This sets up the connection parameters and establishes a connection to the SQL Server database. We use here a Windows account to connect to the local database. Simply adjust the code to your technical constraints.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Set up the database connection\n# declare some variables to store the connections details\ndriver = 'SQL Server'\nserver = 'localhost'\ndatabase = 'Expert-Only'\n# connect to the local SQL Server database\nconnection = pyodbc.connect(f'DRIVER={driver};'\n                            f'SERVER={server};'\n                            f'DATABASE={database};'\n                            f'Trusted_Connection=yes;')\ncursor = connection.cursor()\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"5-open-the-xml-file-and-get-the-root-element\"><\/span>5. Open the XML file and get the root element<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Open the XML file\ntree = ET.parse('C:\\data\\employees.xml')\nroot = tree.getroot()\n<\/pre>\n\n\n\n<p>These lines use the <code>xml.etree.ElementTree<\/code> library to open an existing XML file <code>employees.xml<\/code>. The <code>parse()<\/code> method creates an <code>ElementTree<\/code> object <code>tree<\/code>, and <code>getroot()<\/code> returns the root element of the XML tree as an <code>Element<\/code> object <code>root<\/code>.<\/p>\n\n\n\n<p>If you are working with SSIS, it is also possible to performs <a href=\"https:\/\/expert-only.com\/en\/ssis\/import-xml-file-into-a-table-with-ssis\/\">XML documents import using Integration Services<\/a> packages.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-sql-and-it-tutorials wp-block-embed-sql-and-it-tutorials\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"LFdWa7B4f0\"><a href=\"https:\/\/expert-only.com\/en\/ssis\/import-xml-file-into-a-table-with-ssis\/\">Import an XML file into a SQL table with SSIS<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;Import an XML file into a SQL table with SSIS&#8221; &#8212; SQL and IT Tutorials\" src=\"https:\/\/expert-only.com\/en\/ssis\/import-xml-file-into-a-table-with-ssis\/embed\/#?secret=KMlT2vx3gU#?secret=LFdWa7B4f0\" data-secret=\"LFdWa7B4f0\" width=\"500\" height=\"282\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"6-loop-through-xml-elements-and-insert-data-into-sql-server\"><\/span>6. Loop through XML elements and insert data into SQL Server<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The lines of code below loop through each <code>employee<\/code> element in the XML file using the <code>findall()<\/code> method of the root element, called <code><em>root<\/em><\/code>. For each <code>employee<\/code> element, it retrieves the values of the <code>id<\/code>, <code>name<\/code>, <code>department<\/code>, and <code>salary<\/code> elements using the <code>find()<\/code> method, which returns the first matching element.<\/p>\n\n\n\n<p>Then the Python script it executes an SQL query to insert the data into the <strong><em>dbo.employees<\/em><\/strong> table using the <code>execute()<\/code> method of the cursor object. Finally, it commits the changes to the database using the <code>commit()<\/code> method, this time from the connection object.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Loop through the XML elements and insert the data into the SQL Server table\nfor employee in root.findall('employee'):\n    id = employee.find('id').text\n    name = employee.find('name').text\n    department = employee.find('department').text\n    salary = employee.find('salary').text\n\n    # Build the XML string\n    xml_string = \"&lt;Employee>&lt;ID>{0}&lt;\/ID>&lt;Name>{1}&lt;\/Name>&lt;Department>{2}&lt;\/Department>&lt;Salary>{3}&lt;\/Salary>&lt;\/Employee>\".format(id, name, department, salary)\n\n    # Insert the XML data into the SQL Server table\n    cursor.execute(\"INSERT INTO dbo.employees_xml (id, employee_data) VALUES (?, ?)\", (id, xml_string))\n\nconnection.commit()<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"7-close-the-database-connection-to-sql-server\"><\/span>7. Close the database connection to SQL Server<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">cursor.close()\nconnection.close()\n<\/pre>\n\n\n\n<p>This closes the cursor and connection objects to the SQL Server database.<\/p>\n\n\n\n<p>Overall, this code retrieves the XML data from a SQL Server table, parses the XML data using ElementTree, creates a new XML document with the parsed data, and writes the XML data to a file. In the next section of the tutorial, we wrap up all the steps together to perform all actions in one functional script. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"8-complete-python-example-to-import-the-xml-file-into-ms-sql\"><\/span>8. Complete Python example to import the XML file into MS SQL<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>As a wrap up, the full code performs this 4 actions: <\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Establishes a connection to a local SQL Server<\/strong> database using the pyodbc library and declares variables for the connection details.<\/li>\n\n\n\n<li><strong>The XML file is opened using the ElementTree module<\/strong> in Python and the XML elements are looped through to extract the data.<\/li>\n\n\n\n<li><strong>The extracted data is then inserted into the dbo.employees table<\/strong> in the SQL Server database using a SQL query and the pyodbc library.<\/li>\n\n\n\n<li>Finally, the <strong>database connection is closed<\/strong>.<\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import pyodbc\nimport xml.etree.ElementTree as ET\n\n# Set up the database connection\n# declare some variables to store the connections details\ndriver = 'SQL Server'\nserver = 'localhost'\ndatabase = 'Expert-Only'\n# connect to the local SQL Server database\nconnection = pyodbc.connect(f'DRIVER={driver};'\n                            f'SERVER={server};'\n                            f'DATABASE={database};'\n                            f'Trusted_Connection=yes;')\ncursor = connection.cursor()\n\n# Open the XML file\ntree = ET.parse('C:\\data\\employees.xml')\nroot = tree.getroot()\n\n# Loop through the XML elements and insert the data into the SQL Server table\nfor employee in root.findall('employee'):\n    id = employee.find('id').text\n    name = employee.find('name').text\n    department = employee.find('department').text\n    salary = employee.find('salary').text\n\n    # Build the XML string\n    xml_string = \"&lt;Employee>&lt;ID>{0}&lt;\/ID>&lt;Name>{1}&lt;\/Name>&lt;Department>{2}&lt;\/Department>&lt;Salary>{3}&lt;\/Salary>&lt;\/Employee>\".format(id, name, department, salary)\n\n    # Insert the XML data into the SQL Server table\n    cursor.execute(\"INSERT INTO dbo.employees_xml (id, employee_data) VALUES (?, ?)\", (id, xml_string))\n\nconnection.commit()\n\n# Close the database connection\ncursor.close()\nconnection.close()\n<\/pre>\n\n\n\n<p>After the execution of the code, to check the result, execute a select query in SSMS to display the XML data from the dbo.employees_xml table.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">select *\nfrom dbo.employees_xml\n<\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"720\" height=\"360\" src=\"https:\/\/expert-only.com\/wp-content\/uploads\/2023\/03\/python-import-xml-document-in-column-result-ssms.jpg\" alt=\"Select query in SSMS after the import of an XML document into SQL Server using Python.\" class=\"wp-image-23144\" srcset=\"https:\/\/expert-only.com\/wp-content\/uploads\/2023\/03\/python-import-xml-document-in-column-result-ssms.jpg 720w, https:\/\/expert-only.com\/wp-content\/uploads\/2023\/03\/python-import-xml-document-in-column-result-ssms-300x150.jpg 300w\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" \/><figcaption class=\"wp-element-caption\"><strong><em>Select query in SSMS after the import of an XML document into SQL Server using Python.<\/em><\/strong><\/figcaption><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-9-conclusion-on-importing-xml-data-into-a-sql-server-column\"><span class=\"ez-toc-section\" id=\"9-conclusion-on-importing-xml-data-into-a-sql-server-column\"><\/span>9. Conclusion on importing XML data into a SQL Server column<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Importing XML data from SQL Server into Python is a powerful capability that can enhance your programming projects in many ways. With the help of libraries such as pyodbc and ElementTree, Python provides an easy and efficient way of handling XML data. By following the steps outlined in this tutorial, you can import SQL Server XML column data into your Python project, opening up new possibilities for data manipulation and analysis.<\/p>\n\n\n\n<p>Whether you&#8217;re working on a data analytics project, building a web application, or developing a machine learning algorithm, the ability to seamlessly import XML data from SQL Server into Python can be a game-changer. By leveraging the powerful tools and libraries available in Python, you can streamline your development process and achieve your goals more efficiently.<\/p>\n\n\n\n<p>So why not give it a try? With the help of this tutorial and a little bit of practice, you&#8217;ll be able to import SQL Server XML data into your Python projects in no time!<\/p>\n\n\n\n<p>Very often, in data management projects involving large number of files, including Python projects. It is a good practice to zip files to reduce the size, to secure them using a password or simply to organize and archive them in a different folder. The tutorial below shows some <a href=\"https:\/\/expert-only.com\/en\/python\/zip-and-unzip-files-in-python\/\">Python scripts to zip and unzip files<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-sql-and-it-tutorials wp-block-embed-sql-and-it-tutorials\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"DBP0boAn8r\"><a href=\"https:\/\/expert-only.com\/en\/python\/zip-and-unzip-files-in-python\/\">How to zip and unzip files with the Python zipfile  module ?<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;How to zip and unzip files with the Python zipfile  module ?&#8221; &#8212; SQL and IT Tutorials\" src=\"https:\/\/expert-only.com\/en\/python\/zip-and-unzip-files-in-python\/embed\/#?secret=oJPPkm2H1i#?secret=DBP0boAn8r\" data-secret=\"DBP0boAn8r\" width=\"500\" height=\"282\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Data is a crucial element in any organization, and with the rapid growth of technology, the amount of data being generated is increasing exponentially. XML data is a popular format for representing structured data in a readable and machine-readable <a class=\"mh-excerpt-more\" href=\"https:\/\/expert-only.com\/en\/python\/import-xml-document-in-sql-server-python\/\" title=\"How to import an XML document into SQL Server using Python?\">&#8230;<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":10754,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[657],"tags":[],"class_list":{"0":"post-23101","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-python"},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.7 (Yoast SEO v26.2) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to import an XML document into SQL Server using Python?<\/title>\n<meta name=\"description\" content=\"Step by step tutorial on how to import an XML document into a SQL Server XML type column with a schema using Python.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/expert-only.com\/en\/python\/import-xml-document-in-sql-server-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to import an XML document into SQL Server using Python?\" \/>\n<meta property=\"og:description\" content=\"Step by step tutorial on how to import an XML document into a SQL Server XML type column with a schema using Python.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/expert-only.com\/en\/python\/import-xml-document-in-sql-server-python\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL and IT Tutorials\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ExpertOnlyCom\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-01-16T06:07:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-03-08T09:34:25+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/matrix-69E193580C5_1920x1080-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t<meta property=\"og:image:height\" content=\"1080\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Expert-Only\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@expert_only\" \/>\n<meta name=\"twitter:site\" content=\"@expert_only\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Expert-Only\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/expert-only.com\/en\/python\/import-xml-document-in-sql-server-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/expert-only.com\/en\/python\/import-xml-document-in-sql-server-python\/\"},\"author\":{\"name\":\"Expert-Only\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef\"},\"headline\":\"How to import an XML document into SQL Server using Python?\",\"datePublished\":\"2023-01-16T06:07:00+00:00\",\"dateModified\":\"2023-03-08T09:34:25+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/expert-only.com\/en\/python\/import-xml-document-in-sql-server-python\/\"},\"wordCount\":1182,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/expert-only.com\/en\/#organization\"},\"image\":{\"@id\":\"https:\/\/expert-only.com\/en\/python\/import-xml-document-in-sql-server-python\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/matrix-69E193580C5_1920x1080-1.jpg\",\"articleSection\":[\"Python tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/expert-only.com\/en\/python\/import-xml-document-in-sql-server-python\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/expert-only.com\/en\/python\/import-xml-document-in-sql-server-python\/\",\"url\":\"https:\/\/expert-only.com\/en\/python\/import-xml-document-in-sql-server-python\/\",\"name\":\"How to import an XML document into SQL Server using Python?\",\"isPartOf\":{\"@id\":\"https:\/\/expert-only.com\/en\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/expert-only.com\/en\/python\/import-xml-document-in-sql-server-python\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/expert-only.com\/en\/python\/import-xml-document-in-sql-server-python\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/matrix-69E193580C5_1920x1080-1.jpg\",\"datePublished\":\"2023-01-16T06:07:00+00:00\",\"dateModified\":\"2023-03-08T09:34:25+00:00\",\"description\":\"Step by step tutorial on how to import an XML document into a SQL Server XML type column with a schema using Python.\",\"breadcrumb\":{\"@id\":\"https:\/\/expert-only.com\/en\/python\/import-xml-document-in-sql-server-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/expert-only.com\/en\/python\/import-xml-document-in-sql-server-python\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/expert-only.com\/en\/python\/import-xml-document-in-sql-server-python\/#primaryimage\",\"url\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/matrix-69E193580C5_1920x1080-1.jpg\",\"contentUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/matrix-69E193580C5_1920x1080-1.jpg\",\"width\":1920,\"height\":1080},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/expert-only.com\/en\/python\/import-xml-document-in-sql-server-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"en\",\"item\":\"https:\/\/expert-only.com\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to import an XML document into SQL Server using Python?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/expert-only.com\/en\/#website\",\"url\":\"https:\/\/expert-only.com\/en\/\",\"name\":\"SQL and IT Tutorials\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/expert-only.com\/en\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/expert-only.com\/en\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/expert-only.com\/en\/#organization\",\"name\":\"Expert-Only\",\"url\":\"https:\/\/expert-only.com\/en\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg\",\"contentUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg\",\"width\":381,\"height\":174,\"caption\":\"Expert-Only\"},\"image\":{\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/ExpertOnlyCom\/\",\"https:\/\/x.com\/expert_only\",\"https:\/\/www.youtube.com\/channel\/UCMS5sR_FwAetB0FmciNvUaA\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef\",\"name\":\"Expert-Only\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g\",\"caption\":\"Expert-Only\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How to import an XML document into SQL Server using Python?","description":"Step by step tutorial on how to import an XML document into a SQL Server XML type column with a schema using Python.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/expert-only.com\/en\/python\/import-xml-document-in-sql-server-python\/","og_locale":"en_US","og_type":"article","og_title":"How to import an XML document into SQL Server using Python?","og_description":"Step by step tutorial on how to import an XML document into a SQL Server XML type column with a schema using Python.","og_url":"https:\/\/expert-only.com\/en\/python\/import-xml-document-in-sql-server-python\/","og_site_name":"SQL and IT Tutorials","article_publisher":"https:\/\/www.facebook.com\/ExpertOnlyCom\/","article_published_time":"2023-01-16T06:07:00+00:00","article_modified_time":"2023-03-08T09:34:25+00:00","og_image":[{"width":1920,"height":1080,"url":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/matrix-69E193580C5_1920x1080-1.jpg","type":"image\/jpeg"}],"author":"Expert-Only","twitter_card":"summary_large_image","twitter_creator":"@expert_only","twitter_site":"@expert_only","twitter_misc":{"Written by":"Expert-Only","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/expert-only.com\/en\/python\/import-xml-document-in-sql-server-python\/#article","isPartOf":{"@id":"https:\/\/expert-only.com\/en\/python\/import-xml-document-in-sql-server-python\/"},"author":{"name":"Expert-Only","@id":"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef"},"headline":"How to import an XML document into SQL Server using Python?","datePublished":"2023-01-16T06:07:00+00:00","dateModified":"2023-03-08T09:34:25+00:00","mainEntityOfPage":{"@id":"https:\/\/expert-only.com\/en\/python\/import-xml-document-in-sql-server-python\/"},"wordCount":1182,"commentCount":0,"publisher":{"@id":"https:\/\/expert-only.com\/en\/#organization"},"image":{"@id":"https:\/\/expert-only.com\/en\/python\/import-xml-document-in-sql-server-python\/#primaryimage"},"thumbnailUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/matrix-69E193580C5_1920x1080-1.jpg","articleSection":["Python tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/expert-only.com\/en\/python\/import-xml-document-in-sql-server-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/expert-only.com\/en\/python\/import-xml-document-in-sql-server-python\/","url":"https:\/\/expert-only.com\/en\/python\/import-xml-document-in-sql-server-python\/","name":"How to import an XML document into SQL Server using Python?","isPartOf":{"@id":"https:\/\/expert-only.com\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/expert-only.com\/en\/python\/import-xml-document-in-sql-server-python\/#primaryimage"},"image":{"@id":"https:\/\/expert-only.com\/en\/python\/import-xml-document-in-sql-server-python\/#primaryimage"},"thumbnailUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/matrix-69E193580C5_1920x1080-1.jpg","datePublished":"2023-01-16T06:07:00+00:00","dateModified":"2023-03-08T09:34:25+00:00","description":"Step by step tutorial on how to import an XML document into a SQL Server XML type column with a schema using Python.","breadcrumb":{"@id":"https:\/\/expert-only.com\/en\/python\/import-xml-document-in-sql-server-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/expert-only.com\/en\/python\/import-xml-document-in-sql-server-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/expert-only.com\/en\/python\/import-xml-document-in-sql-server-python\/#primaryimage","url":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/matrix-69E193580C5_1920x1080-1.jpg","contentUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/matrix-69E193580C5_1920x1080-1.jpg","width":1920,"height":1080},{"@type":"BreadcrumbList","@id":"https:\/\/expert-only.com\/en\/python\/import-xml-document-in-sql-server-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"en","item":"https:\/\/expert-only.com\/en\/"},{"@type":"ListItem","position":2,"name":"How to import an XML document into SQL Server using Python?"}]},{"@type":"WebSite","@id":"https:\/\/expert-only.com\/en\/#website","url":"https:\/\/expert-only.com\/en\/","name":"SQL and IT Tutorials","description":"","publisher":{"@id":"https:\/\/expert-only.com\/en\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/expert-only.com\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/expert-only.com\/en\/#organization","name":"Expert-Only","url":"https:\/\/expert-only.com\/en\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/","url":"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg","contentUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg","width":381,"height":174,"caption":"Expert-Only"},"image":{"@id":"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/ExpertOnlyCom\/","https:\/\/x.com\/expert_only","https:\/\/www.youtube.com\/channel\/UCMS5sR_FwAetB0FmciNvUaA"]},{"@type":"Person","@id":"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef","name":"Expert-Only","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/expert-only.com\/en\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g","caption":"Expert-Only"}}]}},"_links":{"self":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/23101","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/comments?post=23101"}],"version-history":[{"count":0,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/23101\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media\/10754"}],"wp:attachment":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media?parent=23101"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/categories?post=23101"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/tags?post=23101"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}