I create table in PostgreSQL and insert to table nested XML(xml_part column):
CREATE TABLE text_xml (
id numeric,
xml_part XML
);
insert into text_xml
values (1, '<note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Dont forget me this weekend!</body></note>');
insert into text_xml
values (2, '<note><to>Tove</to><from>Alex</from><heading>Reminder</heading><body>Dont forget me this weekend!</body></note>');
Further, I try to read the table in Python with sqlalchemy and pandas:
from sqlalchemy import create_engine
import pandas as pds
# Create an engine instance
alchemyEngine = create_engine('postgresql+psycopg2://<credentials>', pool_recycle=3600)
# Connect to PostgreSQL server
dbConnection = alchemyEngine.connect()
# Read data from PostgreSQL database table and load into a DataFrame instance
dataFrame = pds.read_sql("select * from text_xml", dbConnection)
print(dataFrame.to_xml(index=False))
and I got in print:
<?xml version='1.0' encoding='utf-8'?>
<data>
<row>
<id>2.0</id>
<xml_part><note><to>Tove</to><from>Alex</from><heading>Reminder</heading><body>Dont forget me this weekend!</body></note></xml_part>
</row>
<row>
<id>1.0</id>
<xml_part><note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Dont forget me this weekend!</body></note></xml_part>
</row>
</data>
HTML symbols < and >
ruin my result. I would like to receive without HTML characters like this:
<?xml version='1.0' encoding='utf-8'?>
<data>
<row>
<id>2.0</id>
<xml_part><note><to>Tove</to><from>Alex</from><heading>Reminder</heading><body>Dont forget me this weekend!</body></note></xml_part>
</row>
<row>
<id>1.0</id>
<xml_part><note><to>Tove</to><from>Alex</from><heading>Reminder</heading><body>Dont forget me this weekend!</body></note></xml_part>
</row>
</data>
What am I doing wrong?
2
Answers
You can create elements from string and insert this into the tree again:
Output:
I can’t reproduce this behavier with postgres V16 on win 10. Maybe a sqlalchemy issue?
I created a table:
I insert your xml string:
My query is like this:
And I get this output: