[英]creating xml file with xml schema in java
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 我尝试生成一些包含架构的xml文件。我使用jaxb从架构中创建xml文件,但无法在此xml中添加架构。
<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 但直到现在我仍然能够生成xml文件,如下所示
<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 我不明白如何在node下添加这个模式。有什么办法可以在java中使用jaxb在node下添加这个模式吗?
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. 有什么办法可以更改代码,从而可以通过XML文件添加架构。
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. 我们的应用程序实际上会检查文件结构,如果它看起来不像我给定的示例,那么它将被删除。因此,我应该遵循此结构来更新数据库。现在我的问题是,如何添加此xml文件使用jaxb。
using C# .NET platform it is possible to generate xml file with schema.is it possible in java. 使用C#.NET平台,可以使用schema生成xml文件。
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. 然后,您必须将架构插入事务JAXB对象中,以使其显示在实例文档中。
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 eg : 文档本身(事务XML)可以简单地指定其遵循的架构,甚至可以指定一个位置,例如:
<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. 这应该使实例文档的使用者比将XML包含在实例文档中更好地验证XML。
I know using C# in .NET platform you can do this thing, xml file with schema definition can be possible. 我知道在.NET平台中使用C#可以做到这一点,可以使用具有模式定义的xml文件。 I think in java it is not possible. 我认为在Java中是不可能的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.