How can I convert the below xsd file to java file? - java

plz help me to convert the below xsd into java code. I am using maven also.
![an xsd for login web service]
<xs:element name="loginRequest">
<xs:complexType>
<xs:sequence>
<xs:element name="channel" type="xs:string"/>
<xs:element name="username" type="xs:string"/>
<xs:element name="password" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="loginResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="response" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>

You can use xjc, which comes with your Java development kit to create Java classes from an XML schema file. You can run this from the command line:
xjc schemafile.xsd
You'll have to wrap the elements like this:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- your elements -->
</xs:schema>

Related

Can't generate Java code with the bundled Jaxb for Intellij

I have to modify a project using XSD validation. I had got an XML, I transformed it to XSD via Intellij and then generated the associated code with the JAXB tool. All was fine.
But for the project needs, I had to modify the entire structure of my XML, I did it. I generate the XSD as well. But, When I tried to generate the Java code via the jaxb tools, it was impossible.
When I click in this jaxb tool, it just generated another XSD.
How can I do to generate, once again, the code ?
Thank you !
There is my XSD.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="PurgeConfigurationRacine" type="PurgeConfigurationRacineType"/>
<xs:complexType name="BusinessBaseType">
<xs:sequence>
<xs:element type="ArchiveDataOperationType" name="ArchiveDataOperation"/>
<xs:element type="PurgeDataOperationType" name="PurgeDataOperation"/>
<xs:element type="DeleteArchivedDataOperationType" name="DeleteArchivedDataOperation"/>
</xs:sequence>
<xs:attribute type="xs:string" name="dataSourceRef"/>
</xs:complexType>
<xs:complexType name="SqlQueryType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:string" name="name" use="optional"/>
<xs:attribute type="xs:string" name="step" use="optional"/>
<xs:attribute type="xs:string" name="multi" use="optional"/>
<xs:attribute type="xs:string" name="param" use="optional"/>
<xs:attribute type="xs:string" name="value" use="optional"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="ArchiveDataOperationType">
<xs:sequence>
<xs:element type="SqlQueryType" name="SqlQuery" maxOccurs="unbounded" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="PurgeDataOperationType">
<xs:sequence>
<xs:element type="SqlQueryType" name="SqlQuery" maxOccurs="unbounded" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="PurgeConfigurationRacineType">
<xs:sequence>
<xs:element type="BusinessBaseType" name="BusinessBase"/>
<xs:element type="DataBaseType" name="DataBase"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="DeleteArchivedDataOperationType">
<xs:sequence>
<xs:element type="SqlQueryType" name="SqlQuery" maxOccurs="unbounded" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="DataBaseType">
<xs:sequence>
<xs:element type="ArchiveDataOperationType" name="ArchiveDataOperation"/>
<xs:element type="PurgeDataOperationType" name="PurgeDataOperation"/>
</xs:sequence>
<xs:attribute type="xs:string" name="dataSourceRef"/>
</xs:complexType>
</xs:schema>
The problem that you are facing is a known issue, it is due to the fact that you have an attribute called value which is a protected name, such that you need to specify to xjc that it has to use a different name, it can be done by adding some meta information in your XSD file as next:
<xs:attribute type="xs:string" name="value" use="optional">
<xs:annotation>
<xs:appinfo>
<jaxb:property name="sqlQueryTypeValue"/>
</xs:appinfo>
</xs:annotation>
</xs:attribute>
You will also need to add the corresponding namespace declaration and the jaxb version to use, for example:
<xs:schema ... xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.1">
More information about how to customize JAXB binding

How to handle array in xml / xsd for generating a POJO class?

Sample XML response from REST WS -
<UserInfoDataContract xmlns="http://schemas.datacontract.org/2004/07/Interzoic.SSO.Shared" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<DisplayName>Test User</DisplayName>
<Email>test#test.com</Email>
<FirstName>Test</FirstName>
<IsSuperUser>false</IsSuperUser>
<LastName>User</LastName>
<Password>testuser1</Password>
<PortalID>0</PortalID>
<Roles xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<a:string>Registered Users</a:string>
</Roles>
<UserID>43</UserID>
<Username>testuser</Username>
</UserInfoDataContract>
XSD generated using http://xmlgrid.net/xml2xsd.html
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<!-- XML Schema Generated from XML Document on Thu Apr 09 2015 11:18:33 GMT-0500 (CDT) -->
<!-- with XmlGrid.net Free Online Service http://xmlgrid.net -->
<xs:element name="UserInfoDataContract">
<xs:complexType>
<xs:sequence>
<xs:element name="DisplayName" type="xs:string"></xs:element>
<xs:element name="Email" type="xs:string"></xs:element>
<xs:element name="FirstName" type="xs:string"></xs:element>
<xs:element name="IsSuperUser" type="xs:string"></xs:element>
<xs:element name="LastName" type="xs:string"></xs:element>
<xs:element name="Password" type="xs:string"></xs:element>
<xs:element name="PortalID" type="xs:int"></xs:element>
<xs:element name="Roles">
<xs:complexType>
<xs:sequence>
<xs:element name="a:string" type="xs:string"></xs:element>
</xs:sequence>
<xs:attribute name="xmlns:a" type="xs:string"></xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="UserID" type="xs:int"></xs:element>
<xs:element name="Username" type="xs:string"></xs:element>
</xs:sequence>
<xs:attribute name="xmlns" type="xs:string"></xs:attribute>
<xs:attribute name="xmlns:i" type="xs:string"></xs:attribute>
</xs:complexType>
</xs:element>
When i try to create JAXB Classes from the above XSD in eclipse, it gives me errors related to
<xs:attribute name="xmlns" type="xs:string"></xs:attribute>
<xs:attribute name="xmlns:i" type="xs:string"></xs:attribute>
and
<xs:attribute name="xmlns:a" in Roles element
and
<xs:element name="a:string" type="xs:string"></xs:element>
So i removed them and added
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.datacontract.org/2004/07/Interzoic.SSO.Shared" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays" targetNamespace="http://www.w3.org/2001/XMLSchema-instance" elementFormDefault="qualified" attributeFormDefault="unqualified">
on the top.
How will the "Roles" from my XML be referenced in the XSD so that i can create a correct POJO class?
Taking reference from http://www.w3.org/TR/xmlschema-0/#ListDt, lists should be declared this way
<xsd:simpleType name="listOfMyIntType">
<xsd:list itemType="myInteger"/>
</xsd:simpleType>
I am not able to figure out how i can apply this to my XSD. Any help will be appreciated.
I think in xml in
<Roles xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
the xmlns:a
is considered as attribute by the ide that generates xsd, that's why in generated schema you have
<xs:attribute name="xmlns" type="xs:string"></xs:attribute>
So I agree with deleting
<xs:attribute name="xmlns:a" type="xs:string"></xs:attribute>
<xs:attribute name="xmlns" type="xs:string"></xs:attribute>
<xs:attribute name="xmlns:i" type="xs:string"></xs:attribute>
from xsd.
My only guess is try setting
targetNamespace="http://schemas.datacontract.org/2004/07/Interzoic.SSO.Shared"
as it is used explicitly in your xml

