Reputation: 539
I try to generate a number of xml file which is containing the schema.I use jaxb to make xml file from schema but i did not able to add schema with in this xml.My desired file looks like
<transaction>
<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>
<id>
<in>abcd</in>
<sn>1234567</sn>
<book>computer</book>
<author>klen</author>
</id>
<data>
<dateTime>2011-06-24T17:08:36.3727674+05:30</dateTime>
<key>Err</key>
</data>
</transaction>
but till now i am able to generate xml file look like
<transaction>
<id>
<in>abcd</in>
<sn>1234567</sn>
<book>computer</book>
<author>klen</author>
</id>
<data>
<dateTime>2011-06-24T17:08:36.3727674+05:30</dateTime>
<key>Err</key>
</data>
</transaction>
I didn't understand how to add this schema under node.is there any way to add this schema under node using jaxb in java.Main part of my code is like
transaction.getIdOrDataOrProductData().add(id);
transaction.getIdOrDataOrProductData().add(data);
transaction.getIdOrDataOrProductData().add(productdata);
JAXBContext jaxbContext = JAXBContext.newInstance(Transaction.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT,true);
jaxbMarshaller.marshal(transaction, file);
jaxbMarshaller.marshal(transaction, System.out);
is there any way to change the code by which i can add schema with xml file.
our application actually check the file structure if it is not look like my given example then it will be deleted,So i should to follow this structure by which it will update the database.Now my question is that how can I add this my xml file using jaxb.
using C# .NET platform it is possible to generate xml file with schema.is it possible in java.
Upvotes: 2
Views: 2619
Reputation:
I know using C# in .NET platform you can do this thing, xml file with schema definition can be possible. I think in java it is not possible.
Upvotes: 0
Reputation: 4319
There is nothing in your schema that says that the schema itself should be added to the instance document.
You would need something like:
<xs:element name="transaction" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<!-- Adding an element that can supports the schema definition -->
<xs:element ref="xs:schema" minOccurs="0" maxOccurs="1"/>
<!-- and from here on, what you already have : -->
<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>
...
And then you'd have to insert the schema into your transaction JAXB object for it to show up in your instance document.
Now, I would like to know more about your use case: are you sure you need to do this ? The document itself (the transaction XML) could simply specify the schema it conforms to, and even specify a location e.g. :
<transaction xmlns="urn:mytransactionschema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.com/transaction.xsd" >
This should give consumers of your instance document the ability to validate the XML better than by including it in the instance doc.
Upvotes: 1