JAXB从XML架构生成类并未返回所有属性的Getter Setter
我正在使用jaxb来构建Java类,以从 https:/> https:/ /gitlab.gnome.org/GNOME/gupnp-av/-/tree/master/data
It works but not as well as I would like some elements dont return getters and setters for most attributes or even a name/value地图。取而代之的是,他们只提供了我必须迭代以找到我想要的属性的所有jaxbelements的列表,为什么?
/**
* Gets the value of the allowedUnderItem property.
*
* <p>
* This accessor method returns a reference to the live list,
* not a snapshot. Therefore any modification you make to the
* returned list will be present inside the JAXB object.
* This is why there is not a <CODE>set</CODE> method for the allowedUnderItem property.
*
* <p>
* For example, to add a new item, do as follows:
* <pre>
* getAllowedUnderItem().add(newItem);
* </pre>
*
*
* <p>
* Objects of the following type(s) are allowed in the list
* {@link JAXBElement }{@code <}{@link ChannelGroupNameType }{@code >}
* {@link JAXBElement }{@code <}{@link ChannelIDType }{@code >}
* {@link JAXBElement }{@code <}{@link DayOfWeekEnLen3 }{@code >}
* {@link JAXBElement }{@code <}{@link DescType }{@code >}
* {@link JAXBElement }{@code <}{@link DeviceUDNType }{@code >}
* {@link JAXBElement }{@code <}{@link ElementType }{@code >}
* {@link JAXBElement }{@code <}{@link ElementType }{@code >}
* {@link JAXBElement }{@code <}{@link ElementType }{@code >}
* {@link JAXBElement }{@code <}{@link ElementType }{@code >}
* {@link JAXBElement }{@code <}{@link ElementType }{@code >}
* {@link JAXBElement }{@code <}{@link ElementType }{@code >}
* {@link JAXBElement }{@code <}{@link ElementType }{@code >}
* {@link JAXBElement }{@code <}{@link ElementType }{@code >}
* {@link JAXBElement }{@code <}{@link ForeignMetadataType }{@code >}
* {@link JAXBElement }{@code <}{@link GenreType }{@code >}
* {@link JAXBElement }{@code <}{@link InclusionType }{@code >}
* {@link JAXBElement }{@code <}{@link ObjectLinkRefType }{@code >}
* {@link JAXBElement }{@code <}{@link ObjectLinkType }{@code >}
* {@link JAXBElement }{@code <}{@link OwnerType }{@code >}
* {@link JAXBElement }{@code <}{@link PersonWithRoleType }{@code >}
* {@link JAXBElement }{@code <}{@link PersonWithRoleType }{@code >}
* {@link JAXBElement }{@code <}{@link PersonWithRoleType }{@code >}
* {@link JAXBElement }{@code <}{@link PreservedTimeRangeType }{@code >}
* {@link JAXBElement }{@code <}{@link PriceType }{@code >}
* {@link JAXBElement }{@code <}{@link ProgramCodeType }{@code >}
* {@link JAXBElement }{@code <}{@link ProgramIDType }{@code >}
* {@link JAXBElement }{@code <}{@link ProgramIDType }{@code >}
* {@link JAXBElement }{@code <}{@link ProgramListType }{@code >}
* {@link JAXBElement }{@code <}{@link ProgramPreservedType }{@code >}
* {@link JAXBElement }{@code <}{@link QualifiedDateTime }{@code >}
* {@link JAXBElement }{@code <}{@link QualifiedDateTime }{@code >}
* {@link JAXBElement }{@code <}{@link QualifiedDateTimeISO8601 }{@code >}
* {@link JAXBElement }{@code <}{@link QualifiedDateTimeISO8601 }{@code >}
* {@link JAXBElement }{@code <}{@link QualifiedDateTimeRange }{@code >}
* {@link JAXBElement }{@code <}{@link RatingType }{@code >}
* {@link JAXBElement }{@code <}{@link ResExtType }{@code >}
* {@link JAXBElement }{@code <}{@link ResType }{@code >}
* {@link JAXBElement }{@code <}{@link SearchClassType }{@code >}
* {@link JAXBElement }{@code <}{@link SearchClassType }{@code >}
* {@link JAXBElement }{@code <}{@link StateVariableCollectionType }{@code >}
* {@link JAXBElement }{@code <}{@link StatusType }{@code >}
* {@link JAXBElement }{@code <}{@link Boolean }{@code >}
* {@link JAXBElement }{@code <}{@link Boolean }{@code >}
* {@link JAXBElement }{@code <}{@link Boolean }{@code >}
* {@link JAXBElement }{@code <}{@link Boolean }{@code >}
* {@link JAXBElement }{@code <}{@link Integer }{@code >}
* {@link JAXBElement }{@code <}{@link Integer }{@code >}
* {@link JAXBElement }{@code <}{@link Integer }{@code >}
* {@link JAXBElement }{@code <}{@link Integer }{@code >}
* {@link JAXBElement }{@code <}{@link Long }{@code >}
* {@link JAXBElement }{@code <}{@link Long }{@code >}
* {@link JAXBElement }{@code <}{@link Long }{@code >}
* {@link JAXBElement }{@code <}{@link Long }{@code >}
* {@link JAXBElement }{@code <}{@link Long }{@code >}
* {@link JAXBElement }{@code <}{@link Long }{@code >}
* {@link JAXBElement }{@code <}{@link Long }{@code >}
* {@link JAXBElement }{@code <}{@link Long }{@code >}
* {@link JAXBElement }{@code <}{@link Long }{@code >}
* {@link JAXBElement }{@code <}{@link String }{@code >}
* {@link JAXBElement }{@code <}{@link String }{@code >}
* {@link JAXBElement }{@code <}{@link String }{@code >}
* {@link JAXBElement }{@code <}{@link String }{@code >}
* {@link JAXBElement }{@code <}{@link String }{@code >}
* {@link JAXBElement }{@code <}{@link String }{@code >}
* {@link JAXBElement }{@code <}{@link String }{@code >}
* {@link JAXBElement }{@code <}{@link String }{@code >}
* {@link JAXBElement }{@code <}{@link String }{@code >}
* {@link JAXBElement }{@code <}{@link String }{@code >}
* {@link JAXBElement }{@code <}{@link String }{@code >}
* {@link JAXBElement }{@code <}{@link String }{@code >}
* {@link JAXBElement }{@code <}{@link String }{@code >}
* {@link JAXBElement }{@code <}{@link String }{@code >}
* {@link JAXBElement }{@code <}{@link String }{@code >}
* {@link JAXBElement }{@code <}{@link String }{@code >}
* {@link JAXBElement }{@code <}{@link String }{@code >}
* {@link JAXBElement }{@code <}{@link String }{@code >}
* {@link JAXBElement }{@code <}{@link String }{@code >}
* {@link JAXBElement }{@code <}{@link String }{@code >}
* {@link JAXBElement }{@code <}{@link String }{@code >}
* {@link JAXBElement }{@code <}{@link String }{@code >}
* {@link JAXBElement }{@code <}{@link String }{@code >}
* {@link JAXBElement }{@code <}{@link String }{@code >}
* {@link JAXBElement }{@code <}{@link String }{@code >}
* {@link JAXBElement }{@code <}{@link String }{@code >}
* {@link JAXBElement }{@code <}{@link String }{@code >}
* {@link JAXBElement }{@code <}{@link String }{@code >}
* {@link JAXBElement }{@code <}{@link String }{@code >}
* {@link JAXBElement }{@code <}{@link String }{@code >}
* {@link JAXBElement }{@code <}{@link String }{@code >}
* {@link JAXBElement }{@code <}{@link String }{@code >}
* {@link JAXBElement }{@code <}{@link String }{@code >}
* {@link JAXBElement }{@code <}{@link List }{@code <}{@link String }{@code >}{@code >}
*
*
*/
public List<JAXBElement<?>> getAllowedUnderItem() {
if (allowedUnderItem == null) {
allowedUnderItem = new ArrayList<JAXBElement<?>>();
}
return this.allowedUnderItem;
}
meaning I have to write code like
if(object instanceof ContainerType)
{
id = ((ContainerType)object).getId();
title = ((ContainerType)object).getTitle().getValue();
Iterator<JAXBElement<?>> i = ((ContainerType)object).getAllowedUnderContainer().iterator();
while(i.hasNext())
{
JAXBElement next = i.next();
if(next.getName().getLocalPart().equals("albumArtURI"))
{
albumArtistUri = (String)next.getValue();
}
}
}
How can I improve this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尽管在这种情况下可能很困难,但由于您正在使用的模式的复杂性,但是您可以尝试使用 jaxb xml绑定为此。
JAXB XML绑定使您可以以不同的方式自定义JAXB Java类生成过程。
在这些自定义中,它使您可以指示生成器使用自己的类模型。
该信息应在绑定XML文件中提供; in your use case, it could be similar to this:
Note the definition of the custom classes
didl.ContainerType
anddidl.ItemType
in order to represent the complex typesContainer.Type
和item.Type
。ContainerType
could look like this:I will use the
xjc
tool in the answer but the solution should be easily portable to Maven, for example.Using
xjc
, you need to generate your code like this to tale into account the bindings file and classes requested:As you can see, we provided both the bindings file,
binding.xml
,以及classPath
生成器应在其中搜索您的自定义类。The same approach, although for simpler use cases, has been exemplified here in different questions in Stackoverflow, for example in these ones:
This Oracle article已经过时了,但是它提供了一个很好的解释,即有关字段之间的实际对应关系。
Although probably difficult in this case, due to the complexity of the schema you are working with, but you could try using JAXB XML bindings for this purpose.
JAXB XML bindings allows you to customize the JAXB Java classes generation process in different ways.
Among these customizations, it allows you to instruct the generator to use your own model of classes.
This information should be provided in a bindings XML file; in your use case, it could be similar to this:
Note the definition of the custom classes
didl.ContainerType
anddidl.ItemType
in order to represent the complex typescontainer.type
anditem.type
, respectively.ContainerType
could look like this:I will use the
xjc
tool in the answer but the solution should be easily portable to Maven, for example.Using
xjc
, you need to generate your code like this to tale into account the bindings file and classes requested:As you can see, we provided both the bindings file,
binding.xml
, and theclasspath
where the generator should search for your custom classes.The same approach, although for simpler use cases, has been exemplified here in different questions in Stackoverflow, for example in these ones:
This Oracle article is quite dated, but it provides a good explanation about how the actual correspondence between fields happens.
请尝试在终端中使用以下内容。您应该创建所有所需的Java bean。
这样做的是使用XJC命令行工具从提供的XSD生成工件。您可以在此处阅读更多有关它的信息:使用JAXB XJC工具从XML架构文件生成JAXB类
Please try using the following in a terminal. You should have all the required Java Beans created.
What this does is uses the xjc command line tool to generate the artifacts from the supplied xsd. You can read more about it here: Using JAXB xjc tooling to generate JAXB classes from an XML schema file