views:

98

answers:

1

I'm very new to this, so go easy on me.

From my understanding, JAXB allows the programmer to use Java classes to write XML, and it takes care of creating the proper XML structure by reading XSDs. This way, when the schema changes, the programmer doesn't have to go back and rewrite a bunch of hard-coded XML because JAXB automatically translates the java classes into XML based on the most current schema. Is this about right?

Is there an equivalent of JAXB for ColdFusion, or would it work with ColdFusion? Does CF have this built in?

If I'm way off base, let me know.

Thanks in advance.

+3  A: 

If you are so fund of JAXB, you could easily use JavaLoader ( never had problems with it, it's enterprise ready http://javaloader.riaforge.org/ ) and use your lib almost the same. Ok it could happen that you have to do soemthing "stupid" like this:

var byteClass = createObject("java", "java.lang.Byte").TYPE;
var byteArray = createObject("java","java.lang.reflect.Array").newInstance(byteClass, javacast( "int", 4096 ));

But I guess that is much better then to waste time figuring out some new library.

here's example how to use STaX2 to parse huuge XML files:

<cfscript>
            var loadPaths = [ expandPath('#application.libpath#java/jars/stax2-api-3.0.1.jar')
                             ,expandPath('#application.libpath#java/jars/woodstox-core-lgpl-4.0.5.jar')];
            var javaloader = createObject("component", "#application.libpath#.java.javaloader.JavaLoader").init(loadPaths); 

            var fileStream = createObject("java","java.io.FileInputStream").init(arguments.file);
            var XMLInputFactory = javaloader.create("org.codehaus.stax2.XMLInputFactory2").newInstance();
            var XMLStreamConstants = CreateObject( "java", "javax.xml.stream.XMLStreamConstants");
            var XMLStreamReader = XMLInputFactory.createXMLStreamReader(fileStream);

</cfscript>
zarko.susnjar
Thanks for the quick response! So to be sure, javaloader will allow me to use JAXB? and also, what is STaX2?
Jimmy
@Jimmy - Nothing against the javaLoader, but I thought JAXB was already built in. Have you tried accessing its classes using createObject("java", ..)?
Leigh
@Leigh - I totally would, but I have no idea what I'm doing. I'm having troubles finding something straight forward for coldfusion on the internet.
Jimmy
@Jimmy - Well, I am not familiar with JAXB, OR if it is even the correct way to go here ... But that library could be built in already. So it may be as simple as using createObject. Have you used JAXB before? Because if you look at zarko's example the syntax of creating and using java objects in CF is very similar to how you would do it in java. So you could take a java example and adapt it.
Leigh
@Jimmy - STaX2 ( http://docs.codehaus.org/display/WSTX/StAX2 ) I use it for parsing 2-3GB XML since regular DOM oriented parser would kill the server. I never used JAXB but if it would mean something you can post piece of your java code and we could "translate" it for you :)@Leigh - I checked now, it's not already loaded in CF claspath.@Both - You don't have to use javaLoader, you can put your jars into Coldfusion's lib directory and access it directly.
zarko.susnjar
Leigh
@zarko - I don't actually have any Java code. All I have are some schema by Intuit and a yearning for learning, that's all. In their help docs they mention JAXB, so I was just trying to figure out what I am supposed to do. Also, I'd like to point out that my server is hosted. Dunno if that makes a difference. In other words, I can't install stuff on the server.
Jimmy
@Leigh - Oough, sorry, I just went through jars and looked for that package.I checked now,it is packed in there somewhere.@Jimmy, as far as I understood, you'd use JAXB to (pardon if I'm wrong) to unmarshall their objects. So you'd get some SOAP like XML from their side and deseriallize it to an object. Now, I'd first try to to it in java, probably you'd find more how-to's on google. When you figure out how and what, translate it to CF. Great that you dont have to install or setup anything it's already there. On the other hand you can try to make normal parser if it's not that important.
zarko.susnjar
And yes when you manage to do it, make a nice little tutorial and put link here so we can all benefit ;)
zarko.susnjar
@Jimmy (and @Zarko) - you mentioned you are using a hosted server. To use java objects from CF you will need createObject("java"..) access, which some hosts disable for security reasons. So you may want to verify if you have the necessary permissions before going any further ;) You could run a quick test in a .cfm script. If you do not have access, you will get an exception with the following: <cfset str = createObject("java", "java.lang.String").init("Test")><cfoutput>#str#</cfoutput>
Leigh
alright guys. I'll try to figure this out. If I do, I'll post back here with my results. I'm new to createObject, so it might be a while.
Jimmy
Ok, the jar is in /lib, but coldfusion can't access it? when I use <cfobject type="Java" class="JAXBContext" name="JAXBobj"> or JAXBobj = createObject("java","JAXBContext").newInstance(...) i get an error that says "Class not found: JAXBContext." Any ideas on how to make this accessable?
Jimmy