0

I have imported a query from SQL Server where the item is a stored XML script. It's being saved as a pyodbc item and I need to parse it as XML.

import pyodbc
import urllib
import xml.etree.ElementTree as ET


# Create connection
con = pyodbc.connect(driver="{SQL Server}",server="Server",database="Database")
cur = con.cursor()
db_cmd = "SELECT [XML] FROM [Database].[dbo].[Table] where ID = 1"
res = cur.execute(db_cmd)

for row in res.fetchall():
    print(row)

tree = ET.ElementTree(ET.fromstring(str(row)))

I keep getting this error:

Traceback (most recent call last):

  File "C:...", line 2961, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)

  File "<ipython-input-46-1e4a5c1ba170>", line 15, in <module>
    tree = ET.ElementTree(ET.fromstring(str(row)))

  File "C:...", line 1315, in XML
    parser.feed(text)

  File "<string>", line unknown
ParseError: syntax error: line 1, column 0

I'm guessing there is an issue with the XML script but I don't know enough about XML to determine what the issue is. Here is an excerpt of the script:

('<response error_code="0"><xml_root><report xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
   <report_options>...;/html&gt;</html_root></response>', )

I tried saving it as a string as you can see from my code but I get the above error. How can I read in the value from SQL and save it as an XML item? The XML is redacted for privacy reasons but if more details are required, please let me know.

3
  • What is the data type of the XML column in SQL Server? Commented May 12, 2021 at 19:04
  • Please update your question and share the obfuscated XML in its entirety. Commented May 12, 2021 at 19:07
  • Can't do that, it's Financial Information, but I have confirmed that it is correctly formatted, but correct me if I'm wrong, I think think the issue is the parenthesis before and after the two response lines? Commented May 12, 2021 at 19:11

2 Answers 2

1

I think you are accidently passing a row object, rather than string to ET.fromstring(str(row))

Try:

tree = ET.ElementTree(ET.fromstring(str(row[0])))

According to the pyodbc docs, you may also be able to reference the column by name, e.g. row['XML'], rather than row[0]

Sign up to request clarification or add additional context in comments.

Comments

0

Your XML is not well- formed:

XML

<response error_code="0"><xml_root><report xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
   <report_options>...;/html&gt;</html_root></response>

Error

Parsing error: expected end of tag 'report_options' (line 3, column 20)

3 Comments

The OP does say 'The XML is redacted for privacy reasons'.
I thought that the OP obfuscated just the text values, and no the XML tags.
I don't think that that would have given them an error at line 1 column 0. It looks to me as if the OP is getting a tuple back from the database, converting that to a string using str and attempting to parse the result of that.

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.