Edit XML in Scala in a FP way

geirolz

David Geirola

Posted on August 14, 2019

Edit XML in Scala in a FP way

Hi guys , i'm new here and this in my first post!

I'm here because i'd like to share with you a small project i realized for my company due the difficulties and boilerplates found using Scala XML library's API.

I hope that someone can contribute to it.

Github: https://github.com/geirolz/advxml

Features:

  • Append, Remove, Replace XML nodes
  • Edit node's attributes
  • Read XML handling optionality and mandatory nodes and attributes
  • Convert XML into Object and viceversa
  • Normalize XML
  • Convert Java Document to Scala NodeSeq

Example:

import com.github.geirolz.advxml.all._
import scala.xml._
import scala.util._


//import MonadError instance for Try
import cats.instances.try_._

val doc: Elem =
<Persons>
 <Person Name="Mimmo">
  <Cars>
   <Car Brand="Fiat"/>
  </Cars>
 </Person>
</Persons>


val rule = $(_ \ "Person" \ "Cars")
 ==> Append(<Car Brand="Lamborghini"/>)
 ==> Append(<Car Brand="Ferrari"/>)
 ==> Append(<Car Brand="Bmw"/>)


val result: Try[NodeSeq] = doc.transform[Try](rule)
Enter fullscreen mode Exit fullscreen mode

I hope this small project can help someone in desperation with Scala XML library

Let me know what you think about it :)

💖 💪 🙅 🚩
geirolz
David Geirola

Posted on August 14, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

How to Parse XML in Python
parsexml How to Parse XML in Python

October 2, 2024

Using Deequ 1.1 with Spark 3
apachespark Using Deequ 1.1 with Spark 3

February 25, 2021

"Type Disjunctions" in Scala
scala "Type Disjunctions" in Scala

November 6, 2019

Edit XML in Scala in a FP way
scala Edit XML in Scala in a FP way

August 14, 2019