Xml binding for JRuby?

Does anyone here know how to get JAXB working with JRuby? I’m having
trouble with it. It keeps giving me a: “syntax error, unexpected
tIDENTIFIER (SyntaxError)”

The specific location its complaining about is the line where I use:

sgbc = (SomeGeneratedBindingClass) unmarshaller.unmarshal(file)

The tutorials I’m using for JAXB gave me this Java code:

JAXBContext context = JAXBContext.newInstance(“net.some.package.name”);
Unmarshaller unmarshaller = context.createUnmarshaller();
GeneratedBindingClass gbc = (GeneratedBindingClass)
unmarshaller.unmarshal(file);
xmlValueOfInterest = gbc.getXMLValueOfInterest();

so, it seemed reasonable to use the following Ruby/JRuby implementation
of JAXB:

context = JAXBContext.newInstance(“net.some.package.name”)
unmarshaller = context.createUnmarshaller()
gbc = (GeneratedBindingClass) unmarshaller.unmarshal(file)
xmlValueOfInterest = gbc.getXMLValueOfInterest()
puts xmlValueOfInterest

I’ve used “include Java” and java_import ‘java.some.package’ where
applicable… just can’t seem to get the rest of the syntax right. Can
anybody spot what I’m missing?

Cheers,
Eric

in JRUBY you do not need to cast any objects. so just leave the cast
‘(GeneratedBindingClass)’ away.

that should help. regards Kristian

On Sun, Aug 15, 2010 at 9:38 AM, Eric S. [email protected]
wrote:

JAXBContext context = JAXBContext.newInstance(“net.some.package.name”);
gbc = (GeneratedBindingClass) unmarshaller.unmarshal(file)
Posted via http://www.ruby-forum.com/.


To unsubscribe from this list, please visit:

  http://xircles.codehaus.org/manage_email


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Thanks Kristian… I think I’ll just try and write the xml binding,
using the JAXB binding libraries, in straight java. Then use a simple
java class to access the attributes, that way I can just import the .jar
and use some simple calls to a basic java class… maybe that’ll get the
job done. JRuby seems to be choking on some of the example code that
I’ve been trying to implement. And I’m not savvy enough to figure out
how write it differently. Anyhow, thanks again!

Cheers,
Eric

Eric S. wrote:

Thanks Kristian… I think I’ll just try and write the xml binding,
using the JAXB binding libraries, in straight java. Then use a simple
java class to access the attributes, that way I can just import the .jar
and use some simple calls to a basic java class… maybe that’ll get the
job done. JRuby seems to be choking on some of the example code that
I’ve been trying to implement. And I’m not savvy enough to figure out
how write it differently. Anyhow, thanks again!

Cheers,
Eric

Okay,

Here’s an update for anyone who’s following in my footsteps… If you’re
using JRuby, it seems best practice to minimize your use of Java inline
with Ruby. It can’t handle things like casting and such, so don’t even
bother with it. I created a basic accessor class, and placed all the
unmarshalling JAXB code in the constructor, and used it to initialize
the variables that matched the tags in the xml document. Then in my
JRuby code I just did the following:

include Java
require ‘path/to/my/xml/binding/jar/file’
import ‘my.accessor.class.package.MyAccessorClass’

ac = AccessorClass.new(java.io.File(“path/to/xml/file”))

now you can output your xml values, or store in a database, etc…

puts ac.someAttribute

Yes, its that easy folks! I was making it way harder than it needed to
be. If anyone has any problems with this, don’t hesitate to ask. I think
I understand it pretty well now.

Cheers,
Eric

Eric S. wrote:

Eric S. wrote:

Thanks Kristian… I think I’ll just try and write the xml binding,
using the JAXB binding libraries, in straight java. Then use a simple
java class to access the attributes, that way I can just import the .jar
and use some simple calls to a basic java class… maybe that’ll get the
job done. JRuby seems to be choking on some of the example code that
I’ve been trying to implement. And I’m not savvy enough to figure out
how write it differently. Anyhow, thanks again!

Cheers,
Eric

Okay,

Here’s an update for anyone who’s following in my footsteps… If you’re
using JRuby, it seems best practice to minimize your use of Java inline
with Ruby. It can’t handle things like casting and such, so don’t even
bother with it. I created a basic accessor class, and placed all the
unmarshalling JAXB code in the constructor, and used it to initialize
the variables that matched the tags in the xml document. Then in my
JRuby code I just did the following:

include Java
require ‘path/to/my/xml/binding/jar/file’
import ‘my.accessor.class.package.MyAccessorClass’

ac = AccessorClass.new(java.io.File(“path/to/xml/file”))

now you can output your xml values, or store in a database, etc…

puts ac.someAttribute

Yes, its that easy folks! I was making it way harder than it needed to
be. If anyone has any problems with this, don’t hesitate to ask. I think
I understand it pretty well now.

Cheers,
Eric

Oops, caught a typo:

include Java
require ‘path/to/my/xml/binding/jar/file’
import ‘my.accessor.class.package.MyAccessorClass’

myac = MyAccessorClass.new(java.io.File(“path/to/xml/file”))

now you can output your xml values, or store in a database, etc…

puts myac.someAttribute

Now go enjoy a tasty beverage… :smiley:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs