Best way to automatically determine class for binding fromXML

47 views
Skip to first unread message

John Kida

unread,
Dec 2, 2012, 2:19:11 PM12/2/12
to sca...@googlegroups.com
I am very new to the scalaxb library and have used JAXB in the past with Java. Now that I am using more scala I found this library and have been very impressed with its functionality.

I am developing an application for a system that will need to process an inbound XML stream. I currently have scalaxb working where I can create an object by using scalaxb.fromXML[SomeClass](xmlElement).

My question is what is the best way I can match the XML element to the class it belongs with. I am thinking a case statement, but what is the cleanest way to do this?

For example.. lets say the following XML element comes in... which is 1 of a couple dozen different elements/classes
val xmlStream = <Person>
  <firstname>john</firstname>
  <lastname>smith</lastname>
</Person>  

xmlStream match {
Not_Sure_What_To_Match => scalaxb.fromXML[Person](xmlStream)
... other XML elements...
}

Does anyone have any suggestions on how I can take my xmlStream and match it to a class? I know in JAXB there was a method i think getDelclaredType that you could use to return the Class the XML belonged to.

Thank you for any suggestions.

eugene yokota

unread,
Dec 2, 2012, 3:57:47 PM12/2/12
to sca...@googlegroups.com
Hi John,

At least in the current version of scalaxb, there is no list of elements to Scala type map.
Could you open a github issue for it?

-eugene

John Kida

unread,
Dec 3, 2012, 1:13:01 PM12/3/12
to sca...@googlegroups.com
I posted it in the scalaxb github, however this is kinda what I came up with a temporary solution... Can anyone think of a cleaner or better way?

    val xmlStream = <Channel xmlns="http://schema.broadsoft.com/xsi">
                    <channelId>39962412-3920-4471-b961-3076d91c0046</channelId>
                    <expires>3600</expires>
                  </Channel>
   

    val productClass = xmlStream.label match {
      case "Channel" => //do channel code stuff
      case "Subscription" => //do subscription code stuff
      case ... Many more
    }
Reply all
Reply to author
Forward
0 new messages