I'm using xjc via maven to generate sources. I'm using an XSD and a bindings file. I would like my generated classes to have the annotation #XmlType(name = ""). I can't see how to set the name to be blank.
I've tried (amongst other ideas) annotating using annox:annotate("http://annox.dev.java.net") with annox:class="javax.xml.bind.annotation.XmlType" but this adds another #XmlType annotation rather than replacing/overwriting the existing one.
Is there a way to set the #XmlType's name to be blank?
The name is left blank if the Type is an anonymous type. Check here (Section "Mapping a Class").
To do that, you need to declare your type inside an <element> tag. The following schema shows an example:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Container">
<xs:complexType>
<xs:sequence>
<xs:element ref="Item" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Item">
<xs:complexType >
</xs:complexType>
</xs:element>
</xs:schema>
Here, element Item is of an anonymous type, and here's the generated class:
#XmlType(name = "")
#XmlRootElement(name = "Item")
public class Item {
}
Related
Trying to work with castor oxm using xsd.
How to describe in XSD very simple element witch does not contain anything: no text, no attributes, no nested elements, just own element :
<ComplexElement>
<VerySimpleElement/>
<Element>42</Element>
</ComplexElement>
?
If I use :
<xs:element name="ComplexElement">
<xs:complexType>
<xs:sequence>
<xs:choice>
<xs:element name="VerySimpleElement"/>
<!-- there will be more -->
</xs:choice>
<xs:element ref="DoesNotMatter"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Castor Code Gen gives me VerySimpleElement as ... an Object!!! :
/**
* Field _verySimpleElement.
*/
private java.lang.Object _verySimpleElement;
And the only way to fill it correct - put there as Object ... :
new AnyNode(AnyNode.ELEMENT, "VerySimpleElement", null, null, null);
It looks like so ugly...
Have somebody any ideas?
Apologies if this is a duplicate question, but I could not find anything for my situation. So here goes:
I have the following in an xsd file:
<xs:complexType name="Allocation">
<xs:annotation>
<xs:documentation>Links its owner to an xs:id.</xs:documentation>
</xs:annotation>
<xs:attribute name="idRef" type="xs:IDREF"/>
</xs:complexType>
<xs:complexType name="XYZ">
<xs:sequence>
<xs:element name="SomeAllocation" type="Allocation"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="ABC">
<xs:sequence>
<xs:element name="SomeAllocation" type="Allocation"/>
</xs:sequence>
</xs:complexType>
I am try to generate fields/getters and setters in java that are of specific types for example XYZ.getSomeAllocation() should return type Object1 and ABC.getSomeAllocation() should return type Object2. The problem I am facing is that xjc is generating one Allocation class and the XYZ and ABC classes with the methods mentioned below returning java.lang.Object types.
Obviously creating to different Allocation types which are then used in the different objects solves the problem but I would like to reuse the object holding the xs:IDREF.
Your help is highly appreciated!
Thanks!
I need to generate following schema from java class using JAXB.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xdb="http://xmlns.oracle.com/xdb">
<xs:element name="test" type="test"/>
<xs:complexType name="testName" xdb:SQLType="WEBY_TEST_NAME">
<xs:sequence>
<xs:element minOccurs="0" name="date" type="xs:dateTime"/>
<xs:element name="id" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
How to add xdb:SQLType="WEBY_TEST_NAME" into complexType element using jaxb annotations ?
i done same try for generating the schema for different tags, but the names which include reserve words or else need to declare as
#XmlElement(name="class")
public String getClasss() {
return classs;
}
in pojo, so at time of coding it uses name which we provide, and in java program it uses the declared variables.
may be your declaration become
#XmlElement(name="xdb:SQLType")
public String getxdbSQLType() {
return xdbSQLType;
}
Let's assume we defined a collection type in XSD as
<xs:complexType name="Foos">
<xs:sequence>
<xs:element name="foo" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:all>
<xs:element name="bar" type="xs:string"/>
<xs:element name="baz" type="xs:string"/>
</xs:all>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
When generating Java code from it using XJC the type roughly translates to
public class Foos {
public List<Foos.Foo> getFoos();
public static class Foo {
public String getBar();
public String getBaz();
}
}
As the collection type is part of some other type such as the root of the document, client code of the generated code looks somewhat like this
for(Foo foo : document.getFoos().getFoos())
{
//do something
}
Is there any way to make the client code less ugly without writing a wrapper manually?
It should look like this
for(Foo foo : document.getFoos())
{
//do something
}
Thanks
UPDATE
There are XJC plug-ins that people have written to generate the #XmlElementWrapper annotation instead of having the extra wrapper class.
https://github.com/dmak/jaxb-xew-plugin
ORIGINAL ANSWER
Alternative you could create the class with the #XmlElementWrapper yourself and have the generated classes reference it by doing the following:
Document
You could handcraft your own Document class to get the desired behaviour. You can get the behaviour you are looking for by leveraging the #XmlElementWrapper annotation to get a grouping element.
package forum18247182;
import java.util.*;
import javax.xml.bind.annotation.*;
public class Document {
private List<Foos.Foo> foos = new ArrayList<Foos.Foo>();
#XmlElementWrapper
#XmlElement(name="foo")
public List<Foos.Foo> getFoos() {
return foos;
}
}
XML Schema (schema.xsd)
Here is an expanded XML schema based on your fragment that I will use.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/schema"
xmlns="http://www.example.org/schema"
elementFormDefault="qualified">
<xs:element name="document" type="Document"/>
<xs:complexType name="Document">
<xs:sequence>
<xs:element name="foos" type="Foos"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Foos">
<xs:sequence>
<xs:element name="foo" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:all>
<xs:element name="bar" type="xs:string" />
<xs:element name="baz" type="xs:string" />
</xs:all>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>
Leverage Existing Class When Generating Java Model from XML Schema (binding.xml)
We will use an external binding file to indicate that during class generation we wish to use our existing class for the complex type called Document.
<jxb:bindings
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
version="2.1">
<jxb:bindings schemaLocation="schema.xsd">
<jxb:bindings node="//xs:complexType[#name='Document']">
<jxb:class ref="forum18247182.Document"/>
</jxb:bindings>
</jxb:bindings>
</jxb:bindings>
XJC Call
We will use the -b option to specify our binding file. We will also use the -p option to force the package name of the generated classes to match that of our Document class. We could also have made the package name of our Document class match the package name that results from generating classes from the XML schema.
xjc -b binding.xml -p forum18247182 schema.xsd
Demo Code
package forum18247182;
import javax.xml.bind.*;
import javax.xml.transform.stream.StreamSource;
import forum18247182.Foos.Foo;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance("forum18247182");
Unmarshaller unmarshaller = jc.createUnmarshaller();
StreamSource xml = new StreamSource("src/forum18247182/input.xml");
Document document = unmarshaller.unmarshal(xml, Document.class).getValue();
for(Foo foo : document.getFoos())
{
System.out.println(foo);
}
}
}
Output
Below is the output from running the demo code:
forum18247182.Foos$Foo#51f3336e
forum18247182.Foos$Foo#35b5a4ca
I have the following java class with the JAXB #XMLRootElement annotation
#XmlRootElement(name="ClientData")
public class ClientData {
/**
* The first address field of the person
*/
private String address1 = null;
}
which produces this xml fragment when i generate the xsd schema
<xs:complexType name="clientData">
<xs:sequence>
<xs:element minOccurs="0" name="address1" type="xs:string"/>
Is it possible to use a JAXB annotation so that the documentation details on the address1 field will be included as a xs:annotation/xs:documentention element in my final schema?
<xs:complexType name="clientData">
<xs:sequence>
<xs:element minOccurs="0" name="address1" type="xs:string">
<xs:annotation>
<xs:documentation>The first address field of the person</xs:documentation>
</xs:annotation>
</xs:element>
Simple answer: no it's not possible with builtin JAXB.
I don't know if it's possible, since I've never used it. But as far as I can tell the API doesn't support the documentation element. However, you could use the #XMLElement annotation to give your member a more descriptive name.
//Example: Code fragment
public class USPrice {
#XmlElement(name="itemprice")
public java.math.BigDecimal price;
}
<!-- Example: Local XML Schema element -->
<xs:complexType name="USPrice"/>
<xs:sequence>
<xs:element name="itemprice" type="xs:decimal" minOccurs="0"/>
</sequence>
</xs:complexType>