XML to Scala Case Class Generator

Immutable case classes with List and Option, typed from your sample

XML Input

Scala Output

Paste your XML on the left and the Scala code shows up here.

Or drop a file on this box, , or .

What the generator writes

Give it an XML document and each element name comes back as a final case class. The constructor takes one parameter per attribute and child element, and a trailing comment such as // attribute "trackingNumber" or // <weightKg> records which part of the XML it maps. Case classes give you equality, copy and pattern matching for free.

A repeated element becomes List[T] = Nil, and a field that some records leave out becomes Option[T] = None; a type nested inside itself (categories inside categories) always gets Option. Leaf types follow the data: Int while every value fits 32 bits, Long past that, Double for decimals, Boolean for true/false. Names that collide with Scala keywords are backticked, which is why the sample's <package> elements come out as `package`: List[Package].

The classes describe the data; they do not parse it. With scala-xml you would map each class in a few lines ((node \ "weightKg").text.toDouble). The output compiles with Scala 2.13 and 3. It is generated in your browser and the XML is not uploaded.

How to use it

Paste, set the package, copy.

1

Paste a sample

Drop XML into XML Input, or Upload a file. A child that shows up once stays a single field, so include at least two copies of anything that should be a List. Sample loads a shipment with two packages:

<shipment id="SHP-5531" status="in_transit">
  <package trackingNumber="1Z999AA10123456784"><weightKg>2.4</weightKg></package>
  <package trackingNumber="1Z999AA10123456785"><weightKg>11.0</weightKg><fragile>true</fragile></package>
</shipment>

If the panel reports a parse error, the XML Validator shows the exact line.

2

Choose the package and Option handling

Root class overrides the outer case class name; blank means <shipment> becomes Shipment. Package writes the package line (keyword segments get backticks), and clearing it drops the line. With Option[T] for missing fields off, fragile turns into a plain Boolean with no default value.

3

Copy or download

Download saves a .scala file named after the root class. Copy puts it on the clipboard.

Where it helps

Data pipelines

Spark or Akka jobs that ingest XML exports get typed records to work with instead of passing NodeSeq around.

Integrations

Logistics, banking and healthcare partners still send XML. Case classes make the contract explicit in code review.

Tests and fixtures

Build expected values with case class constructors and compare them against what your parser produced.

Learning a new format

Generating classes from a sample is a fast way to see every field a document can carry, including the optional ones.

Common questions

Does it generate parsing code?

No, only case classes. Mapping XML onto them takes a few lines with scala-xml or whichever XML library you already use.

Which Scala versions does it work with?

The output uses only basic syntax: case classes, default parameter values, List and Option. It compiles with Scala 2.13 and Scala 3.

How are attributes handled?

Each attribute becomes a parameter, the same as a child element. If an element has attributes and text, the text becomes a value parameter.

How are types chosen?

Every value the field holds in your sample is checked. A tracking number like 1Z999AA10123456784 has letters in it, so it stays String; 2.4 and 11.0 make weightKg a Double; an integer outside the 32-bit range is typed Long. Zero-padded values such as 00742 are kept as String so the padding survives.

Why are some names in backticks?

XML names like type or class are Scala keywords. Backticks let you keep the same name without renaming the field.

Is the XML uploaded?

No. Scala source comes out of script running on this page, and your XML stays put. The editor declines anything above 256 KB, and a shipment or two shows the generator every field it needs.

Related tools

Other converters that pair well with this one: