Java class to XSD - java

Could someone please tell me what XSD matches this Java class?
public class MyClass {
private List<String> list1;
private List<String> list2;
private XMLGregorianCalendar date;
// getters and setters
}
I've tried the following, but I'm receiving an One of :attribute, :attributeGroup, :anyAttribute is expected error:
<xs:element name="myClass">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name="list1" type="xs:string"/>
</xs:sequence>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name="list2" type="xs:string"/>
</xs:sequence>
<xs:element name="date" nillable="true" type="xs:dateTime"/>
</xs:complexType>
</xs:element>
So it seems I'm not using the <xs:sequence> tag correctly. Could someone please shed some light? (I'm far from being an expert in XML-related stuff)...
I'm using Spring Boot 1.4.4.RELEASE version with Java 7.

The xs:sequence within a xs:complexType defines a specific order in which an elements children must occur. To create a list of elements you simply use the minOccurs and maxOccurs directly on the xs:element tag you want repeated, as follows:
<xs:element name="myClass">
<xs:complexType>
<xs:sequence>
<xs:element name="list1" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="list2" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="date" nillable="true" type="xs:dateTime"/>
</xs:sequence>
</xs:complexType>
</xs:element>
This should create the class you expect and require XML such as the following (order of tags matter):
<myClass>
<list1>a</list1>
<list1>b</list1>
<list2>y</list2>
<list2>z</list2>
<date>2019-06-26T00:00:00.0000000Z</date>
</myClass>
Placing minOccurs and maxOccurs on the xs:sequence tag requires the entire sequence of elements to be repeated.

Related

Don't display the element in xml if its id attribute is null

I have a xsd where there are 2 complextype elements under <Customer> complextype. One is <NormalCustomer> and <PrivilegedCustomer>. In my xml, I want either Normal or Privileged customer to be present under <Customer> tag based on the id attribute of Normal/Privileged customers.
below is my xsd
<xs:choice>
<xs:element name="normalCustomer" type="tns:normalCustomer" minOccurs="0" nillable="true"/>
<xs:element name="privilegedcustomer" type="tns:privilegedcustomer" minOccurs="0" nillable="true"/>
</xs:choice>
Complextype NormalCustomer
<xs:complexType name="normalCustomer">
<xs:sequence>
<xs:element name="id" type="xs:long"/>
<xs:element name="customerName" type="xs:string" minOccurs="1"/>
<xs:element name="customerType" type="xs:string" minOccurs="1"/>
</xs:sequence>
</xs:complexType>
Complextype PrivilegedCustomer
<xs:complexType name="privilegedCustomer">
<xs:sequence>
<xs:element name="id" type="xs:long"/>
<xs:element name="customerName" type="xs:string" minOccurs="1"/>
<xs:element name="customerType" type="xs:string" minOccurs="1"/>
</xs:sequence>
</xs:complexType>
Note: Am using JAXB for processing
Please suggest me how can I modify the tag to achieve my requirement.
Remove minOccurs="0" nillable="true" from both elements. The <xs:choice> enforces that exactly one of the elements are present. If none present are allowed, add minOccurs="0" to <xs:choice>.

Removing elements from auto generated JAXB schema

Im generating a JAXB Marshalling-Unmarshalling Tool.
I have a set of annotated java classes, selecting all i auto generated JAXB schema in eclipse. I want to remove from schema, some of the fields which i dont want for some reason (May be later ill use them). I tried removed a field from schema like below
Schema Before removing
<xs:complexType name="Address">
<xs:sequence>
<xs:element name="buildingNo" type="xs:string" minOccurs="0"/>
<xs:element name="buildingName" type="xs:string" minOccurs="0"/>
<xs:element name="city" type="xs:string" minOccurs="0"/>
<xs:element name="country" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
Schema after removing buildingNo
<xs:complexType name="Address">
<xs:sequence>
<xs:element name="buildingName" type="xs:string" minOccurs="0"/>
<xs:element name="city" type="xs:string" minOccurs="0"/>
<xs:element name="country" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
If i try to marshall address object based on above schema im getting error like buildingNo not found instead found buildingName,....
How to remove unused fields from schema??

