We frequently use templates as a way of serializing an object. That is, given a Java POJO and a suitable XML template that includes placeholders like ${person.address.street}, we can output fully formed XML etc.
Are there any libraries where you can take that same template and a sample piece of output, and go the other way? That is, produce a populated Java bean (for instance) from a template, a sample XML document and, I guess, a Class name.
You can use Freemarker to help you it's a great JAVA API
You can have XML template file which call getters and then create a dynamic XML file.
Tell me if it's helps,
Related
I'm looking for a solution which automatically generates POJO classfiles from a given .yaml-Files but have not found anything like this yet.
I can not imagine that it should be the only way to write these classes yourself.
The problem is that YAML describes objects, not classes. In general, you cannot automatically derive a POJO structure from a given YAML file. Take, for example, this YAML:
one: foo
two: bar
In YAML, this is a mapping with scalar keys and values. However, there are multiple possibilities to map it to Java. Here are two:
HashMap<String, String>
class Root {
String one;
String bar;
}
To know which one is the right mapping, you would need a schema definition like those for XML. Sadly, YAML currently does not provide a standard way of defining a schema. Therefore, you define the schema by writing the class hierarchy your YAML should be deserialised into.
So, in contrary to what you may think, writing the POJOs is not a superfluous action that could be automated, but instead is a vital step for including YAML in your application.
Note: In the case that you actually want to use YAML to define some data layout and then generate Java source code from it, that is of course possible. However, you'd need to be much more precise in your description to get help on that.
As pointed out in the comments by Jack Flamp, you can use an online tool (jsonschema2pojo) to convert a sample yaml file to its equivalent POJO classes. This tool can convert json or yaml data to corresponding POJO classes and I have used it successfully in the past.
That being said, the tool is forced to make certain "assumptions" when you are using a yaml file(instead of yaml schema). So, it would be a good idea to look at the generated classes carefully before you start using them.
You can find more information about how to use this online tool from its wiki page.
The Accepted Answer is incomplete.
You can try to use https://editor.swagger.io/
After importing yaml file You can generate Java REST Client project through menu with correspondent POJO classes.
I am experimenting with JAXB library to read xml files and I see that I need to define the Class objects along with annotations to indicate the xml element structure. I was wondering if there is a way that I can read the xml file without having to define such a class. this will permit the user to add a new tag without having to redefine my class.
I am not particular about jaxb usage, any other java libraries are ok too.
Sure, if you just want to work with raw XML without binding it onto POJOs, you can use javax.xml.parsers.DocumentBuilderFactory/javax.xml.parsers.DocumentBuilder to read any arbitrary XML directly into a org.w3c.dom.Document and just work with it as a document instead of as mapped data.
I'm looking for a Bean to Bean Mapping Java Framework that their mapping rules could be defined outside/not in java code. The source and target beans has n subBeans so the mapping rules could be a little bit complex (not a simple one-to-one mapping).
A little overview about the process:
It's Simple ETL process but with a configurable mapping logic.
I use Spring Batch to load a multiline record (fixed lenght file) into a bean. Its just a representation of the record as an javabean to use it as base for the defined mapping rules. the result of this mapping is another javabean that is completly different build as the source one. And here I would like to use a generic mapping framework between this to java beans.
The Spring Batch part is completly clear and implemented.
Of course I could defined it hardcoded in java but for transparence reasons I have to export this mapping logic outside the java code.
Does anyone know a such framework? Does one exists? I found Dozer but I think I can't define some complex mapping rules in their XML.
Maybe. I would use the Java Scripting API for this. It allows you to load scripts (JavaScript, Beanshell, Groovy, whatever) and run them. You could put a line of input (or the whole model) in a variable and the script could then create the new object structure.
try to use JMapper Framework.
In XML you can write STATIC and DYNAMIC conversion using placeholders to use values and names of the fields, for example if you need to get and set values from a map the code is the follows:
<conversion name="fromMapToAll" from="map" type="DYNAMIC">
return (${destination.type}) ${source}.get("${destination.name}");
</conversion>
<conversion name="fromAllToMap" to="map" type="DYNAMIC">
${destination}.put("${source.name}",${source});
return ${destination};
</conversion>
see the wiki page for more info.
I'm working with XSL templates in Java, and I'm trying to build a custom tag that will call some Java code, then put a result inside the template. I'm using XOM as my XML engine. I'm kind of new with both XOM and XSL, so I'm not even sure if this is a smart idea.
A very simple example of something I want to do is this, where my_ns is a custom namespace with 'custom_tag' that the method custom tag
<xsl:template name="foo">
<my_ns:custom_tag />
</xsl:template>
public Node custom_tag() {
return Node("<a/>");
}
#result of calling the template foo
<a/>
I'm open to suggestions for involve alternate ways of calling Java from a XSL template.
This is more a question about if your XSLT processor can execute/call java code from within the template more than your XML engine/parser/api. The default XSLT processor for java is Xalan-C or Xalan-J (can't remember which) from the Apache Software Foundation. I do believe both of them allow for extension functions to execute java code inside the method. I've done JDBC sql queries inside a XSL stylesheet before using a xalan-j extension function. I also recall reading that the Saxon XSLT processor also allows this functionality. You'll have search your XSLT processor to get the specifics on to implement this.
The question on whether this is a good idea or not really depends on the problem. Even though I used the SQL extension function mentioned above and it fit the bill in that case, I felt really dirty about it afterwards. The reason I say this is because you lose portability between XSLT processors when you add in the vendor-specific extension functions.
Your example shows you are just simply creating a new node in the output and if that is the case, I don't see the need to have java do this when that is one of the main functions of XSLT: creating nodes. I suspect your real problem is more complex than simply creating a node so I'll suggest you may want to look into doing all the work in java to get the results you are looking for OR doing some of the work in java and passing a parameter (name/value pair using the xsl:param element) to your XSL stylesheet a runtime.
Here's some quick sites to get you started:
http://xml.apache.org/xalan-j/extensions.html
http://www.saxonica.com/documentation/extensions/intro.xml
http://www.w3schools.com/xsl/
http://www.w3schools.com/xsl/el_param.asp
While working with JAXB 2.0 i came across a query which i am unable to solve so far,while doing the validation i have 2 options
1) Either as soon as i found the error throw the exception as i am done.
2) Move ahead if there is any error or validation and i assume this is the best way since it will help one to show all errors or warings with respect to the whole XML.
but since this process also invlolves unmarshalling means it will unmarshall my provided XML is to respected Object even if there is any Error or warning.so all means extra work..
My question is is these a way so that i can do whole validation and if it is successfull only than should the corresponding XML be bind to respected POJO classes
thanks in advance
You can use the javax.xml.validation APIs to validate an XML document against an XML schema. The you can choose to unmarshal this object again using JAXB.
Below is an example of using these APIs. In this example the input is actually an object model, but you can adapt it to work with any XML input.
http://bdoughan.blogspot.com/2010/11/validate-jaxb-object-model-with-xml.html