I am using HyperJAXB sample code of purchase order from http://confluence.highsource.org/display/HJ3/Purchase+Order+Tutorial This example works fine for me. I Am able to create java objects from XSD and persist it into DB. Now I am trying with my XML and XSD.
I am not able to bind following xsd using JAXB. I am using eclipse built in tool as well as HyperJAXB, but both failed to parse my XSD.
I have generated this xsd from xml using online tool: http://www.freeformatter.com/xsd-generator.html
There are multiple problems:
xml:lang=" en-us - gives an eror for this and even if we remove this, there is problem in binding.
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.5.1:testCompile (default-testCompile) on project hyperjaxb3-ejb-template-basic-maven: Comp
ilation failure: Compilation failure:
[ERROR] E:\App\SampleCode\hyperjaxb3-ejb-template-basic-0.5.6-maven-src\hyperjaxb3-ejb-template-basic-0.5.6\target\generated-sources\xjc\generated\CXML.java:[599,7] error
: duplicate class: generated.CXML
[ERROR] E:\App\SampleCode\hyperjaxb3-ejb-template-basic-0.5.6-maven-src\hyperjaxb3-ejb-template-basic-0.5.6\src\test\java\generated\ObjectFactory.java:[32,7] error: dupli
cate class: generated.ObjectFactory
It generates only two classes, not rest of classes
generated\CXML.java
generated\ObjectFactory.java
Can someone help me making this XSD to bind successfully and generate java objects out of it.
XSD:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified">
<xs:element name="cXML">
<xs:complexType>
<xs:sequence>
<xs:element name="Header">
<xs:complexType>
<xs:sequence>
<xs:element name="From">
<xs:complexType>
<xs:sequence>
<xs:element name="Credential">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="Identity"/>
</xs:sequence>
<xs:attribute type="xs:string" name="domain"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="To">
<xs:complexType>
<xs:sequence>
<xs:element name="Credential">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="Identity"/>
</xs:sequence>
<xs:attribute type="xs:string" name="domain"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Sender">
<xs:complexType>
<xs:sequence>
<xs:element name="Credential">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="Identity"/>
</xs:sequence>
<xs:attribute type="xs:string" name="domain"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Request">
<xs:complexType>
<xs:sequence>
<xs:element name="OrderRequest">
<xs:complexType>
<xs:sequence>
<xs:element name="OrderRequestHeader">
<xs:complexType>
<xs:choice maxOccurs="unbounded" minOccurs="0">
<xs:element name="ID">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:short">
<xs:attribute type="xs:string" name="domain"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="Total">
<xs:complexType>
<xs:sequence>
<xs:element name="Money">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:float">
<xs:attribute type="xs:string" name="currency"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Shipping">
<xs:complexType>
<xs:sequence>
<xs:element name="Money">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:float">
<xs:attribute type="xs:string" name="currency"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="Description">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute ref="xml:lang"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Tax">
<xs:complexType>
<xs:sequence>
<xs:element name="Money">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:float">
<xs:attribute type="xs:string" name="currency"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Extrinsic">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:string" name="name"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="BillTo">
<xs:complexType mixed="true">
<xs:sequence>
<xs:element name="Address" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="CompanyName"/>
<xs:element name="ContactId">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:int">
<xs:attribute type="xs:string" name="domain"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element type="xs:string" name="PaymentType"/>
<xs:element name="PostalAddress">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="FirstName"/>
<xs:element type="xs:string" name="LastName"/>
<xs:element type="xs:string" name="Street"/>
<xs:element type="xs:string" name="City"/>
<xs:element type="xs:string" name="State"/>
<xs:element name="Country">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:string" name="isoCountryCode"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element type="xs:int" name="PostalCode"/>
<xs:element type="xs:string" name="Email"/>
<xs:element type="xs:long" name="WorkPhone"/>
</xs:sequence>
<xs:attribute type="xs:string" name="name"/>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute type="xs:int" name="contactId"/>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute type="xs:string" name="name" use="optional"/>
</xs:complexType>
</xs:element>
<xs:element name="Contact">
<xs:complexType>
<xs:sequence>
<xs:element name="ID">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:string" name="domain"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element type="xs:string" name="Name"/>
<xs:element type="xs:string" name="Email"/>
<xs:element type="xs:string" name="Company"/>
<xs:element type="xs:string" name="Department"/>
</xs:sequence>
<xs:attribute type="xs:string" name="Role"/>
</xs:complexType>
</xs:element>
</xs:choice>
<xs:attribute type="xs:string" name="orderDate"/>
<xs:attribute type="xs:string" name="type"/>
</xs:complexType>
</xs:element>
<xs:element name="ItemOut">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="Type"/>
<xs:element name="ItemId">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:int">
<xs:attribute type="xs:string" name="domain"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element type="xs:string" name="StatusType"/>
<xs:element name="PostageAmount">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:float">
<xs:attribute type="xs:string" name="currency"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="Discount">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:float">
<xs:attribute type="xs:string" name="currency"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element type="xs:string" name="orderItemName"/>
<xs:element type="xs:string" name="orderItemDescription"/>
<xs:element type="xs:float" name="Price"/>
<xs:element name="ItemID">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="SupplierPartID"/>
<xs:element type="xs:string" name="SupplierPartAuxiliaryID"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="ItemDetail">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="URL"/>
<xs:element name="Description">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute ref="xml:lang"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="UnitPrice">
<xs:complexType>
<xs:sequence>
<xs:element name="Money">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:float">
<xs:attribute type="xs:string" name="currency"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element type="xs:string" name="UnitOfMeasure"/>
<xs:element name="Extrinsic">
<xs:complexType>
<xs:sequence>
<xs:element name="ul">
<xs:complexType>
<xs:sequence>
<xs:element name="li" maxOccurs="unbounded" minOccurs="0">
<xs:complexType mixed="true">
<xs:sequence>
<xs:element type="xs:float" name="span"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute type="xs:string" name="name"/>
</xs:complexType>
</xs:element>
<xs:element name="Classification">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:string" name="domain"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element type="xs:string" name="ManufacturerPartID"/>
<xs:element name="ManufacturerName">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute ref="xml:lang"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="ShipTo">
<xs:complexType>
<xs:sequence>
<xs:element name="Address">
<xs:complexType>
<xs:sequence>
<xs:element name="ContactId">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:int">
<xs:attribute type="xs:string" name="domain"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element type="xs:string" name="CompanyName"/>
<xs:element name="PostalAddress">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="FirstName"/>
<xs:element type="xs:string" name="LastName"/>
<xs:element type="xs:string" name="Street"/>
<xs:element type="xs:string" name="City"/>
<xs:element type="xs:string" name="State"/>
<xs:element name="Country">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:string" name="isoCountryCode"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element type="xs:int" name="PostalCode"/>
<xs:element type="xs:string" name="Email"/>
<xs:element type="xs:long" name="WorkPhone"/>
</xs:sequence>
<xs:attribute type="xs:string" name="name"/>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute type="xs:int" name="contactId"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Shipping">
<xs:complexType>
<xs:sequence>
<xs:element name="Money">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:float">
<xs:attribute type="xs:string" name="currency"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="Description">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute ref="xml:lang"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Distribution" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="Accounting">
<xs:complexType>
<xs:sequence>
<xs:element name="AccountingSegment" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="Name">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute ref="xml:lang"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="Description">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute ref="xml:lang"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute type="xs:string" name="id" use="optional"/>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute type="xs:string" name="name" use="optional"/>
</xs:complexType>
</xs:element>
<xs:element name="Charge">
<xs:complexType>
<xs:sequence>
<xs:element name="Money">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:string" name="currency" use="optional"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Comments">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute ref="xml:lang"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute type="xs:byte" name="quantity"/>
<xs:attribute type="xs:byte" name="lineNumber"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute type="xs:string" name="payloadID"/>
<xs:attribute type="xs:string" name="timestamp"/>
<xs:attribute ref="xml:lang"/>
</xs:complexType>
</xs:element>
</xs:schema>
XML:
<cXML payloadID="7ece55bf-8af9-42c4-9f5b-613ac5e00819" timestamp="12/4/2014 4:51:22 PM" xml:lang=" en-us ">
<Header>
<From>
<Credential domain="web">
<Identity>UAT Master</Identity>
</Credential>
</From>
<To>
<Credential domain="web">
<Identity>notused</Identity>
</Credential>
</To>
<Sender>
<Credential domain="web">
<Identity>notused</Identity>
</Credential>
</Sender>
</Header>
<Request>
<OrderRequest>
<OrderRequestHeader orderDate="12/4/2014 4:51:22 PM" type="new">
<ID domain="Web">645</ID>
<Total>
<Money currency="USD">129.48</Money>
</Total>
<Shipping>
<Money currency="USD">12.57</Money>
<Description xml:lang="en-US"/>
</Shipping>
<Tax>
<Money currency="USD">8.91</Money>
</Tax>
<Extrinsic name="Additional Comments or Instructions:"/>
<BillTo>
<Address contactId="210306">
<CompanyName>UAT Master</CompanyName>
<ContactId domain="Web">210306</ContactId>
<PaymentType>CREDITCARD</PaymentType>
<PostalAddress name="default">
<FirstName>test</FirstName>
<LastName>test</LastName>
<Street>Hills Road</Street>
<City>NYC</City>
<State>NY</State>
<Country isoCountryCode="USA">USA</Country>
<PostalCode>78759</PostalCode>
<Email>test#test.com</Email>
<WorkPhone>2342423434</WorkPhone>
</PostalAddress>
</Address>
</BillTo>
<Extrinsic name="Purchase Order Number:"/>
<Contact Role="buyer">
<ID domain="Web">John</ID>
<Name>John M</Name>
<Email>test#test.com</Email>
<Company>UAT Master</Company>
<Department/>
</Contact>
</OrderRequestHeader>
<ItemOut quantity="120" lineNumber="1">
<Type>PRINT</Type>
<ItemId domain="Web">210300</ItemId>
<StatusType>APPROVED</StatusType>
<PostageAmount currency="USD">0.00</PostageAmount>
<Discount currency="USD">0.00</Discount>
<orderItemName>cxml test for cc payment</orderItemName>
<orderItemDescription>test cxml only with cc payment</orderItemDescription>
<Price>108.00</Price>
<ItemID>
<SupplierPartID>BUSCRD001</SupplierPartID>
<SupplierPartAuxiliaryID/>
</ItemID>
<ItemDetail>
<URL>
http://47.59.13.45/ACV3UATRepo/NgA=/MQAzADUAOAAwAA==/2012/3/28/someImage==.jpg
</URL>
<Description xml:lang="en-US">Business Card</Description>
<UnitPrice>
<Money currency="USD">0.90</Money>
</UnitPrice>
<UnitOfMeasure>PER_PIECE</UnitOfMeasure>
<Extrinsic name="options">
<ul>
<li>
Envelope Color/Yellow=
<span>0.00</span>
</li>
<li>
Paper Type/Gloss=
<span>0.00</span>
</li>
</ul>
</Extrinsic>
<Classification domain=""/>
<ManufacturerPartID/>
<ManufacturerName xml:lang="en-US"/>
</ItemDetail>
<ShipTo>
<Address contactId="210301">
<ContactId domain="Web">210301</ContactId>
<CompanyName>UAT Master</CompanyName>
<PostalAddress name="default">
<FirstName>test</FirstName>
<LastName>test</LastName>
<Street>Hills Road</Street>
<City>NYC</City>
<State>NY</State>
<Country isoCountryCode="USA">USA</Country>
<PostalCode>78759</PostalCode>
<Email>test#test.com</Email>
<WorkPhone>2342423434</WorkPhone>
</PostalAddress>
</Address>
</ShipTo>
<Shipping>
<Money currency="USD">12.57</Money>
<Description xml:lang="en-US">Fedex Ground</Description>
</Shipping>
<Distribution>
<Accounting name="DistributionCharge">
<AccountingSegment id="">
<Name xml:lang="en-US"/>
<Description xml:lang="en-US"/>
</AccountingSegment>
<AccountingSegment id="">
<Name xml:lang="en-US"/>
<Description xml:lang="en-US"/>
</AccountingSegment>
</Accounting>
<Charge>
<Money currency="USD"/>
</Charge>
</Distribution>
<Distribution>
<Accounting name="DistributionCharge">
<AccountingSegment id="">
<Name xml:lang="en-US"/>
<Description xml:lang="en-US"/>
</AccountingSegment>
<AccountingSegment id="">
<Name xml:lang="en-US"/>
<Description xml:lang="en-US"/>
</AccountingSegment>
</Accounting>
<Charge>
<Money currency="USD"/>
</Charge>
</Distribution>
<Comments xml:lang="en-US"/>
</ItemOut>
</OrderRequest>
</Request>
</cXML>
Related
I am new to XSDs format. I am trying to convert one xsd to java bean using xjc.But on converting I am getting multiple beans rather than getting just one bean.How to obtain one single bean from the XSD?
Below 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="TradeMessage" type="TradeMessageType"/>
<xs:simpleType name="empty-string">
<xs:restriction base="xs:string">
<xs:enumeration value="" />
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="decimal-or-empty">
<xs:union memberTypes="xs:decimal empty-string" />
</xs:simpleType>
<xs:complexType name="contractingEntityType">
<xs:sequence>
<xs:element type="tradingAccountType" name="tradingAccount"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="executingEntityType">
<xs:sequence>
<xs:element type="tradingAccountType" name="tradingAccount"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="tradeFeesType">
<xs:choice maxOccurs="unbounded" minOccurs="0">
<xs:element type="tradeFees_itemType" name="tradeFees_item"/>
</xs:choice>
</xs:complexType>
<xs:complexType name="settlementCurrencyType">
<xs:sequence>
<xs:element type="xs:string" name="iSOCode"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="tradingAccountType">
<xs:sequence>
<xs:element type="xs:string" name="uniqueID"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="currencyType">
<xs:sequence>
<xs:element type="xs:string" name="iSOCode"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="tradingInstrumentType">
<xs:sequence>
<xs:element type="xs:string" name="CUSIP"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="tradeFees_itemType">
<xs:sequence>
<xs:element type="chargeCodeType" name="chargeCode"/>
<xs:element type="xs:string" name="inConsideration"/>
<xs:element type="currencyType" name="currency"/>
<xs:element type="xs:decimal" name="currencyValue"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="IncomingTradeMessageType">
<xs:sequence>
<xs:element type="IncomingTradeType" name="IncomingTrade"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="TradeMessageType">
<xs:sequence>
<xs:element type="IncomingTradeMessageType" name="IncomingTradeMessage"/>
</xs:sequence>
<xs:attribute type="xs:string" name="schemaVersion"/>
</xs:complexType>
<xs:complexType name="chargeCodeType">
<xs:sequence>
<xs:element type="xs:string" name="name"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="theirTradingAccountType">
<xs:sequence>
<xs:element type="xs:string" name="uniqueID"/>
<xs:element type="xs:string" name="organisationUniqueID"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="tradeCurrencyType">
<xs:sequence>
<xs:element type="xs:string" name="iSOCode"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="transactionTypeType">
<xs:sequence>
<xs:element type="xs:string" name="uniqueID"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="channelType">
<xs:sequence>
<xs:element type="xs:string" name="name"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="IncomingTradeType">
<xs:sequence>
<xs:element type="xs:string" name="blockTradeReference"/>
<xs:element type="xs:string" name="sourceReference"/>
<xs:element type="xs:string" name="sourceVersion"/>
<xs:element type="xs:decimal" name="nominal"/>
<xs:element type="xs:decimal" name="price"/>
<xs:element type="xs:string" name="tradeDate"/>
<xs:element type="xs:string" name="valueDate"/>
<xs:element type="transactionTypeType" name="transactionType"/>
<xs:element type="tradingInstrumentType" name="tradingInstrument"/>
<xs:element type="tradeCurrencyType" name="tradeCurrency"/>
<xs:element type="xs:string" name="cancelled"/>
<xs:element type="xs:string" name="tradeTime"/>
<xs:element type="tradeFeesType" name="tradeFees"/>
<xs:element type="xs:decimal" name="grossConsideration"/>
<xs:element type="xs:decimal" name="netConsideration"/>
<xs:element type="xs:decimal" name="settlementConsideration"/>
<xs:element type="settlementCurrencyType" name="settlementCurrency"/>
<xs:element type="decimal-or-empty" name="settlementExRate"/>
<xs:element type="xs:string" name="settlementExRateDirection"/>
<xs:element type="theirTradingAccountType" name="theirTradingAccount"/>
<xs:element type="executingEntityType" name="executingEntity"/>
<xs:element type="contractingEntityType" name="contractingEntity"/>
<xs:element type="decimal-or-empty" name="calculatedNetPrice"/>
<xs:element type="xs:string" name="dealingCapacity"/>
<xs:element type="xs:string" name="agreedPriceIndicator"/>
<xs:element type="xs:string" name="orderReference"/>
<xs:element type="xs:string" name="lastAmendedOnBusinessDate"/>
<xs:element type="xs:string" name="creationAsatDate"/>
<xs:element type="xs:string" name="bpsBranchAccountNumber"/>
<xs:element type="xs:string" name="bpsAccountType"/>
<xs:element type="xs:string" name="shortSell"/>
<xs:element type="decimal-or-empty" name="facilitationPercentage"/>
<xs:element type="xs:string" name="isStepOut"/>
<xs:element type="xs:string" name="stepOutBrokerMPID"/>
<xs:element type="xs:string" name="bargainConditionFO"/>
<xs:element type="channelType" name="channel"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
This XSD is giving me different beans for different properties but actually I would need just one bean comprising of all the properties. As per my requirement I would receive input message (as per above xsd) and convert it directly to java bean and use this bean for further processing. If there is some other better strategy ,kindly let me know.
This is how JAXB works. A complex schema type is mapped to a Java class, and since this schema has many complex types you get several Java classes. When unmarshalling an XML document, you'll get one TradeMessageType instance.
I have the following xsd
<xs:schema attributeFormDefault="unqualified"
elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="screen">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"></xs:element>
<xs:element name="controls" type="Controls"></xs:element>
</xs:sequence>
<xs:attribute name="ref" type="xs:string"></xs:attribute>
</xs:complexType>
</xs:element>
<xs:complexType name="Controls">
<xs:sequence>
<xs:element name="control" type="Control" minOccurs="0" maxOccurs="unbounded"></xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Control" abstract="true">
<xs:sequence>
<xs:element name="name" type="xs:string"></xs:element>
<xs:element name="id" type="xs:string"></xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Button">
<xs:complexContent>
<xs:extension base="Control">
<xs:sequence>
<xs:element name="action" type="xs:string"></xs:element>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="TextField">
<xs:complexContent>
<xs:extension base="Control">
<xs:sequence>
<xs:element name="value" type="xs:string"></xs:element>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
How can I generate the following xml document
<?xml version = "1.0"?>
<screen ref="10_20_25_vwopstellen">
<name>VWOpstellen</name>
<controls>
<button>
<name>btnA</name>
<id>btn_10</id>
<action>click</action>
</button>
<textfield>
<name>fldA</name>
<id>fld_20</id>
</textfield>
</controls>
</screen>
Now it is generating the following xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<screen ref="scrVWOpstellen">
<name>VWOpstellen</name>
<controls>
<control xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Button">
<name>btnA</name>
</control>
<control xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Button">
<name>btnB</name>
</control>
</controls>
</screen>
And when i read my xml document I want a list of contols containing either buttons or textfields
List --> having Button and TextField
I have no idea, i think I should use substitution group but how. I tried something but the xsd is not generating any java code.
thanks
Johan
I'd suggest using an xs:choice, and also defining a target namespace, (http://my.target.namespace in my example), which can make working with JAXB easier in general.
E.g.:
<xs:schema targetNamespace="http://my.target.namespace" xmlns:tns="http://my.target.namespace" attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="screen">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"></xs:element>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="button" type="tns:Button" />
<xs:element name="textfield" type="tns:TextField" />
</xs:choice>
</xs:sequence>
<xs:attribute name="ref" type="xs:string"></xs:attribute>
</xs:complexType>
</xs:element>
<xs:complexType name="Control" abstract="true">
<xs:sequence>
<xs:element name="name" type="xs:string"></xs:element>
<xs:element name="id" type="xs:string"></xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Button">
<xs:complexContent>
<xs:extension base="tns:Control">
<xs:sequence>
<xs:element name="action" type="xs:string"></xs:element>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="TextField">
<xs:complexContent>
<xs:extension base="tns:Control">
<xs:sequence>
<xs:element name="value" type="xs:string"></xs:element>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>
Another thought, sometimes the type derivation isn't really worth it. For something like this you might also just do:
E.g.:
<xs:schema targetNamespace="http://my.target.namespace" xmlns:tns="http://my.target.namespace" attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="screen">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"></xs:element>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="button" type="tns:Button" />
<xs:element name="textfield" type="tns:TextField" />
</xs:choice>
</xs:sequence>
<xs:attribute name="ref" type="xs:string"></xs:attribute>
</xs:complexType>
</xs:element>
<xs:complexType name="Button">
<xs:sequence>
<xs:element name="name" type="xs:string"></xs:element>
<xs:element name="id" type="xs:string"></xs:element>
<xs:element name="action" type="xs:string"></xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="TextField">
<xs:sequence>
<xs:element name="name" type="xs:string"></xs:element>
<xs:element name="id" type="xs:string"></xs:element>
<xs:element name="value" type="xs:string"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>
I have a XSD:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified"
elementFormDefault="qualified">
<xs:element name="Structure">
<xs:annotation>
<xs:documentation>Comment describing your root element</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" ref="Tag1"/>
<xs:element maxOccurs="unbounded" minOccurs="0" ref="Tag2"/>
<xs:element minOccurs="0" ref="Properties"/>
<xs:element minOccurs="0" ref="Tag3"/>
</xs:sequence>
<xs:attribute name="url"/>
</xs:complexType>
</xs:element>
<xs:element name="Tag1">
<xs:complexType>
<xs:attribute name="attr"/>
<xs:attribute name="attr2"/>
</xs:complexType>
</xs:element>
<xs:element name="Tag2">
<xs:complexType>
<xs:sequence maxOccurs="unbounded">
<xs:element minOccurs="0" ref="Object"/>
<xs:element maxOccurs="unbounded" minOccurs="0" ref="Tag2"/>
</xs:sequence>
<xs:attribute name="filter"/>
</xs:complexType>
</xs:element>
<xs:element name="Object">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="tag35">
<xs:complexType>
<xs:sequence minOccurs="0">
<xs:element maxOccurs="unbounded" minOccurs="0" ref="Tag3"/>
</xs:sequence>
<xs:attribute name="attr4"/>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="attr48"/>
</xs:complexType>
</xs:element>
<xs:element name="element52">
<xs:annotation>
<xs:documentation>Text54</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" ref="el58"/>
</xs:sequence>
<xs:attribute name="vector"/>
</xs:complexType>
</xs:element>
<xs:element name="el66" type="el66Type"/>
<xs:element name="el58">
<xs:complexType mixed="true">
<xs:attribute name="ID" type="xs:string"/>
</xs:complexType>
</xs:element>
<xs:element name="Tag3">
<xs:complexType>
<xs:sequence maxOccurs="unbounded" minOccurs="0">
<xs:element minOccurs="0" name="MetaProperty">
<xs:complexType>
<xs:sequence minOccurs="0">
<xs:any/>
</xs:sequence>
<xs:attribute name="name"/>
<xs:attribute name="value"/>
</xs:complexType>
</xs:element>
<xs:element minOccurs="0" ref="Property"/>
</xs:sequence>
<xs:attribute name="ID"/>
<xs:attribute name="language" use="optional"/>
</xs:complexType>
</xs:element>
<xs:element name="Property">
<xs:complexType>
<xs:sequence minOccurs="0">
<xs:any minOccurs="0" processContents="skip"/>
</xs:sequence>
<xs:attribute name="name"/>
<xs:attribute name="type"/>
</xs:complexType>
</xs:element>
<xs:element name="Properties">
<xs:complexType>
<xs:sequence maxOccurs="unbounded" minOccurs="0">
<xs:element minOccurs="0" ref="Property"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType mixed="true" name="el66Type">
<xs:attribute name="ID" type="xs:string"/>
</xs:complexType>
</xs:schema>
And an xml:
<Structure url="/Test/url">
<Tag1/>
<Tag2>
<Object>
<tag35/>
</Object>
</Tag2>
<Properties>
<Property name="bla" type="value1"/>
<Property name="bla2" type="value2"/>
</Properties>
</Structure>
I am doing a validation, and I am getting the following error:
org.xml.sax.SAXParseException; cvc-complex-type.2.4.a: Invalid content was found starting with element 'Tag2'. One of '{Tag1, Tag2, Tag3}' is expected.
I checked in previous questions and always the solution is related with elementFormDefault="qualified" but now it is not the case so, could it be possible that there is a bug there? Any clue, will be grateful.
Your XML is valid against your XSD as posted in your question.
The error message in your question would not be returned. It must have been from different XML or XSD documents.
As Michael Kay states in the comments:
elementFormDefault only affects local elements declarations.
elementFormDefault only comes into play when the XSD has a targetNamespace.
For more details about elementFormDefault see this answer.
I saw that this question was asked many times but I didn't find a solution to my problem. So the error is:
Error on line 2 of document file...doc.xsd: cvc-elt.1: Cannot find the declaration of element 'xs:schema'
The code in the XSD file:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Collection" >
<xs:complexType>
<xs:sequence>
<xs:element name="Description" type="xs:string"/>
<xs:element name="Recipe" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Title" type="xs:string"/>
<xs:element name="Ingredients">
<xs:complexType>
<xs:element name="Ingredient" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="name" type="xs:string" use="required"/>
<xs:attribute name="amount" type="xs:integer" use="required"/>
<xs:attribute name="unit" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:complexType>
</xs:element>
<xs:element name="Preparation">
<xs:complexType>
<xs:element name="Step" type="xs:string minOccurs="0" maxOccurs="unbounded"/>"
</xs:complexType>
</xs:element>
<xs:element name="Comment" type="xs:string" minOccurs="0"/>
<xs:element name="Nutrients">
<xs:complexType>
<xs:attribute name="proteins" type="xs:integer" use="required"/>
<xs:attribute name="carbohidrati" type="xs:NMTOKEN" use="required"/>
<xs:attribute name="fat" type="xs:integer" use="choice"/>
<xs:attribute name="vitamins" type="xs:NMTOKEN" use="required"/>
<xs:attribute name="calories" type="xs:float" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
The code in the XML file:
<?xml version="1.0" encoding="UTF-8"?>
<Collection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="src/doc.xsd">
<Description>Nu prea merge</Description>
<Recipe>
<Title>Paste</Title>
<Ingredients>
<Ingredient name="paste" amount="1" unit="pachet"></Ingredient>
</Ingredients>
<Preparation>
<Step>Fierbere</Step>
<Step>Servire</Step>
</Preparation>
<Nutrients proteins="2" carbohidrati="2" fat="da" vitamins="4" calories="1000.9"></Nutrients>
</Recipe>
</Collection>
The code in my Java class:
import java.io.File;
import java.io.IOException;
import org.jdom2.Document;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.input.sax.XMLReaders;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
public class Main {
public static void main(String[] args) {
File newFile = new File("src/doc.xsd");
Document doc = null;
SAXBuilder sbd = new SAXBuilder(XMLReaders.XSDVALIDATING);
try {
doc = sbd.build(newFile);
} catch (JDOMException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
XMLOutputter outputDoc = new XMLOutputter();
outputDoc.setFormat(Format.getPrettyFormat());
try{
outputDoc.output(doc,System.out);
//outputDoc.output(doc, new FileWriter("src/myXmlDoc2.xml"));
}
catch(Exception e){
System.out.println("Eroare la parsarea documentului XML!");
}
}
}
If anyone has any idea about the possible problem please help me!
Assuming that your XML is fixed, then you should change your XSD as follows:
Fix the extra " as mentioned by #Andreas.
Add xs:sequence under xs:complexType in two places.
Change the type of #fat to xs:string.
Change use="choice" to use="optional".
Altogether, this XSD will successfully validate your XML:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Collection" >
<xs:complexType>
<xs:sequence>
<xs:element name="Description" type="xs:string"/>
<xs:element name="Recipe" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Title" type="xs:string"/>
<xs:element name="Ingredients">
<xs:complexType>
<xs:sequence>
<xs:element name="Ingredient" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="name" type="xs:string" use="required"/>
<xs:attribute name="amount" type="xs:integer" use="required"/>
<xs:attribute name="unit" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Preparation">
<xs:complexType>
<xs:sequence>
<xs:element name="Step" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Comment" type="xs:string" minOccurs="0"/>
<xs:element name="Nutrients">
<xs:complexType>
<xs:attribute name="proteins" type="xs:integer" use="required"/>
<xs:attribute name="carbohidrati" type="xs:NMTOKEN" use="required"/>
<xs:attribute name="fat" type="xs:string" use="optional"/>
<xs:attribute name="vitamins" type="xs:NMTOKEN" use="required"/>
<xs:attribute name="calories" type="xs:float" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Of course, you will also have to check your Java code. (Hint: make sure you're not validating your XSD as XML when you really want to validating your XML against your XSD.)
I have an entity class auto-generated from xsd file;
the problem is that it hasn't any constructor and I must add it manual.
How can I specify a constructor?
Here my xsd file:
entities.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://capgemini.it/soap/entity/generated"
targetNamespace="http://capgemini.it/soap/entity/generated" elementFormDefault="qualified">
<xs:element name="getEntityRequest">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="getEntityResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="entity" type="tns:entity"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="entity">
<xs:sequence>
<xs:element name="firstName" type="xs:string"/>
<xs:element name="lastName" type="xs:string"/>
<xs:element name="cf" type="xs:string"/>
<xs:element name="role" type="xs:string"/>
<xs:element name="valid" type="xs:boolean"/>
<xs:element name="isIta" type="xs:boolean"/>
<xs:element name="sesso" type="xs:boolean"/>
<xs:element name="nationalNumber" type="xs:string"/>
<xs:element name="crs" type="xs:string"/>
<xs:element name="lvlId" type="xs:int"/>
<xs:element name="qual" type="xs:string"/>
</xs:sequence>
</xs:complexType>
Thanks for any suggestion :)