Coder Social home page Coder Social logo

xmlparser's Introduction

Xml Parser

A project for parsing and rendering objects from and to XML

How to use:

Renderer

  1. Define an implicit way to render your type in the companion object
  case class Word(word: String)
  object Word {
    implicit val writes: XmlWrites[Word] = x => XmlObject("word", Seq(XmlLiteral(x.word)))
  }
  1. Use
  val obj = Word("Hello!")
  
  XML.write(obj).toString // mustEqual "<word>Hello!</word>"

Note: Also works with complex and nested types that you defined XmlWrites for

Parser

  1. Define an implicit way to read your type from string
  case class User(id: Int, name: String)
  object User {
    implicit val reads: XmlReads[User] = x =>
      XmlSuccess(
        User(
          id = (x \! "id").as[Int].get,
          name = (x \! "name").as[String].get
         )
      )
  }

Note: This is not the best example but it is simplified for demo purposes

  1. Use
    val xml = "<user><id>12</id><name>John</name></user>"
    val result = User(Some(12), "John")
    
    XmlParser.parse(xml).as[User] // mustEqual XmlSuccess(result)

Format

Format is a more concise way to define XmlReads and XmlWrites at the same time

    case class User(id: Int, name: String)
    object User {
      implicit val fmt: XmlFormat[User] = XmlFormat(
        xmlValue => {
          val id = (xmlValue \! "id").as[Int].get
          val name = (xmlValue \! "name").as[String].get
          XmlSuccess(User(id, name))
        },
        user => XmlObject("user", Seq(
          XmlObject("id", Seq(XmlLiteral(user.id))),
          XmlObject("name", Seq(XmlLiteral(user.name)))
        ))
      )
    }

    ...

    val o = User(123, "Jeff")
    val xml = "<user><id>123</id><name>Jeff</name></user>"
    
    XML.write(o).toString // mustEqual xml
    XML.read[User](XmlParser.parse(xml)) // mustEqual XmlSuccess(o)

For examples, please check the *Spec.scala files

TODO:

  • Make a macro for auto xml rendering on ADTs
  • Recognise XmlObject definitions from Map[String -> Map[...]]
  • A way to define implicit XmlReads easier
  • Add more examples to the Readme
  • Code cleanup

xmlparser's People

Contributors

kdkocev avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.