how to change targetName space in generated jaxb classes - java

I have many xsd's out which i have to generate jaxb classes. now my problem is that. all the xsd's have targetName space which is not mine. i want to generate jaxb classes with different name space. every time.
I have tried this with
targetNamespace elment in bindings.xjb in globalbindings
But i got an error saying that targetNamespace is allowed in jxb:globalbindings..
Can any one help me ..
Thanks In Advance.
Reagrds,
PhaniKiran.Gutha

Try using the XmlSchema annotation, e.g.:
#javax.xml.bind.annotation.XmlSchema(namespace="http://your.custom.namespace.com",
elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
This annotation has to be placed over a package. That's why you need to create a package-info.java file in each package you have the objects you serialize.
You can find more information in Javadoc: http://download.oracle.com/javase/6/docs/api/javax/xml/bind/annotation/XmlSchema.html

Related

How to force XJB to generate the classes, marshalling the XML code with the namespace without prefix independently on exact namespace value?

I have the situation there is an web service implemented in old Visual Studio 2008, which accepts the SOAP requests with content having namespaces only in this form:
<Root xmlns="http://A">
<A>
<A1 />
</A>
<B xmlns="http://B">
<B1 />
</B>
</Root>
All other forms of the XML (unqualified, or qualified with namespace prefix) are causing problems even if the wsdl and referenced xsd structures are manipulated and modified in various ways and the service is recompiled in Visual Studio 2008, the author of the ASP.NET web service is not able to get any other form of XML working.
Is there any way how to achieve the generation of the classes for our Java client accessing this web service by using the external JAXB binding specification, used during the class generation, so the XML would be from the objects marshalled in the above described form?
Am I right, if I think, this could be achieved by the forcing the JAXB to generate the class(ses in multiple directories)
package-info.java
instead of the default form
#javax.xml.bind.annotation.XmlSchema(
namespace = "http://some/custom/namespace",
elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package some.custom.namespace;
in the following form?
#javax.xml.bind.annotation.XmlSchema(
namespace = "http://some/custom/namespace",
elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED,
xmlns = {#XmlNs(prefix = "", namespaceURI = "http://some/custom/namespace")})
package some.custom.namespace;
and if I am right, is there any way to do it? I am unfamiliar with writing the XJB bindings, so any pointer would be appreciated...
Or maybe, after the generation the source code of this class should be postprocessed by something like ant task, which would modify the initially generated JAXB form into the form I think it was the desired content of the class package-info.java?
Could this be somehow achieved by one universal jxb binding file for all usages without necessity to modify it for every namespace folder creating its own package?
I hope this could really work even with multiple different namespaces without prefixes, because the xmlns definitions are then used in JAXBContextImpl as a set of XmlNs types, so if there is an prefix or not hopefully should not matter... Am I right?

What is required in an XSD schema to generate an #XmlElementDecl method?

I am confused as to how the #XmlElementDecl annotation is generated from a WSDL file that was generated by an XSD file. I have noticed that there are tags in my XSD file that are formatted in the following manner:
<xsd:element>...</xsd:element>
and others are formatted as such:
<element>...</element>
or they have a type="element" attribute. The latter two types do not generate any code that contains the #XmlElementDecl annotation, but the first format does.
The annotation is present in the ObjectFactory file.
Does this mean there is a requirement for the schema to have xsd:element as a prefix?
You have to use #XmlElementDecl along with #XmlRegistry annotation in the class to generate the element. You can find this link with an example.
https://docs.oracle.com/javaee/5/api/javax/xml/bind/annotation/XmlElementDecl.html

Spring WS Dynamic WSDL from xsds

Got CommonsXsdSchemaCollection with inline = true with one xsd which xs:include others. It has targetNamespace. Almost all of included xsds has the same namespace, but one xsd doesn't have targetNamespace. Xsd without targetNamespace have xs:attributeGroup with name and elements which refs to it. But when spring tries to inline this xsd exception appears: Attribute group must have ref name set. I debugged the source code of: org.apache.ws.xmlschema:xmlschema-core and I see that xs:attributeGroup element with name gets targetNamespace from xsd which includes it, and xs:attributeGroup with ref doesn't get this namespace, so this error appears.Question is: How to get rid off this error? Is there a way to put targetNamespace to all included elements? Or run this inlining without targetNamespace in some schemas. Or any other ways?

Missing namespace in the javas generated by wsimport

When using wsimport the "standard" way:
wsimport.exe -d C:/temp/generatedClasses -s C:/temp/sourceFiles C:/temp/myWsdl.wsdl
I get source files generated like this:
#XmlRootElement(name = "PingRequest")
public class PingRequest{
Last time the classes were generated the same WSDL/ XSDs should have been used and generated a output like this:
#XmlRootElement(name = "PingRequest", namespace = "http://me.foo.bar/any/")
public class PingRequest {
So the schemas namespace was included as attribute of the annotation. Since the generated class package-info.java has the following entry:
#javax.xml.bind.annotation.XmlSchema(namespace = "http://me.foo.bar/any", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
I assume adding the namespace-attribute is a done by configuration of wsimport/ jaxb schema compiler.
Could anyone explain me how to archive the namespace attribute beeing included?
Update: As Blaise answered correctly (described in the blog-link) the generated file package-info.java defines the namespace for all classes/ types inside the according package. Above example is obsolete if always the same namespace is included per #RootElement. Setting namespaces on #RootElement level may be used to have a certain #RootElement have its own namespace (which in case of wsimport should happen automaticially).
Very many thanks for any suggestions
What i tried:
used google, found https://www.java.net/node/681149 (exaclty my question from back in 2008) with no answer :(
read wsimport/ jaxb schema compiler options, tried out various that only controlled package ouput
read 12 similar question/ answer that poped up when i typed in the title of this question
Closest i found that has something to do with the namespaces was to have 'elementFormDefault="qualified' specified in both the XSD itself and the import part inside the WSDL which i have done.
Specifying the following annotation at the package level, and not specifying the namespace on all the #XmlElement/#XmlRootElement annotations.
#javax.xml.bind.annotation.XmlSchema(
namespace = "http://me.foo.bar/any",
elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
Is the equivalent to not having #XmlSchema and adding the namespace parameter to all the #XmlElement/#XmlRootElement annotations. They will produce/consume the same XML documents.
For More Information
http://blog.bdoughan.com/2010/08/jaxb-namespaces.html

JAXB write namespaces to one class from other classes

When i create a jaxbcontext with all my classes, jaxb build xml for a class with a namespace from other class.
If i pass to jaxbcontext only one class it work fine.
My classes are generated by xjc.
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "", propOrder = {
})
#XmlRootElement(name = "eReq")
public class EReq {
...
And when i pass only this class to jaxbcontext the output is following.
<eReq><status>UNBLOCKED</status></eReq>
But i put all my classes into jaxb context output will be like this:
<eReq xmlns:ns2="myns1" xmlns:ns3="myns2" xmlns:ns4="http://schemas.xmlsoap.org/soap/envelope/"><status>UNBLOCKED</status></eReq>
This is namespaces from my other classes. Why jaxb put it to this class?
Why jaxb put it to this class?
Because the JAXB context contains the superset of all namespaces that it's created with, and it just puts them all in each document it generates.
It does this because the namespaces need to be added to the root element (to avoid hugely wasteful re-declaration of namespaces on each child element), and it doesn't know in advance which namespaces are required for any given set of bound objects (JAXB supports incremental serialization).
So the JAXB runtime probably could avoid doing that; but it doesn't.
If you don't like it, then you need to build multiple contexts.

Categories

Resources