# Create XML files from a dataset

**URL:** <https://community.palantir.com/t/create-xml-files-from-a-dataset/2016>\
**Category:** Ask the Community\
**Tags:** code-repositories\
**Created:** [November 26, 2024, 1:18pm UTC](https://community.palantir.com/t/create-xml-files-from-a-dataset/2016 "2024-11-26T13:18:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Praveen\_Sharma](https://avatars.discourse-cdn.com/v4/letter/p/b9e5f3/32.png) [@Praveen\_Sharma](https://community.palantir.com/u/Praveen_Sharma)\
**Post date:** [November 26, 2024, 1:18pm UTC](https://community.palantir.com/t/create-xml-files-from-a-dataset/2016/1 "2024-11-26T13:18:00Z")

</div>

I have a requirement of transforming a dataset into XML files given a complex XSD. I am trying to use spark-xml for doing this task. However, I get an error “No module named ‘com’ found” even if conda jar “com.databricks.spark.xml” is included in the build.gradle file. If someone has any idea regarding this, could you please guide me? Also, please let me know if you have any alternate approaches to achieve this in Palantir foundry. Thanks in advance.

---

<div class="post-metadata">

**Author:** ![redboyben](https://avatars.discourse-cdn.com/v4/letter/r/96bed5/32.png) [@redboyben](https://community.palantir.com/u/redboyben)\
**Post date:** [November 26, 2024, 3:31pm UTC](https://community.palantir.com/t/create-xml-files-from-a-dataset/2016/2 "2024-11-26T15:31:45Z")

</div>

Just in case, could you review how you’re supposed to add a jar in build.gradle to make sure there’s nothing missing there? https://www.palantir.com/docs/foundry/transforms-python/environment-troubleshooting#packages-which-require-both-a-conda-package-and-a-jar

It could be another option write the files manually but this might not be adequate to your use case depending on scale.

```auto
from transforms.api import transform, Output
import xml.etree.ElementTree as ET

@transform(
    output=Output("output_dataset_path")
)
def write_xml(ctx, output):
    # Create an XML structure based on the XSD
    root = ET.Element("RootElement") # Replace with your root element name from the XSD
    child = ET.SubElement(root, "ChildElement") # Replace with your child element name
    child.set("attributeName", "attributeValue") # Set attributes as defined in the XSD
    child.text = "Element Text" # Set text content if required

    # Convert the XML structure to a string
    xml_str = ET.tostring(root, encoding='utf8', method='xml').decode()

    # Write the XML string to a file in the output filesystem
    output_fs = output.filesystem()
    with output_fs.open('output.xml', 'w') as f:
        f.write(xml_str)

```

---

<div class="post-metadata">

**Author:** ![sandpiper](https://yyz1.discourse-cdn.com/palantir/user_avatar/community.palantir.com/sandpiper/32/273_2.png) [@sandpiper](https://community.palantir.com/u/sandpiper)\
**Post date:** [December 2, 2024, 7:31am UTC](https://community.palantir.com/t/create-xml-files-from-a-dataset/2016/3 "2024-12-02T07:31:48Z")

</div>

> [@redboyben](#):
>
> Just in case, could you review how you’re supposed to add a jar in build.gradle to make sure there’s nothing missing there? https://www.palantir.com/docs/foundry/transforms-python/environment-troubleshooting#packages-which-require-both-a-conda-package-and-a-jar

I confirmed in my own environment that it is indeed possible to use spark-xml in Python transforms to output XML files to a dataset, so as suggested above, you should double-check your build.gradle (and make sure that you are editing `transforms-python/build.gradle`, not the top-level `build.gradle`). For reference, the below is the dependencies block at the bottom of `transforms-python/build.gradle` in my environment:

```auto
dependencies {
    condaJars 'com.databricks:spark-xml_2.13:0.18.0'
}

```

And both of the following code samples worked without issue:

```auto
output.write_dataframe(df, output_format="com.databricks.spark.xml")

```

```auto
output.write_dataframe(df, output_format="xml")

```

---

<div class="post-metadata">

**Author:** ![system](https://yyz1.discourse-cdn.com/palantir/user_avatar/community.palantir.com/system/32/3_2.png) [@system](https://community.palantir.com/u/system)\
**Post date:** [January 31, 2025, 7:32am UTC](https://community.palantir.com/t/create-xml-files-from-a-dataset/2016/4 "2025-01-31T07:32:30Z")

</div>

Closed on January 31, 2025, 7:32am UTC

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