XSD FOR XML element multiple occurrences

i'm new to xsd and i want to generate xml like below with STUDENTRECORD occurring multiple times. i'm using jaxb to generate classes on the xsd
<STUDENTDETAIL>
<STUDENTINFORMATION>
<STUDENTRECORD>
<NAME>ABC</NAME>
<CLASS>4</CLASS>
<MAJOR>SCIENCE</MAJOR>
<GRADE>A</GRADE>
</STUDENTRECORD>
<STUDENTRECORD>
<NAME>DEF</NAME>
<CLASS>4</CLASS>
<MAJOR>SCIENCE</MAJOR>
<GRADE>B</GRADE>
</STUDENTRECORD>
</STUDENTINFORMATION>
My current xsd which generates STUDENTRECORD only once.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://webservice.com/WS" targetNamespace="http://webservice.com/WS" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="Student" type="Student"/>
<xs:complexType name="Student">
<xs:sequence>
<xs:element name="STUDENTDETAIL">
<xs:complexType>
<xs:sequence>
<xs:element name="STUDENTINFORMATION">
<xs:complexType>
<xs:sequence>
<xs:element name="STUDENTRECORD">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="NAME"/>
<xs:element type="xs:string" name="CLASS"/>
<xs:element type="xs:string" name="MAJOR"/>
<xs:element type="xs:string" name="GRADE"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
Please help to fix.
Thanks
You just need to set a maxOccurs attribute on the STUDENTRECORD element declaration, like this:
<xs:element name="STUDENTRECORD" maxOccurs="unbounded">
This will allow the <STUDENTRECORD> to appear as many times you want. By default a given element is required to appear once.
Similarly, you can set minOccurs attribute to specify the minimal number of occurrences of an element.

webservices arguments instead of parameters

I'm doing Java-first development on a service and the WSDL file being generated (I'm using Tomcat v6.0 as my container with the CXF Servlet) is using arg0, arg1 etc. as the parameter names for my service methods rather than using the actual parameter name specified in my Java code. Is this a known shortcoming or am I doing something wrong? The WSDL isn't very self-documenting with parameter names like this!
Here's an example wsdl snippet:
<xs:complexType name="insertVendor">
<xs:sequence>
<xs:element minOccurs="0" name="arg0" type="xs:string"/>
<xs:element minOccurs="0" name="arg1" type="xs:string"/>
<xs:element minOccurs="0" name="arg2" type="xs:string"/>
<xs:element minOccurs="0" name="arg3" type="xs:string"/>
<xs:element minOccurs="0" name="arg4" type="xs:string"/>
<xs:element minOccurs="0" name="arg5" type="xs:string"/>
<xs:element minOccurs="0" name="arg6" type="xs:string"/>
<xs:element minOccurs="0" name="arg7" type="xs:string"/>
<xs:element minOccurs="0" name="arg8" type="xs:string"/>
<xs:element minOccurs="0" name="arg9" type="xs:string"/>
<xs:element minOccurs="0" name="arg10" type="xs:string"/>
<xs:element minOccurs="0" name="arg11" type="xs:string"/>
</xs:sequence>
Thanks,
mallesh
You can create request class and pass it in service method
Class Requestclass(){
// all the arguments you want, create POJO class
}
pass this class in your method,
insertVendor(RequestClass req){}
Here is the answer
Official answer: The JAX-WS spec (specifically section 3.6.1) mandates that it be generated this way. To customize the name, you have to use an #WebParam(name = "blah") annotation to specify better names. (You can use #WebResult for the return value, but you'll only see the results if you look at the XML.)

Jaxb generate one get method for all elements

Part of my xsd file looks like this:
<xs:complexType name="group">
<xs:sequence maxOccurs="unbounded" minOccurs="1">
<xs:element name="title" type="xs:string"/>
<xs:element name="description" type="xs:string"/>
<xs:element name="module" type="module" nillable="true" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="name" type="xs:string" />
</xs:complexType>
When I generate my classes using Jaxb in Eclipse. The "Group" class generates a method called:
public List<JAXBElement<?>> getTitleAndDescriptionAndModule();
How do I change the xsd so that I get getter for each of the elements?

Categories