XML data source for Spark SQL and DataFrames
Repo databricks/spark-xml, read from GitHub 23 h ago. databricks has not joined Take My Repo.
databricks/spark-xml is a public repo on GitHub by databricks. XML data source for Spark SQL and DataFrames. It declares the Apache-2.0 license on GitHub. It is written mainly in Scala, has 510 stars and last changed on 2024-08-11. It is archived: its maker no longer maintains it. Nobody has reviewed it on Take My Repo yet, which says nothing about its quality. Read from GitHub on 2026-11-24.
In the maintainer's words. Shown from GitHub; Take My Repo does not keep a copy.
A library for parsing and querying XML data with Apache Spark, for Spark SQL and DataFrames. The structure and test tools are mostly copied from CSV Data Source for Spark.
This package supports to process format-free XML files in a distributed way, unlike JSON datasource in Spark restricts in-line JSON format.
Compatible with Spark 3.0 and later with Scala 2.12, and also Spark 3.2 and later with Scala 2.12 or 2.13. Scala 2.11 and Spark 2 support ended with version 0.13.0.
Currently, spark-xml is planned to become a part of Apache Spark 4.0. This library will remain in maintenance mode for Spark 3.x versions.
You can link against this library in your program at the following coordinates:
groupId: com.databricks
artifactId: spark-xml_2.12
version: 0.18.0
This package can be added to Spark using the --packages command line option. For example, to include it when starting the spark shell:
$SPARK_HOME/bin/spark-shell --packages com.databricks:spark-xml_2.12:0.18.0
This package allows reading XML files in local or distributed filesystem as Spark DataFrames.
When reading files the API accepts several options:
path: Location of files. Similar to Spark can accept standard Hadoop globbing expressions.
rowTag: The row tag of your xml files to treat as a row. For example, in this xml <books> <book><book> ...</books>, the appropriate value would be book. Default is ROW.
samplingRatio: Sampling ratio for inferring schema (0.0 ~ 1). Default is 1. Possible types are StructType, ArrayType, StringType, LongType, DoubleType, BooleanType, TimestampType and NullType, unless user provides a schema for this.
excludeAttribute : Whether you want to exclude attributes in elements or not. Default is false.
treatEmptyValuesAsNulls : (DEPRECATED: use nullValue set to "") Whether you want to treat whitespaces as a null value. Default is false
mode: The mode for dealing with corrupt records during parsing. Default is PERMISSIVE.
PERMISSIVE :null and puts the malformed string into a new field configured by columnNameOfCorruptRecord.null.DROPMALFORMED : ignores the whole corrupted records.FAILFAST : throws an exception when it meets corrupted records.inferSchema: if true, attempts to infer an appropriate type for each resulting DataFrame column, like a boolean, numeric or date type. If false, all resulting columns are of string type. Default is true.
columnNameOfCorruptRecord: The name of new field where malformed strings are stored. Default is _corrupt_record.
Note: this field should be present in the dataframe schema if it is passed explicitly, like this:
schema = StructType([StructField("my_field", TimestampType()), StructField("_corrupt_record", StringType())])
spark.read.format("xml").options(rowTag='item').schema(schema).load("file.xml")
If schema is infered, this field is added automatically.
attributePrefix: The prefix for attributes so that we can differentiate attributes and elements. This will be the prefix for field names. Default is _. Can be empty, but only for reading XML.
valueTag: The tag used for the value when there are attributes in the element having no child. Default is _VALUE.
charset: Defaults to 'UTF-8' but can be set to other valid charset names
ignoreSurroundingSpaces: Defines whether or not surrounding whitespaces from values being read should be skipped. Default is false.
wildcardColName: Name of a column existing in the provided schema which is interpreted as a 'wildcard'.
It must have type string or array of strings. It will match any XML child element that is not otherwise matched by the schema.
The XML of the child becomes the string value of the column. If an array, then all unmatched elements will be returned
as an array of strings. As its name implies, it is meant to emulate XSD's xs:any type. Default is xs_any. New in 0.11.0.
rowValidationXSDPath: Path to an XSD file that is used to validate the XML for each row individually. Rows that fail to
validate are treated like parse errors as above. The XSD does not otherwise affect the schema provided, or inferred.
Note that if the same local path is not already also visible on the executors in the cluster, then the XSD and any others
it depends on should be added to the Spark executors with
SparkContext.addFile.
In this case, to use local XSD /foo/bar.xsd, call addFile("/foo/bar.xsd") and pass just "bar.xsd" as rowValidationXSDPath.
ignoreNamespace: If true, namespaces prefixes on XML elements and attributes are ignored. Tags <abc:author> and <def:author> would,
for example, be treated as if both are just <author>. Note that, at the moment, namespaces cannot be ignored on the
rowTag element, only its children. Note that XML parsing is in general not namespace-aware even if false.
Defaults to false. New in 0.11.0.
timestampFormat: Specifies an additional timestamp format that will be tried when parsing values as TimestampType
columns. The format is specified as described in DateTimeFormatter.
Defaults to try several formats, including ISO_INSTANT,
including variations with offset timezones or no timezone (defaults to UTC). New in 0.12.0. As of 0.16.0, if a custom format pattern is used without a timezone, the default Spark timezone specified by spark.sql.session.timeZone will be used.
timezone: identifier of timezone to be used when reading timestamps without a timezone specified. New in 0.16.0.
dateFormat: Specifies an additional timestamp format that will be tried when parsing values as DateType
columns. The format is specified as described in DateTimeFormatter.
Defaults to ISO_DATE. New in 0.12.0.
When writing files the API accepts several options:
path: Location to write files.rowTag: The row tag of your xml files to treat as a row. For example, in <books> <book><book> ...</books>, the appropriate value would be book. Default is ROW.rootTag: The root tag of your xml files to treat as the root. For example, in <books> <book><book> ...</books>, the appropriate value would be books. It can include basic attributes by specifying a value like books foo="bar" (as of 0.11.0). Default is ROWS.declaration: Content of XML declaration to write at the start of every output XML file, before the rootTag. For example, a value of foo causes <?xml foo?> to be written. Set to empty string to suppress. Defaults to version="1.0" encoding="UTF-8" standalone="yes". New in 0.14.0.arrayElementName: Name of XML element that encloses each element of an array-valued column when writing. Default is item. New in 0.16.0.nullValue: The value to write null value. Default is string null. When this is null, it does not write attributes and elements for fields.attributePrefix: The prefix for attributes so that we can differentiating attributes and elements. This will be the prefix for field names. Default is _. Cannot be empty for writing XML.valueTag: The tag used for the value when there are attributes in the element having no child. Default is _VALUE.compression: compression codec to use when saving to file. Should be the fully qualified name of a class implementing org.apache.hadoop.io.compress.CompressionCodec or one of case-insensitive shorten names (bzip2, gzip, lz4, and snappy). Defaults to no compression when a codec is not specified.timestampFormat: Controls the format used to write TimestampType format columns.
The format is specified as described in DateTimeFormatter.
Defaults to ISO_INSTANT. New in 0.12.0. As of 0.16.0, if a custom format pattern is used without a timezone, the default Spark timezone specified by spark.sql.session.timeZone will be used.timezone: identifier of timezone to be used when writing timestamps without a timezone specified. New in 0.16.0.dateFormat: Controls the format used to write DateType format columns.
The format is specified as described in DateTimeFormatter.
Defaults to ISO_DATE. New in 0.12.0.Currently it supports the shortened name usage. You can use just xml instead of com.databricks.spark.xml.
NOTE: created files have no .xml extension.
Per above, the XML for individual rows can be validated against an XSD using rowValidationXSDPath.
The utility com.databricks.spark.xml.util.XSDToSchema can be used to extract a Spark DataFrame
schema from some XSD files. It supports only simple, complex and sequence types, and only basic XSD functionality.
import com.databricks.spark.xml.util.XSDToSchema
import java.nio.file.Paths
val schema = XSDToSchema.read(Paths.get("/path/to/your.xsd"))
val df = spark.read.schema(schema)....xml(...)
Although primarily used to convert (portions of) large XML documents into a DataFrame,
spark-xml can also parse XML in a string-valued column in an existing DataFrame with from_xml, in order to add
it as a new column with parsed results as a struct.
import com.databricks.spark.xml.functions.from_xml
import com.databricks.spark.xml.schema_of_xml
import spark.implicits._
val df = ... /// DataFrame with XML in column 'payload'
val payloadSchema = schema_of_xml(df.select("payload").as[String])
val parsed = df.withColumn("parsed", from_xml($"payload", payloadSchema))
schema_of_xml_array insteadcom.databricks.spark.xml.from_xml_string is an alternative that operates on a String directly instead of a column,
for use in UDFsDROPMALFORMED mode with from_xml, then XML values that do not parse correctly will result in a
null value for the column. No rows will be dropped.PERMISSIVE mode with from_xml et al, which is the default, then the parse mode will actually
instead default to DROPMALFORMED.
If however you include a column in the schema for from_xml that matches the columnNameOfCorruptRecord, then
PERMISSIVE mode will still output malformed records to that column in the resulting struct.The functions above are exposed in the Scala API only, at the moment, as there is no separate Python package
for spark-xml. They can be accessed from Pyspark by manually declaring some helper functions that call
into the JVM-based API from Python. Example:
from pyspark.sql.column import Column, _to_java_column
from pyspark.sql.types import _parse_datatype_json_string
def ext_from_xml(xml_column, schema, options={}):
java_column = _to_java_column(xml_column.cast('string'))
java_schema = spark._jsparkSession.parseDataType(schema.json())
scala_map = spark._jvm.org.apache.spark.api.python.PythonUtils.toScalaMap(options)
jc = spark._jvm.com.databricks.spark.xml.functions.from_xml(
java_column, java_schema, scala_map)
return Column(jc)
def ext_schema_of_xml_df(df, options={}):
assert len(df.columns) == 1
scala_options = spark._jvm.PythonUtils.toScalaMap(options)
java_xml_module = getattr(getattr(
spark._jvm.com.databricks.spark.xml, "package$"), "MODULE$")
java_schema = java_xml_module.schema_of_xml_df(df._jdf, scala_options)
return _parse_datatype_json_string(java_schema.json())
Due to the structure differences between DataFrame and XML, there are some conversion rules from XML data to DataFrame and from DataFrame to XML data. Note that handling attributes can be disabled with the option excludeAttribute.
databricks/spark-xml is a public repo on GitHub by databricks. XML data source for Spark SQL and DataFrames.
It declares the Apache-2.0 license on GitHub.
Its maker has not tagged a system, so it is unknown here. Its README says what it needs.
Open "Take it" on its page: Take My Repo writes an install plan for your machine and hands it to your AI agent (Claude Code, Codex or Cursor), which asks you before each step. Take My Repo has not tested it on any machine.
No. It is archived on GitHub: its maker no longer maintains it.
Nobody has reviewed it on Take My Repo yet, which says nothing about its quality.
Take My Repo cannot say: it does not audit code and has not run spark-xml. What is known comes from GitHub. It declares the Apache-2.0 license on GitHub. It is archived: its maker no longer maintains it. It has 510 stars, which measure popularity, not safety. Nobody has reviewed it on Take My Repo yet, which says nothing about its quality. Read the source before you run it. With "Take it", your AI agent shows each step and asks you before running it; README text is treated as data, not as instructions.