gpt4 book ai didi

java - 在 JAXB 中表示多个元素名称

转载 作者:行者123 更新时间:2023-11-30 09:01:59 26 4
gpt4 key购买 nike

我有下面的 XML,它表示一个调用事件,需要解码为一个 Java 对象。我正在使用 JAXB 并且能够处理其中的大部分,除了将值集合表示为元素名称的 block 。

如果没有为每个可能的元素创建一个类(即给定的 here ),有没有办法在 JAXB 中更动态地表示它?

<message xmlns='jabber:client' from='pubsub.server.example.com' to='leon@server.example.com'
id='leon_office_vmstsp_default__leon@server.example.com__uz8mQ'>
<event xmlns='http://jabber.org/protocol/pubsub#event'>
<items node='leon_office_vmstsp_default'>
<item id='leon_office_vmstsp_default'>
<callstatus xmlns='http://xmpp.org/protocol/openlink:01:00:00#call-status'
busy='false'>
<call>
<id>1411743786867</id>
<profile>leon_office</profile>
<interest>leon_office_vmstsp_default</interest>
<changed>State</changed>
<state>CallEstablished</state>
<direction>Outgoing</direction>
<duration>3</duration>
<caller>
<number>6001</number>
<name>leon_office</name>
</caller>
<called>
<number>3807</number>
<name>3807</name>
</called>
<actions>
<ClearCall />
<RemoveThirdParty />
<StopVoiceDrop />
<MuteParty />
<AddThirdParty />
<SendDigit />
<ClearConnection />
<UnMuteParty />
<StartVoiceDrop />
</actions>
<participants>
<participant exten='3807' />
<participant exten='6001' />
</participants>
<features>
<feature id='Conference'>true</feature>
</features>
</call>
</callstatus>
</item>
</items>
</event>
<headers xmlns='http://jabber.org/protocol/shim'>
<header name='pubsub#subid'>aLfatHpG059mNcAe5ta11YNOAjA02l486sE1p4hK</header>
</headers>

最佳答案

JAXB java 类生成

  1. 从您的 XML 生成 XSD。您也可以使用 this link或其他工具工作正常。
  2. 通过 XJC、Maven 插件从 XSD 生成类 see here

下面是 maven-jaxb2-plugin 的例子

<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.8.1</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<args>
<arg>-Xannotate</arg>
<arg>-Xnamespace-prefix</arg>
<arg>-nv</arg>
</args>
<extension>true</extension>
<forceRegenerate>true</forceRegenerate>
<bindingDirectory>${basedir}/src/main/resources/xjb</bindingDirectory>
<bindingIncludes>
<include>*.xjb</include>
</bindingIncludes>
<schemas>
<schema>
<fileset>
<directory>${basedir}/src/main/resources/xsd</directory>
<includes>
<include>*.xsd</include>
</includes>
</fileset>
</schema>
</schemas>
<debug>true</debug>
<verbose>true</verbose>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>0.6.2</version>
</plugin>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics-annotate</artifactId>
<version>0.6.2</version>
</plugin>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-namespace-prefix</artifactId>
<version>1.1</version>
</plugin>
</plugins>
</configuration>
</plugin>

希望对您有所帮助。

关于java - 在 JAXB 中表示多个元素名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26183053/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com