JAXB解组并嵌入了XSD [英] JAXB unmarshall and embedded XSD
问题描述
我有一个嵌入XSD的XML,所以它类似于:
I have an XML who embed the XSD, so it's something like :
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Table">
<xs:complexType>
<xs:sequence>
<xs:element name="elem1" type="xs:string" minOccurs="0" />
<xs:element name="elem2" type="xs:string" minOccurs="0" />
<xs:element name="elem3" type="xs:string" minOccurs="0" />
<xs:element name="elem4" type="xs:string" minOccurs="0" />
<xs:element name="elem5" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<Table>
<elem1>random1</elem1>
<elem2/>
<elem3>random3</elem3>
<elem4>random4</elem4>
<elem5>random5</elem5>
</Table>
</NewDataSet>
我目前正在使用嵌入式XSD的克隆作为Schema:
I'm currently using the a clone of the embedded XSD as Schema :
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Table">
<xs:complexType>
<xs:sequence>
<xs:element name="elem1" type="xs:string" minOccurs="0" />
<xs:element name="elem2" type="xs:string" minOccurs="0" />
<xs:element name="elem3" type="xs:string" minOccurs="0" />
<xs:element name="elem4" type="xs:string" minOccurs="0" />
<xs:element name="elem5" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
但问题是,当我想解组条目XML时,我收到以下错误消息:
But the problem is that, when i want to unmarshal an entry XML I get the following error message :
意外元素(uri: http://www.w3.org/2001/XMLSchema ,local:schema)。预期的元素是< {} Table>
unexpected element (uri:"http://www.w3.org/2001/XMLSchema", local:"schema"). Expected elements are <{}Table>
我试图在我的XSD上添加标签但是没有成功,仍然会收到错误,因为命名空间(我猜?)。
任何帮助和解释都会非常感激。
I tried to add the tag on my XSD but without success, still get an error because of the namespace (i guess ?).
Any help and explanations would be really appreciate.
推荐答案
您可以在 XMLStreamReader
上利用StAX StreamFilter
,以便不将与XML模式对应的元素报告为事件。然后你可以使用JAXB从 XMLStreamReader
解组。
You can leverage a StAX StreamFilter
on an XMLStreamReader
so that the elements corresponding to the XML schema are not reported as events. Then you can unmarshal from the XMLStreamReader
with JAXB.
- JAXB filtered parsing
这篇关于JAXB解组并嵌入了XSD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!