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

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

Related

How to use JAXBElement in jackson xml serialization?

I have autogenerated java classes from xsd using xsd2java. I cannot modify neither xsd nor the java classes.
Problem: in one class an element of List<JAXBElement> is generated.
If I now add any JAXBElement, the jackson xml marshaller won't show the proper xml element, but the properties of the JAXBElement serialized. Like declaredType, scope, etc. See below.
#XmlRootElement(name = "bookingRequest")
public class AutogeneratedReq {
private List<JAXBElement<?>> someElements;
}
Usage:
AutogeneratedReq req = new AutogeneratedReq();
JAXBElement<?> person = new ObjectFactory().createPerson();
req.getSomeElements().add(person);
Result:
<someElements>
<JAXBElement>
<name>person</name>
<declaredType>net.some.company.Person</declaredType>
<scope>net.some.company</scope><value someattribues="test"/>
<nil>false</nil>
<globalScope>false</globalScope>
<typeSubstituted>false</typeSubstituted>
</JAXBElement>
</someElements>
Question: how can I tell jackson or spring-mvc to generate proper xml, and not JAXBElement serialization explicit?
I don't know which xsd2java utility you currently use, but you can try the following maven plugin to generate Java classes from XSD files.
https://github.com/highsource/jaxb2-basics/wiki/Using-JAXB2-Basics-Plugins
And then you can use following extension to create correctly typed POJO's.
https://github.com/highsource/jaxb2-basics/wiki/JAXB2-Simplify-Plugin
BUT even if you can create typed POJO attributes, the XML file generated from this POJO may not be 100% valid against original XSD file.
<jaxb:bindings multiple="true" node="//xs:element[#name='someElement']//xs:complexType//xs:choice//xs:element">
<simplify:as-element-property/>
</jaxb:bindings>

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

How do I assign a xsd location in JaxB

I'm using JaxB 2.2, can't use MOXy or other JaxB implementations due restrictions.
Is there a way to assign a created xsd to a Xml Element or Class in Eclipse? For example if I have an xsd in WebContent/validators/schemas and the file name is "foo.xsd" and I have a class in "foo.bar.classes" package named "foobar.java" with three fields "foo1" "foo2" "foo3". How do I assign the xsd to the class or the fields? I'm using #XMLElement and #XmlRootElement on that class. Thanks.

How to detect required XML attributes?

I have an XML content without defined attributes, like this:
<rootElement>
<subElement1/>
</rootElement>
I want to populate this XML content with required attributes defined in XML Schema (XSD) for this XML.
For example, according to XSD subElement1 has required attribute 'id'.
What is the best way (for Java processing) to detect that and add such attributes to XML?
We need to add required attributes and set appropriate values for them.
As a result for example above we need to have the following XML:
<rootElement>
<subElement1 id="some-value"/>
</rootElement>
In the XML schema definition, i.e. XSD file, attributes are optional by default. To make an attribute required, you have to define:
<xs:attribute name="surname" type="xs:string" use="required"/>
You will find a very good introduction on XML and XML Schema Definitions, i.e. XSD, on W3 Schools.
In Java the equivalent of defining a XML schema is using JAXB, i.e. Java API for XML Binding that is included into Java SE. There you would define, e.g.
#XmlRootElement
public class Person { public #XmlAttribute(required=true) String surname; }
Hope this could clarify your question.
I would suggest you to use JAXB for that. Search the Internet for tutorials.
Steps to proceed further with JAXB,
Generate Java files using JAXB by providing the schema
Unmarshal your XML to generated Java classes (beans). Don't do validation or set validation handler here.
Populate those classes with appropriate values. required elements can be found using annotation look up. JAXB annotation for element would look like something, #XmlElement(name = "ElementName", required = true). And an attribute annotation would be something similar to this, #XmlAttribute(required = true)
Marshal your bean back to XML. You can validate your bean using ValidationHandler, while marshalling. Below is the sample code snippet,
marshller = JAXBContext.newInstance(pkgOrClassName).createUnmarshaller();
marshller.setSchema(getSchema(xsd)); // skip this line for unmarshaller
marshller.setEventHandler(new ValidationHandler()); // skip this line for unmarshaller
Use a DOM parser.Has methods to traverse XML trees, access, insert, and delete nodes
I have had the same idea of Cris but I think that with this validator you don't have information about the point in which you have had the error.
I think that you have to create or extend your own validator.

how to change targetName space in generated jaxb classes

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

Categories

Resources