Java has annotations and that is good. However, some developers feel that it is best to annotate code with metadata using xml files - others prefer annotations but would use metadata to override annotations in source code.
I am writing a Java framework that uses annotations. The question is: is there a standard way to define and parse metadata from xml files. I think this is something every framework that uses annotations could benefit from but I can seem to find something like this on the Internet.
Must I roll my own xml parsing/validation or has someone already done something like this?
There is not a standard way, but here are some Java frameworks who does it:
JPA - check ejb-3_0-fr-spec-persistence.pdf
Spring Framework
TestNG - as written above, though I think it focuses to much on the annotation side rather than the actual configuration he tries to achieve
Seam Framework
I wrote the Annox library which does exactly what you need. With Annox you can read arbitrary annotations from XML.
It's not exactly what you want, but the backport175 project has an implementation of annotations for Java versions before Java 5.
It has some of the functionality you search in that it will read both its own style implementations and "real" annotations if they are present. Maybe this can be used as a starting point to build a more general framework.
Use JAXB http://java.sun.com/developer/technicalArticles/WebServices/jaxb/
You would write the xsd for your metadata file, generate JAXB classes that can help you parse the xml files.
Related
In Java SE/EE, you can generate a Java model from an XML schema and then marshal and unmarshal Java to XML or JSON. The marshallers use reflection and annotations which are not available for most Java mobile APIs which have more or less a Java 1.4 compatibility level.
Is there any existing solution for Java-to-JSON binding on such restricted platforms that does not use platform specific APIs (e.g. Android)?
A possible approach might be an XJC plugin that suppresses annotations and adds marshal() and unmarshal() methods to each model class.
See also: Is there a need in JAXB implementation for Android?
I have not seen such solutions.
It is surely possible to workaround both annotations and reflection by generating the marshalling/unmarshalling code directly in schema-derived classes - just as you suggest. Actually JAXB 1 worked in this way in many senses. However I'm not sure if there's a pressing need for such technology.
ps. I've implemented a JAXB analog for JavaScript (compiler is based on XJC) so it would be definitely possible.
There is a solution named JSONx Framework, but it requires jdk1.8 at the least.
I have a web application using spring annotations extensively. Can I switch back from spring annotations to xml configuration files? Encluding controllers,..etc. I need examples of the configuration files please.
Yes you can switch to externalize the configuration using xml's. Initially only xml's were supported by spring. You can get examples from their reference manual.
If you are looking for complete examples then www.springbyexample.org
Yes. Spring supports annotations, configuration classes and xml configuration. It was never the goal of annotations to deprecate the xml configuration, and it is still fully supported today.
At the S2G forum in Amsterdam last year, it was specifically stated that the goal remains for both approaches to be completely equivalent.
As for the details on how to do it, the documentation of Spring is very good. I suggest you start there. Check out the pet store example, and read up on ContextLoaderListener. It should get you started.
Yes, you can switch back and forth to XML and annotations. In fact, if you even need, you can use a combination of both. Additionally, depending on the type of control you are trying to extract from XML configuration, you can also use #Configuration annotations which provides a way of producing XML configuration via Java code. Keep in mind, however, that there are a few obscure configuration constructs that are not representable by any annotations and can only be done via XML files.
I am looking for a easy to implement xml to java binding. The problem I am facing that there is more than one xml file, and I need to create one object tree from these files.
JAXB is not helping for two reason : the xmls are not usually have any schema, and second JAXB does not offer any solution for combining them.
I tried smooks too, but it also doesn't offer any multiple XML digestion system.
Does anybody have any idea?
You can do this in JAXB using an initialized XmlAdapter. Below is a link to answer I gave to a similar question:
Using JAXB to cross reference XmlIDs from two XML files
Note:
JAXB implementations (Metro, EclipseLink MOXy, Apache JaxMe) do not require an XML schema:
http://wiki.eclipse.org/EclipseLink/Examples/MOXy/GettingStarted
If you use JAXB, I suggest creating your own XSDs for the XML files your need to use. That'll help you document what you thought the schema was at the time, and help identify any future issues due to change at the source.
Then, create a class or classes that deserialize the individual documents into the JAXB-generated classes and then build the object you want from those objects.
Even if you don't use JAXB, I'd still recommend using this kind of pattern to isolate the transformation from XML to Java and keep the part of your app that knows about XML in one place, away from your business logic.
I've used XMLBeans before. Really easy and flexible to use, should be able to help you a great deal.
I'm writing an import function of XML files to my Java application. I am using XOM to parse the XML files. The code for parsing the XML is not easy to understand, it is some hardcoded .getChild(3) and so on. It is hard to follow the code compared to the declarative XML document.
Isn't there a more maintainable way to parse XML documents to Java objects? I would like to have it in a more declarative way, where I can specify what tags corresponds to what Java classes.
Have a look at JAX/B - fairly simple annotation-based approach. It's a standard Java API.
There are tools to generate Annotated Java classes from XSDs or sample XML files. I describe my use of it in my blog
I really like Simple for converting XML to Java.
Have a look at Apache Commons Digester.
Agreed JAXB (JSR-222) is the best solution. Note that JAXB is a spec meaning you have a choice of implementations:
http://bdoughan.blogspot.com/2010/07/jaxb-xml-binding-standard.html
Standard JAXB allows you to specify the mappings by means of annotations, MOXy JAXB also allows you to specify your metadata via XML:
http://wiki.eclipse.org/EclipseLink/Examples/MOXy/EclipseLink-OXM.XML
If you want a maintainable solution you need to break the one-to-one relationship between XML elements found in almost all XML binding solutions, and use XPath based mapping used in MOXy:
http://bdoughan.blogspot.com/2010/07/xpath-based-mapping.html
parse google geocode with xstream
The Simple XML framework uses annotations on field and method declarations as well as on class definitions to map XML to Java and back. Its many times more lightweight than JAXB (which pulls in tonnes of dependencies). In fact it has no external dependencies at all. And its faster too. I tried JAXB many times, but found the annotations and functionality awkward and cumbersome. Check out the Tutorial.
Check Castor XML Mapping
Here is documentation for same : http://www.castor.org/xml-mapping.html
I finally found XStream that was easy to use and parses the XML in a declarative way.
Are Java annotations used for adding functionality to Java code besides just adding documentation about what's going on in the code? What's the most advanced/complex functionality you could add to your code through an annotation?
Annotation are basically not more than a tag (with optional additional data) on a class/method/field. Other code (libraries or tools) can discover these tags and execute functionality dependant on the annotations found. I don't see a real limit on the complexity of the functionality possibly added by annotations. This can for example emulate AOP (adding functionality before or after a method with an annotation).
Annotations as such only add information (metadata) to a class.
One can easily build a system that uses that metadata to provide additional functionality, however.
For example you can use apt to generate classes based on the information provided by the annotation.
An annotation needs a tool to react to it. If such a tool does not exist the annotation is merely a notation. The "tool" can be an APT based agent or some piece of code that uses reflection (for instance, JUnit's #Test).
Several annotations are recognized by the Java compiler and thus have pre-defined semantics: #Override, #Deprecated, #Target.
I would understand Annotations as a way to document your code in a machine readable way.
For example in Hibernate you can specify the whole persistence information for your objects as annotations. This is directly readable for you and not in a distant xml file. But is also readable for the tool to generate configurations, database schemes etc.