【问题标题】:How to handle array in xml / xsd for generating a POJO class?如何处理 xml / xsd 中的数组以生成 POJO 类?
【发布时间】:2015-06-15 06:07:17
【问题描述】:

来自 REST WS 的示例 XML 响应 -

<UserInfoDataContract xmlns="http://schemas.datacontract.org/2004/07/Interzoic.SSO.Shared"  xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<DisplayName>Test User</DisplayName>
<Email>test@test.com</Email>
<FirstName>Test</FirstName>
<IsSuperUser>false</IsSuperUser>
<LastName>User</LastName>
<Password>testuser1</Password>
<PortalID>0</PortalID>
<Roles xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<a:string>Registered Users</a:string>
</Roles>
<UserID>43</UserID>
<Username>testuser</Username>
</UserInfoDataContract>

使用http://xmlgrid.net/xml2xsd.html生成的XSD

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
      <!-- XML Schema Generated from XML Document on Thu Apr 09 2015 11:18:33 GMT-0500 (CDT) -->
      <!-- with XmlGrid.net Free Online Service http://xmlgrid.net -->
   <xs:element name="UserInfoDataContract">
          <xs:complexType>
                 <xs:sequence>
                        <xs:element name="DisplayName" type="xs:string"></xs:element>
                        <xs:element name="Email" type="xs:string"></xs:element>
                        <xs:element name="FirstName" type="xs:string"></xs:element>
                        <xs:element name="IsSuperUser" type="xs:string"></xs:element>
                        <xs:element name="LastName" type="xs:string"></xs:element>
                        <xs:element name="Password" type="xs:string"></xs:element>
                        <xs:element name="PortalID" type="xs:int"></xs:element>
                        <xs:element name="Roles">
                               <xs:complexType>
                                      <xs:sequence>
                                             <xs:element name="a:string" type="xs:string"></xs:element>
                                         </xs:sequence>
                                      <xs:attribute name="xmlns:a" type="xs:string"></xs:attribute>
                                  </xs:complexType>
                           </xs:element>
                        <xs:element name="UserID" type="xs:int"></xs:element>
                        <xs:element name="Username" type="xs:string"></xs:element>
                    </xs:sequence>
                 <xs:attribute name="xmlns" type="xs:string"></xs:attribute>
                 <xs:attribute name="xmlns:i" type="xs:string"></xs:attribute>
             </xs:complexType>
      </xs:element>

当我尝试在 Eclipse 中从上述 XSD 创建 JAXB 类时,它给了我与

相关的错误
<xs:attribute name="xmlns" type="xs:string"></xs:attribute>
<xs:attribute name="xmlns:i" type="xs:string"></xs:attribute> 

<xs:attribute name="xmlns:a" in Roles element

<xs:element name="a:string" type="xs:string"></xs:element>

所以我删除了它们并添加了

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.datacontract.org/2004/07/Interzoic.SSO.Shared"  xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays"  targetNamespace="http://www.w3.org/2001/XMLSchema-instance"  elementFormDefault="qualified" attributeFormDefault="unqualified">

在顶部。

如何在 XSD 中引用我的 XML 中的“角色”,以便我可以创建正确的 POJO 类?

参考http://www.w3.org/TR/xmlschema-0/#ListDt,列表应该这样声明

<xsd:simpleType name="listOfMyIntType">
<xsd:list itemType="myInteger"/>
</xsd:simpleType> 

我无法弄清楚如何将其应用于我的 XSD。任何帮助将不胜感激。

【问题讨论】:

    标签: java xml xsd jaxb


    【解决方案1】:

    我认为在xml中

    &lt;Roles xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays"&gt;

    xmlns:a 被生成 xsd 的 ide 视为属性,这就是为什么在生成的模式中你有

    <xs:attribute name="xmlns" type="xs:string"></xs:attribute>
    

    所以我同意删除

    <xs:attribute name="xmlns:a" type="xs:string"></xs:attribute>
    <xs:attribute name="xmlns" type="xs:string"></xs:attribute>
    <xs:attribute name="xmlns:i" type="xs:string"></xs:attribute>
    

    来自 xsd。

    我唯一的猜测是尝试设置

    targetNamespace="http://schemas.datacontract.org/2004/07/Interzoic.SSO.Shared"
    

    因为它在您的 xml 中明确使用

    【讨论】:

    • 我试过了,但没有任何区别。你认为我应该用什么替换 &lt;xs:element name="Roles"&gt; &lt;xs:complexType&gt;&lt;xs:sequence&gt; &lt;xs:element name="a:string" type="xs:string"&gt;&lt;/xs:element&gt; &lt;/xs:sequence&gt; &lt;xs:attribute name="xmlns:a" type="xs:string"&gt;&lt;/xs:attribute&gt; &lt;/xs:complexType&gt; &lt;/xs:element&gt; 来补偿角色的
    • 我认为您需要删除带有 xmlns 名称的属性,正如我所说。但是您的 xml 可能不会针对 xsd 进行验证。您知道他们为什么将 xmlns 放入发送给您的 xml 中吗?可能他们有自己需要包含的 xsd 架构?
    猜你喜欢
    • 2019-08-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-20
    • 1970-01-01
    • 1970-01-01
    • 2011-10-15
    • 2020-03-26
    • 2012-11-25
    相关资源
    最近更新 更多