Apologies if the question is already posted elsewhere. I couldn't find the correct answer yet.
I am using javax.xml.bind.Unmarshaller to unmarshall a XML document into a Java Object. While unmarshalling I need to validate this XML against a pre-defined DTD to ensure the tags in the XML document are correct. Can someone let me know how to do this?
Related
I'm trying to enhance a module from using JaxB to JiBX. I was able to produce the java classes through the jiBX maven plugin in pom.xml although I'm having issues on how I can generate an Objectfactory just like the one in JaxB's. Is there like a counterpart in JiBX because I've been searching through the net for hours but I can't find one. I'm new to marshalling and unmarshalling mechanisms so any help will be much appreciated. Thanks!
Rocky,
JiBX has a similar mechanism. You need to create a JiBX Binding Factory then create a Marshalling or Marshalling context. The best way to see this is to look at one of our examples. Here's a link to a simple program that creates a java object, marshalls it to XML, then unmarshals the XML back to java objects.
https://github.com/jibx/maven-plugin/blob/master/test-suite/base-binding-test/org.jibx.schema.test.person/src/main/java/org/jibx/schema/test/person/PersonTest.java
Enjoy!
Don
I have a string to unmarshal
<?xml version="1.0" encoding="UTF-8"?><Nbr>65</Nbr>
i want to get the value 65.
please help me , i am beginner in JAXB.
You should look at unmarshalling examples available over internet. One such example: http://www.javatpoint.com/jaxb-unmarshalling-example
Is there any way, xsd tags for instance, to unmarshall xsd files to get private fields with accessor methods instead of protected fields which is the default behaviour? I have tried to search in the documentation but no avail.
I am aware of the plugin plugin solution here on stackoverflow but I would like to have a xsd-tag or a built-in solution on existing technologies, not extra things to handle like writing a plugins that requires extra maintenance cost =)
I'm migrating my project from j2ee-1.3.jar to javaee-api-5.jar.
web.xml used to use http://java.sun.com/dtd/web-app_2_3.dtd DTD for validation now it needs to use the XSD http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd
The original web.xml as usual fails with the current XSD,while trying to install in webLogic
I have following questions -
1. Do i need to compulsory do the correction of all the elements which are not correct according to the new XSD basically servlet tags are rearranged or changed I guess.
2. Is it compulsory to have an XSD in web.xml, can I do away with the XSD definition and hence validation of the XML
Do i need to compulsory do the correction of all the elements which are not correct according to the new XSD basically servlet tags are rearranged or changed I guess.
Yes
Is it compulsory to have an XSD in web.xml, can I do away with the XSD definition and hence validation of the XML
No
References
XML 1.0 Schema: xml.xsd
I am trying to generate java classes from sample xsd using jdk 1.6, xjc command.
But I don't want to use annotations i.e I don't want to generated java classes to contain annotations. How can I do it?
In JAXB (JSR-222) the only standard representation of the XML-binding metadata defined in the standard is JAXB annotations. Therefore an option to generate the classes without these annotations is of limited use.
I'm the EclipseLink JAXB (MOXy) lead an we do offer an XML (and JSON) representation of the JAXB metadata as an extension:
XML - http://blog.bdoughan.com/2010/12/extending-jaxb-representing-annotations.html
JSON - http://blog.bdoughan.com/2012/04/extending-jaxb-representing-metadata-as.html
If you are interested in having the ability to generate this metadata instead of annotations please request an enhancement in our bug database against the MOXy component:
https://bugs.eclipse.org/bugs/enter_bug.cgi?product=EclipseLink
This question has two parts:
In the compile time you have to generate your code differently. You can write an XJC plugin to do this.
In the runtime you have to use the JAXB implementation which works without annotations (see Blaise's answer on this part).
See this question on XJC extensibility:
XJC - is it extensible?
You can write an XJC plugin which completely replaces the code generation. So I can imagine an XJC plugin which generates XML mappings for MOXy instead of annotations in classes. You can also do this with JAXB RI using an extra annotation reader, but it is much trickier as simply using MOXy.
Be warned, however, that writing advanced XJC plugins my be quite complex.