loading...

Edit XML in Scala in a FP way

geirolz profile image David Geirola Updated on ・1 min read

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 business due the difficult and boilerplate Scala XML library's API.

This project is vert young and simple. It's based on Cats-core using MonadError in to handle errors.

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)

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

Let me know what you think about it :)

Posted on by:

geirolz profile

David Geirola

@geirolz

Scala and Java developer for YOOX NET-A-PORTER GROUP

Discussion

pic
Editor guide
 

Hi guys, i've just released the new version of this library v0.1.3, go to take a look ;)