当前位置: 首页 > 知识库问答 >
问题:

具有重复缩进的JAXB列表的XSD到Java

漆雕昊天
2023-03-14
<!-- JAXB 2 -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
    <execution>
        <id>set-additional-system-properties</id>
        <goals>
            <goal>set-system-properties</goal>
        </goals>
    </execution>
</executions>
<configuration>
    <properties>
        <property>
            <name>javax.xml.accessExternalSchema</name>
            <value>file,http</value>
        </property>
    </properties>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.3.1</version>
<executions>
    <execution>
        <id>api</id>
        <goals>
            <goal>xjc</goal>
        </goals>
    </execution>
</executions>
<configuration>
    <xjbSources>
        <xjbSource>${basedir}/src/main/resources/xsd/api/api.xjb</xjbSource>
    </xjbSources>
    <sources>
        <source>${basedir}/src/main/resources/xsd/api/api.xsd</source>
    </sources>
    <packageName>com.example.ems.aaa.xsd.api</packageName>
</configuration>
</plugin>
<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings version="2.1"
               xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
               xmlns="http://www.w3.org/2001/XMLSchema">

    <jaxb:globalBindings>
        <jaxb:serializable uid="1"/>
    </jaxb:globalBindings>

</jaxb:bindings>
<complexType name="UserPrivilege">
<sequence>
    <element name="Id" type="string"></element>
    <element name="Name" type="string"></element>
    <element name="Method" type="string"></element>
    <element name="Path" type="string"></element>
</sequence>
</complexType>

<complexType name="UserPrivilegeList">
<sequence>
    <element name="Privilege" type="myapi:UserPrivilege" minOccurs="0" maxOccurs="unbounded" />
</sequence>
</complexType>

<complexType name="UserRole">
<sequence>
    <element name="Id" type="string"></element>
    <element name="Name" type="string"></element>
    <element name="Privileges" type="myapi:UserPrivilegeList"></element>
</sequence>
</complexType>
<UserRole>
  <id>17b55c53-7328-4444-95e2-648fb5f9de89</id>
  <name>CONFIGURATOR</name>
  <privileges>
    <privilege>
      <privilege>
        <id>b5f7f39a-f87c-4874-9b16-381a5e0613b3</id>
        <name>CONFIG_PUT</name>
        <method>PUT</method>
        <path>/config/**</path>
      </privilege>
      <privilege>
        <id>7045c699-5608-4584-a3d0-41a96b9d7903</id>
        <name>CONFIG_GET</name>
        <method>GET</method>
        <path>/config/**</path>
      </privilege>
    </privilege>
  </privileges>
</UserRole>
<privileges>
    <privilege>
      <privilege>

我需要将“UserRole”定义为complexType,因为我将它用作另一个complexType的另一序列中的元素。

问候。

共有1个答案

姬高澹
2023-03-14

将您的XSD(其中的一部分)更改为

<element name="UserRole">
<complexType >

<sequence>

    <element name="Id" type="string"></element>

    <element name="Name" type="string"></element>

    <element name="Privileges" type="myapi:UserPrivilegeList"></element>

</sequence>
</complexType>
</element>

Java测试代码可能看起来像

public class PrivIlegeTester {

    public static void main(String[] args) throws JAXBException {
        // TODO Auto-generated method stub
        JAXBContext context = JAXBContext.newInstance(UserRole.class);
        Marshaller m = context.createMarshaller();

        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

        UserPrivilege uPriv = new UserPrivilege();
        uPriv.setId("45455-4466");
        uPriv.setMethod("GET");
        uPriv.setName("GET-CONFIG");
        uPriv.setPath("/*");

        UserPrivilege uPriv0 = new UserPrivilege();
        uPriv0.setId("45888-4466");
        uPriv0.setMethod("POST");
        uPriv0.setName("POST-CONFIG");
        uPriv0.setPath("/*");

        UserRole ur = new UserRole();
        ur.setPrivileges(new UserPrivilegeList());
        ur.getPrivileges().getPrivilege().add(uPriv);
        ur.getPrivileges().getPrivilege().add(uPriv0);

        m.marshal(ur, System.out);


    }

}

和输出

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<userRole xmlns="http://www.example.org/Privilege">
    <Privileges>
        <Privilege>
            <Id>45455-4466</Id>
            <Name>GET-CONFIG</Name>
            <Method>GET</Method>
            <Path>/*</Path>
        </Privilege>
        <Privilege>
            <Id>45888-4466</Id>
            <Name>POST-CONFIG</Name>
            <Method>POST</Method>
            <Path>/*</Path>
        </Privilege>
    </Privileges>
</userRole>
 类似资料:
  • 我尝试使用thajaxb来整理xml,我使用xjc命令从xsd文件order.xsd生成java类 我得到了许多带注释的类,但没有一个@XmlRootElement(name="Order"),也没有名为Order的类

  • 首先,我的问题类似于这个已经回答过的问题,在Java中,将两个arrayList合并成一个新的arrayList,没有重复项,并且顺序正确。 然而,这里的不同之处在于,我尝试将两个列表合并在一起,而不仅仅是一个字符串。我的意图是合并以下类型的两个对象(为了简化,我从示例中去掉了不必要的信息): 所以我得到了一个新的项目,它有一个总结计数。这将消除不需要的重复,因为这些对象上的uniqueKey是相

  • 我需要在Java中生成xsd文件,该文件使用jaxb maven插件(http://mojo.codehaus.org/jaxb2-maven-plugin/xjc-mojo.html)将生成如下所示的XML: 我不想编辑jaxb自动生成的类或类似的东西。 类似的题目我已经查过了,还没有找到解决的办法。 提前道谢。

  • 我必须得到包含最大值的对象列表。Java8中的比较器max只返回一个对象。 为了做到这一点,我必须流式地显示两次列表: 这个解决方案有效。我想有一个更好的方法来解决这个问题,但我想不出来。

  • 我有这个字符串列表 我想根据其中的数字对这个列表进行排序。 例如,如果我有,我希望它像一样排序。 我使用了,但它返回类似的内容。 我应该怎么做才能把它按正确的顺序排列?

  • 我有一个表格结构 如何使用条件?我尝试使用这个,但结果是