jaxb设置字符串类型
如何让 JAXB 编组字符串或原始数据类型并在结果 XML 中设置 type="string" 或 type="int"。我尝试为每个字段指定特定类型
@XmlSchemaType(name = "string",namespace = "http://www.w3.org/2001/XMLSchema",type = String.class)
,但这并没有什么区别,结果 XML 没有类型。
感谢您阅读
更新 这基本上就是我在 JaxB 类中所拥有的内容:
@XmlElement(required = true)
protected Keys keys;
protected String workflowID;
protected String fromFigure;
protected String fromPort;
这是生成的 XML
<keys type="draw2d.ArrayList">
<data type="Array">
<element type="draw2d.FlowConnectionModel">
<workflowID>d8f71b92-dc69-4115-9095-d748265d4e68</workflowID>
<fromFigure>706531d9-cd03-4347-9ba2-d9b525035e0d</fromFigure>
<fromPort>out_right_initialState</fromPort>
请注意,有一个用于键、数据和元素类型的类型集,但没有一个用于工作流 ID、fromFigure 和 fromPort 的原始数据类型的类型集。我想要的是这样的:
<keys type="draw2d.ArrayList">
<data type="Array">
<element type="draw2d.FlowConnectionModel">
<workflowID type="string">d8f71b92-dc69-4115-9095-d748265d4e68</workflowID>
<fromFigure type="string">706531d9-cd03-4347-9ba2-d9b525035e0d</fromFigure>
<fromPort type="string">out_right_initialState</fromPort>
How can I get JAXB to marshall a string or primitive data type and set the type="string", or type="int" in the resulting XML. I have tried specifying a specific type for each field
@XmlSchemaType(name = "string",namespace = "http://www.w3.org/2001/XMLSchema",type = String.class)
But this makes no difference the resulting XML is without type.
Thanks for reading
Update
this is basically what I have in a JaxB class:
@XmlElement(required = true)
protected Keys keys;
protected String workflowID;
protected String fromFigure;
protected String fromPort;
This is the resulting XML
<keys type="draw2d.ArrayList">
<data type="Array">
<element type="draw2d.FlowConnectionModel">
<workflowID>d8f71b92-dc69-4115-9095-d748265d4e68</workflowID>
<fromFigure>706531d9-cd03-4347-9ba2-d9b525035e0d</fromFigure>
<fromPort>out_right_initialState</fromPort>
Notice that there is a type set for the keys, data and element types but none for the primitive data types of workflowID, fromFigure and fromPort. What I want is this:
<keys type="draw2d.ArrayList">
<data type="Array">
<element type="draw2d.FlowConnectionModel">
<workflowID type="string">d8f71b92-dc69-4115-9095-d748265d4e68</workflowID>
<fromFigure type="string">706531d9-cd03-4347-9ba2-d9b525035e0d</fromFigure>
<fromPort type="string">out_right_initialState</fromPort>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不会在生成的 XML 中看到任何差异。如果您从类中生成 XSD,您预计会看到 XSD 的差异。
You will not see any difference in produced XML. You are expected to see difference in XSD if you generate it from your class.
最后,我不得不更改架构以使用复杂类型而不是简单类型。
原始模式的片段
更改为
带有额外的复杂类型
现在,我在对指定的每种类型进行编组时得到了所需的内容。
感谢那些回复和阅读的人。
In the end I had to change the schema to use complex types as opposed to simple types.
A snippet from the original schema
Changed to this
With extra complex types
And now I get what I need when marshalling with every type specified.
Thanks for those that responded and read.