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
Related
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?
This is my first question on StackOverflow in general, so please bear with me.
I have an issue with marshalling XML that uses the default prefix for different namespaces. Each element that uses the default prefix should specify its namespace in its opening tag. An example what I'm trying to achieve would be something like this (simplified):
<parent xmlns="parentNamespace" xmlns:cd1="child1Namespace">
<cd1:child1/>
<child2 xmlns="child2Namespace">
...
</child2>
<child3 xmlns="child3Namespace">
...
</child3>
</parent>
I have tried setting namespaces prefixes in package-info.java of the object I'm trying to marshall using annotations like this:
#javax.xml.bind.annotation.XmlSchema(
namespace = "parentNamespace",
elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED,
xmlns = {
#XmlNs(prefix="",parentNamespace"),
#XmlNs(prefix="cd1", namespaceURI="child1Namespace"),
#XmlNs(prefix="", namespaceURI="child2Namespace"),
#XmlNs(prefix="", namespaceURI="child3Namespace")
}
)
But the end result is something like this:
<ns1:parent xmlns:ns1="parentNamespace" xmlns:cd1="child1Namespace" xmlns="child2Namespace xmlns:ns2="child3Namespace"<
<cd1:child1/>
<child2>
...
</child2>
<ns2:child3>
...
</ns2:child3>
</ns1:parent>
Which really is a valid XML, but unfortunately for reasons that are outside my control isn't good enough (doesn't pass validation). Also note that solution using Java 8 is highly preferable as I wouldn't want to mess with NamespacePrefixMapper if at all possible (if it even works).
So the question is: Is it possible to generate XML using JAXB that uses same prefix for multiple namespaces and how?
I have generated Java classes from an XSD, and in my XML responses I am getting a ns2: prefix which I do not need.
For example, I am getting this response:
<ns2:location xmlns:ns2="http://www.example.com/">
<ns2:response/>
</ns2:location>
But the response I am expecting is as follows:
<location>
<response/>
</location>
Your example shows not just a namespace prefix (ns2) declaration but also an actual namespace (http://www.example.com/ for the location and response elements) as not being desired.
If you have full control over the XSD, you can remove the
targetNamespace="http://www.example.com/"
attribute from the xs:schema element in the XSD. Elements of XML documents conforming to this XSD will then no longer be in any namespace, and your generated Java classes will reflect this new setting.
However, be aware that deleting (or changing) targetNamespace effectively changes the names of the components defined in the XSD. Think twice about making such a change if the XSD is defined by another party or otherwise already in use by others.
I have generated Java classes from XSD, all works fine from a unmarshalling point of view.
However, when I marshall from JAXB classes I get the following:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<message xmlns="http://poc.cmc.com/ScreenLayout">
<Data>
<Type>Sample</Type>
. . .
</message>
But I need
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns0:message xmlns:ns0="http://poc.cmc.com/ScreenLayout">
<ns0:Data>
<ns0:Type>Sample</ns0:Type>
. . .
how can I control that from Java?
Thanks a lot
You can use the #XmlSchema annotation on a package-info class to assign a prefix to the namespace:
#XmlSchema(
namespace = "http://poc.cmc.com/ScreenLayout",
elementFormDefault = XmlNsForm.QUALIFIED,
xmlns={#XmlNs(prefix="ns0", namespaceURI="http://poc.cmc.com/ScreenLayout")})
package your.package;
import javax.xml.bind.annotation.*;
Cant post this as a comment!
because the consuming application is very dumb and needs the prefix
In that case the dumb application is not really consuming xml. Take a look at this link http://bdoughan.blogspot.com/2010/08/jaxb-namespaces.html and play with the namespace options. Specifically
#XmlSchema (
xmlns = {
#javax.xml.bind.annotation.XmlNs(prefix = "ns1", namespaceURI="http:test"),
#javax.xml.bind.annotation.XmlNs(prefix = "xsd", namespaceURI="http:www.w3.org2001XMLSchema")
},
namespace = "http:test",
elementFormDefault = XmlNsForm.UNQUALIFIED,
attributeFormDefault = XmlNsForm.UNSET
)
used in a package-info.java file.
#XmlType(namespace="http://www.example.org/type")
Used on a class declaration
#XmlElement(namespace="http://www.example.org/property")
Used on a property.
Some combination or only one of these options may give you what you want. However you should understand that you're fighting an uphill battle when you move from valid xml to xml that must contain a specific namespace prefix on all elements.
According to XML spec both xml's are the same, as xmlns="" defines default namespace which applies to current and all child elements.
XML parsers should give you the same DOM or SAX in both cases
I did the following steps and everything works. Basically I've compiled form the wsdl or whatever schema file you have.
Download from
https://repo1.maven.org/maven2/com/sun/xml/bind/jaxb-ri/ (I've used version 2.3.4)
With the following command:
xjc.sh -wsdl -npa -mark-generated -d src/main/java -p your.package.hierarchy src/main/resources/wsdl/*
I had wsdl files and that's the reason for the first flag, the -npa is to add the namespace information in the annotation rather than on a package-info.java since for some reason that wasn't working for me.
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