XML to Scala Converter - Generate Scala Case Classes from XML
Turn an XML document into Scala case classes with a scala-xml fromXml method and Play JSON formats.
How to Convert XML to Scala Case Classes
Paste or Upload Your XML
Paste XML into the left editor, upload an .xml or .txt file, or press Sample to load a worked example.
Example XML
<?xml version="1.0" encoding="UTF-8"?>
<microservice id="SVC-2024-001" version="3.2.1">
<service-info>
<name>UserAuthService</name>
<scala-version>2.13.10</scala-version>
<is-active>true</is-active>
</service-info>
<database>
<type>PostgreSQL</type>
<port>5432</port>
</database>
</microservice>Let It Read the Structure
The Scala output updates as you type. Every element and attribute is looked at, including all copies of repeated elements. Broken XML shows the line and column of the first problem instead of code.
Check the Generated Case Classes
Look over the case classes, field names and types. The original XML names are used in fromXml, so the Scala names can follow Scala conventions.
Scala output for the example
The file also starts with a few comment lines, the imports and the XmlRead helper object; the case classes follow:
case class Root(
id: Option[String] = None, // attribute id
version: Option[String] = None, // attribute version
serviceInfo: Option[ServiceInfo] = None,
database: Option[Database] = None
)
object Root {
implicit lazy val format: OFormat[Root] = Json.format[Root]
def fromXml(node: Node): Root =
Root(
id = XmlRead.attr(node, "id", ""),
version = XmlRead.attr(node, "version", ""),
serviceInfo = XmlRead.children(node, "service-info", "").headOption.map(ServiceInfo.fromXml),
database = XmlRead.children(node, "database", "").headOption.map(Database.fromXml)
)
}
case class ServiceInfo(
name: Option[String] = None,
scalaVersion: Option[String] = None,
isActive: Option[Boolean] = None
)
object ServiceInfo {
implicit lazy val format: OFormat[ServiceInfo] = Json.format[ServiceInfo]
def fromXml(node: Node): ServiceInfo =
ServiceInfo(
name = XmlRead.children(node, "name", "").headOption.map(n => XmlRead.text(n)),
scalaVersion = XmlRead.children(node, "scala-version", "").headOption.map(n => XmlRead.text(n)),
isActive = XmlRead.children(node, "is-active", "").headOption.map(n => XmlRead.text(n).toBoolean)
)
}
case class Database(
`type`: Option[String] = None,
port: Option[Int] = None
)
object Database {
implicit lazy val format: OFormat[Database] = Json.format[Database]
def fromXml(node: Node): Database =
Database(
`type` = XmlRead.children(node, "type", "").headOption.map(n => XmlRead.text(n)),
port = XmlRead.children(node, "port", "").headOption.map(n => XmlRead.text(n).toInt)
)
}Copy or Download the Code
Copy the code or download it as XmlModels.scala, add scala-xml and play-json to your sbt build, and load documents with Root.fromXml.
What is XML to Scala Conversion?
It means writing Scala types that match the shape of an XML document, so you can work with typed fields instead of walking nodes by hand. Doing that by hand gets tedious once a document has a few levels of nesting and some repeated elements.
This converter looks at the whole document, merges every copy of each element, and writes a case class for each element that has children or attributes. Each companion object gets a fromXml method that reads the document with scala-xml, plus a Play JSON format if you also want to send the data on as JSON.
Frequently Asked Questions
What does the generated Scala code contain?
One case class per XML element that has child elements or attributes, plus a companion object with a fromXml method and an implicit Play JSON OFormat. A small XmlRead helper object at the top does the scala-xml lookups. Every field is an Option with a default of None, and repeated elements become a List with a default of Nil, so a document that leaves something out still loads.
How are the field types chosen?
From the text of every value the element has in your document. Whole numbers that fit 32 bits become Int, bigger ones up to 64 bits become Long (so an ID like 8901240544102066246 keeps every digit), numbers with a decimal point become Double, and only the exact words true and false become Boolean. Values with leading zeros such as 00501, hex like 0x1F, numbers past 64 bits and elements that are sometimes empty stay String, because converting them would change or lose data.
How do I read an XML document with these classes?
Add scala-xml 2.x to your build, then call Root.fromXml(scala.xml.XML.loadString(xmlText)), or XML.loadFile for a file. fromXml finds child elements by local name and namespace, reads attributes, and converts the text with toInt, toLong, toDouble or toBoolean where the type says so. The generated code was checked with Scala 2.13, scala-xml 2.3.0 and play-json 3.0.4.
What happens with repeated elements and different copies of the same element?
All copies of an element name are merged before any code is written. A field that only appears in the second <endpoint> still makes it into the class, and if a value is a number in one copy and text in another, the field becomes a String. When an element appears more than once under the same parent, its field becomes a List.
How are XML names turned into Scala names?
Class names use PascalCase and field names camelCase, so service-info becomes ServiceInfo and serviceInfo. Scala keywords like type, class or val are wrapped in backticks, names starting with a digit get a field prefix, names a case class already uses for itself (copy, toString, hashCode) get a Value suffix, and clashes get a number. The original XML name stays in the fromXml code, so lookups still match the document.
Is my XML uploaded anywhere?
No. The conversion runs in your browser, and the XML you paste or upload is not sent to our server.