Python - Convert XML field from SQL SERVER into XML FILE using pyodbc
<p>I have the next code:</p>

<pre><code>import pypyodbc as pyodbc

cnxn = pyodbc.connect(
    'Trusted_Connection=yes;DRIVER={SQL Server};SERVER=localhost;DATABASE=HOLA;UID=sa;PWD=HOLAMUNDO'
)

idPerson = 2

cursor = cnxn.cursor()
cursor.execute('SELECT * FROM prueba WHERE id=?', (idPerson,))
for row in cursor.fetchall():
    print(row)
</code></pre>

<p>This works fine, and the selected XML field have the next format:</p>

<pre><code>&lt;PERSON&gt;
  &lt;INFO&gt;
    &lt;NAME&gt;Charlie&lt;/NAME&gt;
    &lt;LASTNAME&gt;Brown&lt;/LASTNAME&gt;
  &lt;/INFO&gt;
&lt;/PERSON&gt;
</code></pre>

<p>How can I take that field and save it in a new XML file in a directory...?</p>

<p>This is the script for my database:</p>

<pre><code>USE [HOLA]
GO
/****** Object:  Table [dbo].[prueba]    Script Date: 04/03/2016 8:29:30 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[prueba](
    [id] [int] NOT NULL,
    [xml] [xml] NULL,
 CONSTRAINT [PK_prueba] PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO
</code></pre>