Generating parent tag for a xs:sequence elements

In my xsd, I have something like this:
<xs:complexType name="Disk">
<xs:attribute name="index" type="xs:int"></xs:attribute>
<xs:attribute name="providerId" type="xs:int"></xs:attribute>
<xs:attribute name="size" type="xs:int"></xs:attribute>
</xs:complexType>
<xs:complexType name="Server">
<xs:sequence>
<xs:element name="Disk" maxOccurs="unbounded" type="Disk"></xs:element>
</xs:sequence>
</xs:complexType>
I generated JAXB classes from above xsd and create an object of type Server and populated the arraylist named disk. Now when I try to marshal Server object to xml, I see:
<server><Disk index="0" providerId="123" size="10000"/><Disk index="1" providerId="1234" size="10000"/></server>
Is there any way by which I should see XML like:
<server><Disks><Disk index="0" providerId="123" size="10000"/><Disk index="1" providerId="1234" size="10000"/></Disks></server>
I want the list of disks xml tags to be enclosed in the tag. If it is possible, how to do it?
If u want tag can occurs many times in server tag use this.
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="Disk">
<xs:attribute name="index" type="xs:int"></xs:attribute>
<xs:attribute name="providerId" type="xs:int"></xs:attribute>
<xs:attribute name="size" type="xs:int"></xs:attribute>
</xs:complexType>
<xs:complexType name="Server">
<xs:sequence>
<xs:element name="Disks" maxOccurs="unbounded" type="Disks"></xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Disks">
<xs:sequence>
<xs:element name="Disk" maxOccurs="unbounded" type="Disk"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>
Or if you want Disks tag only once use following XSD
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="Disk">
<xs:attribute name="index" type="xs:int"></xs:attribute>
<xs:attribute name="providerId" type="xs:int"></xs:attribute>
<xs:attribute name="size" type="xs:int"></xs:attribute>
</xs:complexType>
<xs:complexType name="Server">
<xs:sequence>
<xs:element name="Disks" minOccurs="0" maxOccurs="1" type="Disks"></xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Disks">
<xs:sequence>
<xs:element name="Disk" maxOccurs="unbounded" type="Disk"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>

Create Login System with JAXB and EclipseLink

I want to create an XML schema that contains
<root>
<login>
<username> </username>
<paswword> </paswword>
</login>
</root>
How can i read this values and use them for authentication, with or without JAXB.
Here is the XML schema of your document:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="paswword" type="xs:string"/>
<xs:element name="username" type="xs:string"/>
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element ref="login"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="login">
<xs:complexType>
<xs:sequence>
<xs:element ref="username"/>
<xs:element ref="paswword"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Give that schema to some king of script that you are using to generate JAXB objects, pack them into a jar, put it into your classpath and use for your any purposes.

How to add inline schema in java with xml

I make xml file using Jaxb from a xml schema my existing xml file looks like
<transaction>
<id>
<in>computer</in>
<sn>1234567</sn>
<book>JAVA</book>
<author>klen</author>
</id>
<data>
<dateTime>2011-06-24T17:08:36.3727674+05:30</dateTime>
<key>Err</key>
</data>
</transaction>
but I want to add inline schema before <id> node
My schema is looking like
<xs:schema id="transaction" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="transaction" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="id">
<xs:complexType>
<xs:sequence>
<xs:element name="in" type="xs:string" minOccurs="0" />
<xs:element name="sn" type="xs:string" minOccurs="0" />
<xs:element name="book" type="xs:string" minOccurs="0" />
<xs:element name="author" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="data">
<xs:complexType>
<xs:sequence>
<xs:element name="dateTime" type="xs:dateTime" minOccurs="0" />
<xs:element name="key" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="productData">
<xs:complexType>
<xs:sequence>
<xs:element name="dateTime" type="xs:dateTime" minOccurs="0" />
<xs:element name="key" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
now I want to add this before node under node.
Is it possible using Jaxb in first time?I cannot generate xml file with inline schema using jaxb.Due to the large size of my xml I cannot do this using Dom parser.Presently I try that at first generate xml file with data using jaxb and then rewrite the xml file and add schema under <transaction> node before <id> node.
I will prefer sun provided api.I am new in java so if some codesnips will be added it will be help full for me.

Categories