pyspark.sql.functions.to_xml#
- pyspark.sql.functions.to_xml(col, options=None)[source]#
Converts a column containing a
StructType
into a XML string. Throws an exception, in the case of an unsupported type.New in version 4.0.0.
- Parameters
- col
Column
or str name of column containing a struct.
- options: dict, optional
options to control converting. accepts the same options as the XML datasource. See Data Source Option for the version you use.
- col
- Returns
Column
a XML string converted from given
StructType
.
Examples
>>> from pyspark.sql import Row >>> data = [(1, Row(age=2, name='Alice'))] >>> df = spark.createDataFrame(data, ("key", "value")) >>> df.select(to_xml(df.value, {'rowTag':'person'}).alias("xml")).collect() [Row(xml='<person>\n <age>2</age>\n <name>Alice</name>\n</person>')]