How to add inline schema in java with xml - java

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.

Related

Prefix not appeared n on generated XML

I've this XSD Schema definition
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:ns2="http://www.xxxx.xxxxxxxxx.xxxxxxxx/xxxx/xxxx">
<xs:element name="myRootElemnt">
<xs:complexType>
<xs:sequence>
<xs:element name="messageType">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:short" name="id" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element type="xs:int" name="time" />
<xs:element type="xs:short" name="date" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
The generated XML is:
But, the XML still incomplete, it must contains xmlns:ns2="http://www.xxxx.xxxxxxxxx.xxxxxxxx/xxxx/xxxx"> and the prefix ns2 for the root element , like this:
Any Solutions?

How to specify a list of complexType in XML?

I have XML specified the following:
<xs:element name="getNewsResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="newsItem" type="tns:newsList"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:simpleType name="newsList">
<xs:list itemType="tns:news"/>
</xs:simpleType>
<xs:complexType name="news">
<xs:sequence>
<xs:element name="id" type="xs:string"/>
<xs:element name="date" type="xs:string"/>
<xs:element name="author" type="tns:author"/>
<xs:element name="title" type="xs:string"/>
<xs:element name="shortDescription" type="xs:string"/>
<xs:element name="content" type="xs:string"/>
</xs:sequence>
</xs:complexType>
I would like to have a list of the news in my response. However when I would like to create Java object with jaxb2 the xml returns the folllowing error when I run mvn clean compile -X:
org.xml.sax.SAXParseException: cos-st-restricts.1.1: The type 'newsList' is atomic, so its {base type definition}, 'tns:news', must be an atomic simple type definition or a built-in primitive datatype.
How I should change my XML to be able to compile?
In addition to using the built-in list types, you can create new list types by derivation from existing atomic types. You cannot create list types from existing list types, nor from complex types.
https://www.w3.org/TR/xmlschema-0/#ListDt
This is from one of my working XSD, a user with multiple addresses:
<xs:complexType name="user">
<xs:sequence>
<xs:element name="addresses" type="tns:addressData" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
Note that addressData is a complexType.
I guess this is what you need:
<xs:element name="getNewsResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="newsItems" type="tns:news" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="news">
<xs:sequence>
<xs:element name="id" type="xs:string"/>
<xs:element name="date" type="xs:string"/>
<xs:element name="author" type="tns:author"/>
<xs:element name="title" type="xs:string"/>
<xs:element name="shortDescription" type="xs:string"/>
<xs:element name="content" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://pro.com/balcao/xdto"
xmlns:tns="http://pro.com/balcao/xdto"
elementFormDefault="qualified">
<xs:element name="salvarCliente" >
<xs:complexType>
<xs:sequence>
<xs:element name="nome" type="xs:string" />
<xs:element name="telefone" type="xs:string" />
<xs:element name="provincia" type="xs:string" />
<xs:element name="municipio" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="listarClientes">
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name="clientes" type="tns:cliente" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="cliente">
<xs:sequence>
<xs:element name="nome" type="xs:string" />
<xs:element name="telefone" type="xs:string" />
<xs:element name="provincia" type="xs:string" />
<xs:element name="municipio" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:schema>

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>.

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>

Java jaxb xsd how to allow maxOccurence

I want following xml output and have following xsd
<msgBody>
<Contato>
<cd_id>11</cd_id>
<property key="name" value="Adde" />
<property key="Phone" value="34343" />
<property key="Address" value="address" />
</Contato>
<Contato>
<cd_id>12</cd_id>
<property key="name" value="BJ" />
<property key="Phone" value="7899" />
<property key="Address" value="sdfkjsdfsdf" />
</Contato>
</msgBody>
I have following xsd where i don't know how can i allow to have multiple property tags
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="msgBody">
<xs:complexType>
<xs:sequence>
<xs:element name="Contato" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:int" name="cd_id"/>
<xs:element type="property_data_type" name="property"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="property_data_type">
<xs:sequence>
<xs:element type="xs:string" name="key"/>
<xs:element type="xs:string" name="value"/>
</xs:sequence>
Try this
<xs:element name="msgBody">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="Contato"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="contao">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:int" name="cd_id"/>
<xs:element maxOccurs="unbounded" ref="propertytype"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="propertytype">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="key"/>
<xs:element type="xs:string" name="value"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Please add maxOccurs.
<xs:element type="property_data_type" name="property" maxOccurs="unbounded"/>
Alternatively, you can put any concrete limit like:
<xs:element type="property_data_type" name="property" maxOccurs="10"/>
It means you can only have a maximum of 10 property elements. The <maxOccurs> indicator specifies the maximum number of times an element can occur.

Categories