Search |
||
Read web.xml with one line of codePosted by survivant on December 19, 2008 at 7:26 AM PST
I needed to parse a web.xml for a sample for Grizzly and I wanted to find a way to have something that I can reuse later. You don't have to do it by hand. There are tools out there to do it for you. One of them is XMLBeans from Apache group and more popular is JAXB. For JDK 1.4 ,I used XMLBeans and if you have JDK6 JAXB will reduce the dependencies and will be more smaller. I'll show you how to use theses two API. XMLBEANS What I like about this tool is that I can generate Java classes from a xsd within seconds using Ant. I want to show you how to obtain a Class that will contains a web.xml loaded with only one line of code :) Let's start by downloading XMLBeans. I'll skip to the folder structure I'll use for this demo. lib/ : Where you put the XMLBeans libraries Download build.xml from here. I use the schemas from Glassfish sources : glassfish/appserv-commons/schemas You don't have to copy all the files. Copy just the main schemas and Ant will download the missing schemas. For my demo I used theses files : schemas/j2ee_1_4.xsd you just have to launch the ant task with this command : ant -autoproxy that will generate a jar file and the java source for theses schemas. Now.. How do we load a web.xml within our Java application ? #1 - Include the jar file : j2ee_schemas-xmlbeans.jar into your project WebAppDocument webAppDocument = WebAppDocument.Factory.parse(webxmlFile); After that .. It's up to you, webAppDocument contains all the config from web.xml. JAXB You will need the librairies for the compilation, but after that if you use JDK6, you can remove them, because JAXB is include in JDK6. #1 - To generate the schemas, you use ant. JAXBContext jc = JAXBContext.newInstance("ca.sebastiendionne.jaxb.xsd"); It's take a little more line of code, but it's not complicated. I put the source project (xmlbeans + jabx) here. »
Related Topics >>
Web Applications Comments
Comments are listed in date ascending order (oldest first)
Submitted by felipegaucho on Sun, 2008-12-21 11:34.
The idea is correct, but I can't understand why you are using external libs if JRE provides the same quality for free.. :) You can use XJC and JAXB that is already included in JRE 6 - Just check this entry: http://weblogs.java.net/blog/felipegaucho/archive/2008/12/the_smallest_a...
Isn't better if you don't need any external library in your example ? :)
Submitted by survivant on Sun, 2008-12-21 13:18.
Really a good comment. The reason is simple. I wanted to show that it was possible to do it easily. I used Xmlbeans because it works with JDK 1.4+.
If the target is JDK6+, I would suggest using XJC and JAXB like in your sample.
I'll add a section, if you have JDK6....
|
||
|
|