Kamil Kaletka
Kamil Kaletka

Reputation: 21

Adding CDATA to XML fields

How to add CDATA to all generated fields in python from xlsx to xml?

Code looks like:

from lxml import etree as et

raw_data = pd.read_excel(r'path_to_file')
root = et.Element('document')


for row in raw_data.iterrows():
    root_tags = et.SubElement(root, 'root')  
    # These are the tag names for each row 
    Column_heading_1 = et.SubElement(root_tags, 'sku')
    Column_heading_2 = et.SubElement(root_tags, 'product_url')

    # The values inside the [] are the raw file column headings.
    Column_heading_1.text = str(row[1]['sku'])
    Column_heading_2.text = str(row[1]['product_url'])

tree = et.ElementTree(root)
et.indent(tree, space="\t", level=0)
tree.write('output.xml', encoding="utf-8")

and in output I need:

<document>
            <root>
                <sku><![CDATA[Z.18181.16158141231205807]]></sku>
                <product_url><![CDATA[https://link.test]]></product_url>
            </root>
            <root>
                <sku><![CDATA[Z.18181.16158141231205807]]></sku>
                <product_url><![CDATA[https://link.test]]></product_url>
            </root>
</document>

Upvotes: 0

Views: 2436

Answers (1)

Kamil Kaletka
Kamil Kaletka

Reputation: 21

The answer is to add CDATA, e.g.:

Column_heading_1.text = et.CDATA(str(row[1]['sku']))

Upvotes: 1

Related Questions