I've used xjc of jaxb 2.2.6 to generate a set of classes from a xsd file.
By editing "package-info.java" I've associated to the different namespaces the prefix value.
So I've created a Test Class with a main that Unmarshal an xml file, edit some information, and marshal the object in xml format.
Everything works like a charm and javax.xml.bind.Marshaller object match correctly namespace and prefix as defined in package-info.
When I deploy this application as WS using axis2 on tomcat7 in the same machine and call a ws method that execute the code described above javax.xml.bind.Marshaller create an xml file with default namespace (ns1, ns2....).
The package-info.java that I've used is something like this:
#javax.xml.bind.annotation.XmlSchema(
namespace = "....",
xmlns = {
#XmlNs(namespaceURI = "....", prefix = "myprefix"),
#XmlNs(namespaceURI = "...", prefix = "myprefix2"),
},
elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package ....;
import javax.xml.bind.annotation.XmlNs;
This is a piece of right output (This output is obtained when I execute the code as "Java Application"):
.....
<ContactInformation>
<rm:ContactDescription>ASD</rm:ContactDescription>
<rm:ContactRole>ASD</rm:ContactRole>
<rm:ContactLocation/>
<rm:AdditionalContactInformation>
<xnl:PartyName>
<xnl:PersonName>
<xnl:NameElement xnl:ElementType="FirstName">ASD</xnl:NameElement>
<xnl:NameElement xnl:ElementType="LastName">ASD</xnl:NameElement>
</xnl:PersonName>
<xnl:OrganisationName>
<xnl:NameElement>ASD</xnl:NameElement>
</xnl:OrganisationName>
</xnl:PartyName>
</rm:AdditionalContactInformation>
</ContactInformation>
......
This is a piece of wrong output (This output is obtained when I execute the code inside an "Axis2/Tomcat7 WS Application"):
.....
<ContactInformation>
<ns2:ContactDescription>ASD</ns2:ContactDescription>
<ns2:ContactRole>ASD</ns2:ContactRole>
<ns2:ContactLocation/>
<ns2:AdditionalContactInformation>
<ns7:PartyName>
<ns7:PersonName>
<ns7:NameElement ns7:ElementType="FirstName">ASD</ns7:NameElement>
<ns7:NameElement ns7:ElementType="LastName">ASD</ns7:NameElement>
</ns7:PersonName>
<ns7:OrganisationName>
<ns7:NameElement>ASD</ns7:NameElement>
</ns7:OrganisationName>
</ns7:PartyName>
</ns2:AdditionalContactInformation>
</ContactInformation>
......
For each case exists a package-info.java where namespaces translation are declared.
How can I resolve this issue?
A JAXB (JSR-222) implementation is not required to use the prefixes as defined in the #XmlSchema annotation. The prefixes are used are not significant, and the namespace qualification between JAXB (JAX-WS) implementations will be the same although the prefixes may be different.
Related
I want XMl document tag as follows using EclipseLink Moxy using annotation:-
<Document xmlns=“urn:iso:std:20022:tech:xsd:pacs.009.001.08” xsi:schemaLocation=“urn”iso:std:20022:tech:xsd:pacs.009.001.08 schema.xsd” xmlns:xsi=“https://www.w3.org/2001/XMLSchema-instance”>
I am using package-info.java as follows:-
#XmlSchema(namespace = "usn:iso:std:20022:tech:xsd:pacs.009.001.08", elementFormDefault=javax.xml.bind.annotation.XmlNsForm.UNQUALIFIED, xmlns = {#XmlNs(namespaceURI = "https://www.w3.org/2001/XMLSchema-instance", prefix="swift-pacs") })
Whereas my java Model class has #XmlRootElement as follows:-
XmlRootElement(name="Document", namespace="urn:iso:std:20022:tech:xsd:pacs.009.001.08")
With this i am getting output as:-
<ns0:Document xmlns:ns0="urn:iso:std:20022:tech:xsd:pacs.009.001.08" xmlns:swift-pacs="https://www.w3.org/2001/XMLSchema-instance">
But i want to add "xsi:schemaLocation=“urn”iso:std:20022:tech:xsd:pacs.009.001.08 schema.xsd" to it and want output as shown below:-
<Document xmlns=“urn:iso:std:20022:tech:xsd:pacs.009.001.08” xsi:schemaLocation=“urn”iso:std:20022:tech:xsd:pacs.009.001.08 schema.xsd” xmlns:xsi=“https://www.w3.org/2001/XMLSchema-instance”>
How to do it? If possible can anyone suggest how to remove ns0 prefix (ns0:Document) before document and ns0 suffix (xmlns:ns0) after xmlns.
I'm trying to get my JAXB marshaller to use the provided schemaLocation without using
marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "some location");
I see that there is an option to provide a schema location in my
package descriptor
#javax.xml.bind.annotation.XmlSchema(
namespace = "http://my.website.com/TheClass"
, elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED
, location = "http://my.website.com/TheClass TheClass.xsd"
)
package com.mypackage.beans;
but it won't print in the xml
I assume that your main issue is that the generated XML from your marshaller misses the required name space.
Have you tried adding the xmlns={#XmlNs(prefix="your_name_space", namespaceURI="http://my.website.com/TheClass")}
in your XmlSchema annotation in your package descriptor?
(Suggestion taken from: JAXB namespace prefixes missing)
I am attempting to Unmarshall XML from a file using JAXB. I also have a web service.
I want to use no namespace, not the namespace defined by the web service.
I am getting the following error:
Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"CustomerName"). Expected elements are <{http://www.ws.NET}CustomerName>
I think that the solution is to change the package-info.java file to use no namespace. Is this the correct approach? and what changes should I make to this file?
The package-info.java file looks as follows:
#javax.xml.bind.annotation.XmlSchema(namespace = "http://www.ws.NET", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package mynewpackage;
Thanks for your help
Changing QUALIFIED to UNQUALIFIED will help if the top-level element (corresponding to the #XmlRootElement annotated class) is in the target namespace but its children aren't, e.g.
<ns:Response xmlns:ns="http://www.ws.NET">
<CustomerName>Ian</CustomerName>
</ns:Response>
but this won't help if the top level (in this case Response) element is also unqualified.
When you have a JAXB model that is annotated to be namespace qualified and you want to unmarshal XML that isn't there are a couple of options.
If You Can Change the Model
You may just need to remove the #XmlSchema annotation from the package-info class. If there are no other package level annotations you could remove the whole class. Note that namespace information may also be specified on other annotations such as #XmlElement.
http://blog.bdoughan.com/2010/08/jaxb-namespaces.html
If You Can't Change The Model
If you can't change the JAXB model to remove the namespace qualification then you can leverage a SAX XMLFilter to apply a namespace to the XML as it's being read.
http://www.stackoverflow.com/questions/14609597/how-flexible-is-jaxb/14610836#14610836
I am trying to write a Java application that consumes a given web service, using JAX-WS and wsimport. The SOAP message that it sends to the service is mostly correct. However, on of the parameters passed to the service function is an array of strings. Although the array itself is given the proper namespace in the SOAP XML, elements ('parm' in the message below) have no namespace. This causes the service to fail.
<?xml version="1.0" ?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:Submit xmlns:ns2="http://www.acme.com/service/wsdl">
<ns2:service>SomeJob</ns2:service>
<ns2:parms>
<parm>someparam</parm>
<parm>anotherparam</parm>
</ns2:parms>
</ns2:Submit>
</S:Body>
</S:Envelope>
The <parm> elements should have read <ns2:parm>, or alternatively the <Submit> tag could have defined a default namespace <Submit xmlns="http://www.acme.com/service/wsdl"> (that is what the service suggests in an example SOAP message).
This question does resemble the one in JAX-WS: why nested elements are in "" namespace?. However there changing the style from RPC/Literal to Document/Wrapped solved the issue, while in my case the service is Document/Wrapped to begin with.
How can I convince the JAX-WS library to generate namespaces on the nested elements in the array?
TIA,
Jeroen
Update: Editing the generated code appears to work. Someone suggested adding a namespace attribute to the #XmlElement line preceeding the definition of 'parm' in the generated array type:
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "ArrayOfString", propOrder = {
"parm"
})
public class ArrayOfString {
#XmlElement(nillable = true, namespace="http://www.acme.com/service/wsdl")
protected List<String> parm;
...
}
This causes JAX-WS to add the namespace and will probably solve the issue. As code generation is, in this case, done only once, editing the generated code is acceptable. Still I wonder whether there isn't a better solution.
You can also influence the namespace creation by adding
elementFormDefault="qualified"
either to your Java #XmlSchema annotation - or in your case - to the XSD defining the service.
I want to map following xml using custom name sapace. I checked How to have custom namespace prefix but could not find any answer.
<p385:execute xmlns:p385="http://tal.myserver.com">
<version xsi:type="xsd:string">0.1.0</version>
<xmlData xsi:type="xsd:string">
.... xml encoded data
</xmlData>
</p385:execute>
How can i map this to a java class?
Since it is only the root element that is namespace qualified, you just need to specify the namespace on the #XmlRootElement annotation for the class.
#XmlRootElement(namespace="http://tal.myserver.com")
public class Execute {
}
You can the suggest the prefix that should be used for the namespace using the package level #XmlSchema annotation:
http://blog.bdoughan.com/2011/11/jaxb-and-namespace-prefixes.html
Use the wsimport tool to generate artifacts such as JAXB classes from a WSDL:
http://docs.oracle.com/javase/7/docs/technotes/tools/share/wsimport.html
http://jax-ws-commons.java.net/jaxws-maven-plugin/wsimport-mojo.html