{"id":23076,"date":"2023-01-09T06:28:00","date_gmt":"2023-01-09T05:28:00","guid":{"rendered":"https:\/\/expert-only.com\/?p=23076"},"modified":"2023-03-08T11:52:32","modified_gmt":"2023-03-08T10:52:32","slug":"sql-server-xml-column-to-document-python","status":"publish","type":"post","link":"https:\/\/expert-only.com\/en\/uncategorized\/sql-server-xml-column-to-document-python\/","title":{"rendered":"How to export SQL Server XML column into a document in Python ?"},"content":{"rendered":"\n<h4 class=\"has-text-align-center wp-block-heading\">Python code to read the data from a SQL Server XML column stored in a table and export it to an XML document.<\/h4>\n\n\n\n<p>Step-by-step tutorial on how to export SQL Server XML column into a document in Python. To create an XML table in SQL Server, we first define the table schema using an XML schema collection. The data from the original table is then inserted into the new XML table. Using the pyodbc library, we establish a connection to the SQL Server database and retrieve the XML data from the XML type table.<\/p>\n\n\n\n<p>We create an XML document using the lxml module and loop through each row in the SQL Server table to create a new Element object. Finally, we write the XML data to a file using the tostring() method with the &#8216;pretty_print&#8217; and &#8216;encoding&#8217; options set to True and &#8216;utf-8&#8217; respectively. This is a simple process that allows you to easily export XML data from SQL Server to other systems that support XML.<\/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\/uncategorized\/sql-server-xml-column-to-document-python\/#step-1-create-the-xml-column-in-t-sql\" >Step 1: Create the XML column in T-SQL<\/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\/uncategorized\/sql-server-xml-column-to-document-python\/#step-2-connect-to-sql-server-database\" >Step 2: Connect to SQL Server Database<\/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\/uncategorized\/sql-server-xml-column-to-document-python\/#step-3-retrieve-the-xml-data-from-the-column\" >Step 3: Retrieve the XML Data from the column<\/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\/uncategorized\/sql-server-xml-column-to-document-python\/#step-4-convert-the-xml-data-into-an-xml-document\" >Step 4: Convert the XML Data into an XML Document<\/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\/uncategorized\/sql-server-xml-column-to-document-python\/#step-5-write-data-to-the-xml-document\" >Step 5: Write data to the XML document<\/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\/uncategorized\/sql-server-xml-column-to-document-python\/#step-6-full-python-code-to-wrap-up-all-the-steps\" >Step 6: Full Python code to wrap up all the steps<\/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\/uncategorized\/sql-server-xml-column-to-document-python\/#conclusion-on-exporting-xml-data-to-document-with-python\" >Conclusion on exporting XML data to document with Python<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"h-step-1-create-the-xml-column-in-t-sql\"><span class=\"ez-toc-section\" id=\"step-1-create-the-xml-column-in-t-sql\"><\/span>Step 1: Create the XML column in T-SQL<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>To create an XML table in SQL Server, we first need to define the table schema using an XML schema collection. Here&#8217;s an example of how to create an XML table for the &#8220;employees&#8221; data. This code creates an XML schema collection named <code>EmployeeSchema<\/code> which defines the structure of the XML data that will be stored in the <code>employee_data<\/code> column of the <code>employees_xml<\/code> table. The <code>CREATE TABLE<\/code> statement creates the <code>employees_xml<\/code> table with an <code>id<\/code> column and an <code>employee_data<\/code> column of type <code>xml<\/code> with the <code>EmployeeSchema<\/code> schema.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\" 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);<\/pre>\n\n\n\n<p>The schema with an XML formatting looks like this: <\/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;xsd:schema xmlns:xsd=\"http:\/\/www.w3.org\/2001\/XMLSchema\">\n\t&lt;xsd:element name=\"Employee\">\n\t\txsd:complexType\n\t\t\txsd:sequence\n\t\t\t\t&lt;xsd:element name=\"ID\" type=\"xsd:integer\"\/>\n\t\t\t\t&lt;xsd:element name=\"Name\" type=\"xsd:string\"\/>\n\t\t\t\t&lt;xsd:element name=\"Department\" type=\"xsd:string\"\/>\n\t\t\t\t&lt;xsd:element name=\"Salary\" type=\"xsd:integer\"\/>\n\t\t\t&lt;\/xsd:sequence>\n\t\t&lt;\/xsd:complexType>\n\t&lt;\/xsd:element>\n&lt;\/xsd:schema><\/pre>\n\n\n\n<p>Next, we can insert the data from the &#8220;employees&#8221; table into the new XML table. This SQL code inserts the data into the XML <a href=\"https:\/\/expert-only.com\/fr\/python\/se-connecter-a-sql-server-avec-python\/\">table<\/a>. Indeed, the <code>INSERT INTO<\/code> statement inserts the data from the <code>employees<\/code> table into the <code>employees_xml<\/code> table, transforming it into an XML format using the <code>FOR XML<\/code> clause and the <code>PATH<\/code> and <code>ROOT<\/code> keywords to structure the resulting <a href=\"https:\/\/schemas.isotc211.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">XML document<\/a>.<\/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=\"\">INSERT INTO dbo.employees_xml (id, employee_data)\nVALUES \n(1, '&lt;Employee>&lt;ID>1&lt;\/ID>&lt;Name>John Smith&lt;\/Name>&lt;Department>Sales&lt;\/Department>&lt;Salary>50000&lt;\/Salary>&lt;\/Employee>'),\n(2, '&lt;Employee>&lt;ID>2&lt;\/ID>&lt;Name>Jane Doe&lt;\/Name>&lt;Department>Marketing&lt;\/Department>&lt;Salary>60000&lt;\/Salary>&lt;\/Employee>'),\n(3, '&lt;Employee>&lt;ID>3&lt;\/ID>&lt;Name>Bob Johnson&lt;\/Name>&lt;Department>IT&lt;\/Department>&lt;Salary>70000&lt;\/Salary>&lt;\/Employee>'),\n(4, '&lt;Employee>&lt;ID>4&lt;\/ID>&lt;Name>Alice Wong&lt;\/Name>&lt;Department>HR&lt;\/Department>&lt;Salary>55000&lt;\/Salary>&lt;\/Employee>');\n<\/pre>\n\n\n\n<p>Now we have an XML table &#8220;employees_xml&#8221; that contains the same data as the original &#8220;employees&#8221; table. We can export this table into Python using the same method as before.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"step-2-connect-to-sql-server-database\"><\/span>Step 2: Connect to SQL Server Database<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>First, we need to establish a connection to the SQL Server database. We can use the <code>pyodbc<\/code> library to connect to the database using the following code:<\/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\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;')\n<\/pre>\n\n\n\n<p>Technically, in this tutoriel, we perform the exact opposite on this one on <a href=\"https:\/\/expert-only.com\/en\/python\/import-xml-document-in-sql-server-python\/\">how to import an XML document in a XML typed SQL Server column<\/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=\"kfIJ7p8SmW\"><a href=\"https:\/\/expert-only.com\/en\/python\/import-xml-document-in-sql-server-python\/\">How to import an XML document into SQL Server using 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 import an XML document into SQL Server using Python?&#8221; &#8212; SQL and IT Tutorials\" src=\"https:\/\/expert-only.com\/en\/python\/import-xml-document-in-sql-server-python\/embed\/#?secret=iUFmrnCacc#?secret=kfIJ7p8SmW\" data-secret=\"kfIJ7p8SmW\" 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=\"step-3-retrieve-the-xml-data-from-the-column\"><\/span>Step 3: Retrieve the XML Data from the column<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Next, we need to retrieve the XML data from the XML type table in SQL Server. We can use a SQL query to retrieve the data and store it in a variable as a string. <\/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\n\n# Execute SQL query to retrieve data\ncursor.execute('SELECT * FROM dbo.employees_xml')\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"step-4-convert-the-xml-data-into-an-xml-document\"><\/span>Step 4: Convert the XML Data into an XML Document<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>This code block creates an XML document using the ElementTree module of the lxml library. It starts by creating the root element of the document, which is an Element object with the tag name &#8220;employees&#8221;.<\/p>\n\n\n\n<p>The code then loops through each row in the result set of the SQL query executed using the cursor object. For each row, a new Element object is created with the tag name &#8220;employee&#8221;. This element is then added as a child to the root element of the XML document using the etree.SubElement() method.<\/p>\n\n\n\n<p>The code then uses the etree.SubElement() method again to create child elements for each of the four columns in the SQL Server table: &#8220;id&#8221;, &#8220;name&#8221;, &#8220;department&#8221;, and &#8220;salary&#8221;. The text values for these child elements are set using the data retrieved from the SQL Server table, with the exception of the &#8220;id&#8221; element, which is set to the id value of the current row using the str() function to convert it to a string.<\/p>\n\n\n\n<p>The &#8220;employee_data&#8221; variable contains the XML data retrieved from the &#8220;employee_data&#8221; column in the SQL Server table, which is in string format. The etree.fromstring() method is used to parse this string into an Element object, which can then be used to access the values of its child elements using the .find() method. The code sets the text values of the child elements for the &#8220;name&#8221;, &#8220;department&#8221;, and &#8220;salary&#8221; elements using the values retrieved from the parsed XML data.<\/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=\"\"># Create XML document\nroot = etree.Element('employees')\n\nfor row in cursor.fetchall():\n    employee_data = etree.fromstring(row.employee_data)\n    employee = etree.SubElement(root, 'employee')\n    etree.SubElement(employee, 'id').text = str(row.id)\n    etree.SubElement(employee, 'name').text = employee_data.find('Name').text\n    etree.SubElement(employee, 'department').text = employee_data.find('Department').text\n    etree.SubElement(employee, 'salary').text = employee_data.find('Salary').text\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"step-5-write-data-to-the-xml-document\"><\/span>Step 5: Write data to the XML document<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The last code block uses the <code>etree.tostring()<\/code> method to convert the root element into an XML string, which is then written to a file named &#8220;employees.xml&#8221;. The <code>pretty_print=True<\/code> argument adds line breaks and indentation to the output to make it more readable. The <code>encoding=str<\/code> argument specifies that the XML should be written as a string object rather than bytes.<\/p>\n\n\n\n<p>The <code>with<\/code> statement is used to open the file and automatically close it after the write operation is complete. The <code>open()<\/code> function takes two arguments: the file path\/name and the mode in which to open the file. In this case, we use the mode <code>w<\/code> to open the file for writing.<\/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=\"\"># Save XML document to file\ntree = ET.ElementTree(root)\ntree.write('employees.xml', encoding='utf-8', xml_declaration=True)<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"step-6-full-python-code-to-wrap-up-all-the-steps\"><\/span>Step 6: Full Python code to wrap up all the steps<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The complete code for exporting an XML type table from SQL Server into an XML document in Python is shown below. To export data from a SQL Server XML table to an XML document in Python, we need to perform the following steps, listed here in detail:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Import the required libraries, i.e., pyodbc and lxml.etree.<\/li>\n\n\n\n<li>Declare variables to store the connection details for the database.<\/li>\n\n\n\n<li>Establish a connection to the SQL Server database using the pyodbc library.<\/li>\n\n\n\n<li>Create a cursor object to execute SQL queries.<\/li>\n\n\n\n<li>Execute an SQL query to retrieve data from the &#8217;employees_xml&#8217; table.<\/li>\n\n\n\n<li>Create an XML document using the ElementTree module and assign the root element the tag name &#8217;employees&#8217;.<\/li>\n\n\n\n<li>Loop through each row in the queried SQL Server table, create a new &#8217;employee&#8217; Element object, and set child elements for each of the four columns with the corresponding data.<\/li>\n\n\n\n<li>Convert the XML document to a string and save it to a file with the file path &#8216;C:\\data\\employees.xml&#8217;.<\/li>\n\n\n\n<li>Close the database connection.<\/li>\n<\/ul>\n\n\n\n<p>This is a straightforward process that allows you to easily incorporate XML data into your Python projects or export it to other systems that require XML data.<\/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 lxml.etree as etree\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\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# Execute SQL query to retrieve data\ncursor.execute('SELECT * FROM dbo.employees_xml')\n\n# Create XML document\nroot = etree.Element('employees')\n\nfor row in cursor.fetchall():\n    employee_data = etree.fromstring(row.employee_data)\n    employee = etree.SubElement(root, 'employee')\n    etree.SubElement(employee, 'id').text = str(row.id)\n    etree.SubElement(employee, 'name').text = employee_data.find('Name').text\n    etree.SubElement(employee, 'department').text = employee_data.find('Department').text\n    etree.SubElement(employee, 'salary').text = employee_data.find('Salary').text\n\n# Save XML document to file\nxml_string = etree.tostring(root, pretty_print=True, encoding=str)\nwith open('C:\\data\\employees.xml', 'w') as f:\n    f.write(xml_string)\n\n# Close database connection\ncursor.close()\nconnection.close()\n<\/pre>\n\n\n\n<p>The XML document exported looks like, thanks to the indentation option used:<\/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;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<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"conclusion-on-exporting-xml-data-to-document-with-python\"><\/span>Conclusion on exporting XML data to document with Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In this Python tutorial, we have seen how to export a SQL Server XML typed column, with a basic schema, into an XML document using Python. We used the <code>pyodbc<\/code> library to connect to the SQL Server database and retrieve the XML data, and the <code>lxml<\/code> library to parse the XML data and write it to the XML file.<\/p>\n\n\n\n<p>Indeed, Python provides easy and efficients ways to work with XML data, so we can easily integrate our Python code with a SQL Server database and work with XML data. I really hope this article was helpful in understanding how to export XML documents directly from SQL Server columns with Python.<\/p>\n\n\n\n<p>If you have any questions or comments, feel free to leave them below as, we&#8217;ll come back to you as soon as possible. To go further, let&#8217;s learn how to <a href=\"https:\/\/expert-only.com\/en\/python\/export-sql-server-data-to-csv-in-python\/\">export a MS SQL table into a CSV file using Python<\/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\">\nhttps:\/\/expert-only.com\/en\/python\/export-sql-server-data-to-csv-in-python\/\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Python code to read the data from a SQL Server XML column stored in a table and export it to an XML document. Step-by-step tutorial on how to export SQL Server XML column into a document in Python. To <a class=\"mh-excerpt-more\" href=\"https:\/\/expert-only.com\/en\/uncategorized\/sql-server-xml-column-to-document-python\/\" title=\"How to export SQL Server XML column into a document in Python ?\">&#8230;<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":10837,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-23076","post","type-post","status-publish","format-standard","has-post-thumbnail"],"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 export SQL Server XML column into a document in Python?<\/title>\n<meta name=\"description\" content=\"Python full example of code to export SQL Server XML column into a document in Python, explained step by step.\" \/>\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\/uncategorized\/sql-server-xml-column-to-document-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to export SQL Server XML column into a document in Python ?\" \/>\n<meta property=\"og:description\" content=\"Python full example of code to export SQL Server XML column into a document in Python, explained step by step.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/expert-only.com\/en\/uncategorized\/sql-server-xml-column-to-document-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-09T05:28:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-03-08T10:52:32+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/programming-1857236_1920x1080.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\/uncategorized\/sql-server-xml-column-to-document-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/expert-only.com\/en\/uncategorized\/sql-server-xml-column-to-document-python\/\"},\"author\":{\"name\":\"Expert-Only\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef\"},\"headline\":\"How to export SQL Server XML column into a document in Python ?\",\"datePublished\":\"2023-01-09T05:28:00+00:00\",\"dateModified\":\"2023-03-08T10:52:32+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/expert-only.com\/en\/uncategorized\/sql-server-xml-column-to-document-python\/\"},\"wordCount\":1221,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/expert-only.com\/en\/#organization\"},\"image\":{\"@id\":\"https:\/\/expert-only.com\/en\/uncategorized\/sql-server-xml-column-to-document-python\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/programming-1857236_1920x1080.jpg\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/expert-only.com\/en\/uncategorized\/sql-server-xml-column-to-document-python\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/expert-only.com\/en\/uncategorized\/sql-server-xml-column-to-document-python\/\",\"url\":\"https:\/\/expert-only.com\/en\/uncategorized\/sql-server-xml-column-to-document-python\/\",\"name\":\"How to export SQL Server XML column into a document in Python?\",\"isPartOf\":{\"@id\":\"https:\/\/expert-only.com\/en\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/expert-only.com\/en\/uncategorized\/sql-server-xml-column-to-document-python\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/expert-only.com\/en\/uncategorized\/sql-server-xml-column-to-document-python\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/programming-1857236_1920x1080.jpg\",\"datePublished\":\"2023-01-09T05:28:00+00:00\",\"dateModified\":\"2023-03-08T10:52:32+00:00\",\"description\":\"Python full example of code to export SQL Server XML column into a document in Python, explained step by step.\",\"breadcrumb\":{\"@id\":\"https:\/\/expert-only.com\/en\/uncategorized\/sql-server-xml-column-to-document-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/expert-only.com\/en\/uncategorized\/sql-server-xml-column-to-document-python\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/expert-only.com\/en\/uncategorized\/sql-server-xml-column-to-document-python\/#primaryimage\",\"url\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/programming-1857236_1920x1080.jpg\",\"contentUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/programming-1857236_1920x1080.jpg\",\"width\":1920,\"height\":1080},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/expert-only.com\/en\/uncategorized\/sql-server-xml-column-to-document-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"en\",\"item\":\"https:\/\/expert-only.com\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to export SQL Server XML column into a document in 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 export SQL Server XML column into a document in Python?","description":"Python full example of code to export SQL Server XML column into a document in Python, explained step by step.","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\/uncategorized\/sql-server-xml-column-to-document-python\/","og_locale":"en_US","og_type":"article","og_title":"How to export SQL Server XML column into a document in Python ?","og_description":"Python full example of code to export SQL Server XML column into a document in Python, explained step by step.","og_url":"https:\/\/expert-only.com\/en\/uncategorized\/sql-server-xml-column-to-document-python\/","og_site_name":"SQL and IT Tutorials","article_publisher":"https:\/\/www.facebook.com\/ExpertOnlyCom\/","article_published_time":"2023-01-09T05:28:00+00:00","article_modified_time":"2023-03-08T10:52:32+00:00","og_image":[{"width":1920,"height":1080,"url":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/programming-1857236_1920x1080.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\/uncategorized\/sql-server-xml-column-to-document-python\/#article","isPartOf":{"@id":"https:\/\/expert-only.com\/en\/uncategorized\/sql-server-xml-column-to-document-python\/"},"author":{"name":"Expert-Only","@id":"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef"},"headline":"How to export SQL Server XML column into a document in Python ?","datePublished":"2023-01-09T05:28:00+00:00","dateModified":"2023-03-08T10:52:32+00:00","mainEntityOfPage":{"@id":"https:\/\/expert-only.com\/en\/uncategorized\/sql-server-xml-column-to-document-python\/"},"wordCount":1221,"commentCount":0,"publisher":{"@id":"https:\/\/expert-only.com\/en\/#organization"},"image":{"@id":"https:\/\/expert-only.com\/en\/uncategorized\/sql-server-xml-column-to-document-python\/#primaryimage"},"thumbnailUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/programming-1857236_1920x1080.jpg","inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/expert-only.com\/en\/uncategorized\/sql-server-xml-column-to-document-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/expert-only.com\/en\/uncategorized\/sql-server-xml-column-to-document-python\/","url":"https:\/\/expert-only.com\/en\/uncategorized\/sql-server-xml-column-to-document-python\/","name":"How to export SQL Server XML column into a document in Python?","isPartOf":{"@id":"https:\/\/expert-only.com\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/expert-only.com\/en\/uncategorized\/sql-server-xml-column-to-document-python\/#primaryimage"},"image":{"@id":"https:\/\/expert-only.com\/en\/uncategorized\/sql-server-xml-column-to-document-python\/#primaryimage"},"thumbnailUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/programming-1857236_1920x1080.jpg","datePublished":"2023-01-09T05:28:00+00:00","dateModified":"2023-03-08T10:52:32+00:00","description":"Python full example of code to export SQL Server XML column into a document in Python, explained step by step.","breadcrumb":{"@id":"https:\/\/expert-only.com\/en\/uncategorized\/sql-server-xml-column-to-document-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/expert-only.com\/en\/uncategorized\/sql-server-xml-column-to-document-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/expert-only.com\/en\/uncategorized\/sql-server-xml-column-to-document-python\/#primaryimage","url":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/programming-1857236_1920x1080.jpg","contentUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/programming-1857236_1920x1080.jpg","width":1920,"height":1080},{"@type":"BreadcrumbList","@id":"https:\/\/expert-only.com\/en\/uncategorized\/sql-server-xml-column-to-document-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"en","item":"https:\/\/expert-only.com\/en\/"},{"@type":"ListItem","position":2,"name":"How to export SQL Server XML column into a document in 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\/23076","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=23076"}],"version-history":[{"count":0,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/23076\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media\/10837"}],"wp:attachment":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media?parent=23076"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/categories?post=23076"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/tags?post=23076"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}