本文介绍了使用JAXB编组一个只包含多个子节点类型之一的XML节点(SharePoint查询)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在尝试创建基于Microsoft SharePoint生成XML的JAXB注释类查询架构。我有一个 SpWhereClause 类:I'm trying to create JAXB annotated classes to generate XML based on the Microsoft SharePoint Query schema. I have an SpWhereClause class:@XmlType(name="Where")@XmlAccessorType(XmlAccessType.FIELD)public class SpWhereClause {}但我不确定如何构造/注释其属性。 < Where> 元素可以包含许多不同类型的子元素(< Eq> ,< BeginsWith> ,<包含> 等。让我们忽略< And> 和< Or> 现在),但不超过一个。 Eq 和 BeginsWith 每个单独有效的其中的子元素,但它不能是这样的:But I'm not sure how to structure/annotate its properties. A <Where> element can have many different types of child elements (<Eq>, <BeginsWith>, <Contains> etc. Let's ignore <And> and <Or> for now), but not more than one. Eq and BeginsWith are each individually valid children of Where, but it can't be like this:<Where> <Eq>...</Eq> <BeginsWith>...</BeginsWith></Where>没有嵌套< Eq> 和< BeginsWith> 在< Or> 元素中。我的第一个想法是使用< FieldRef> 和 AbstractSpComparison 类>< Value> 所有比较共有的元素:My first thought was to create an AbstractSpComparison class with the <FieldRef> and <Value> elements common to all comparisons:@XmlAccessorType(XmlAccessType.FIELD)public abstract class AbstractSpComparison { @XmlElement private SpFieldRef fieldRef; @XmlElement private SpValue value; ...}然后有 SpEqualsComparison 扩展它:@XmlType(name="Eq")public class SpEqualsComparison extends AbstractSpComparison {}但是,使用摘要 SpWhereClause 中的类让我无法控制子元素的名称。这个:However, using the abstract class in SpWhereClause leaves me unable to control the name of the child element. This:@XmlElementprivate AbstractSpComparison comparison;结果如下:<Where> <comparison>...</comparison></Where>而不是:<WHERE> <Eq>...</Eq></WHERE>为什么不是的Eq名称SpEqualsComparison 用于命名比较元素?处理这种情况的正确方法是什么?Why isn't the "Eq" name from SpEqualsComparison being used to name the comparison element? What's the right way to handle a situation like that?我认为只将每个可能的子元素类型作为 SpWhereClause ,并且只设置我需要的那个(在某处有一些验证逻辑),但这似乎不必要地冗长。I considered just having every possible child element type as a property of SpWhereClause, and only set the one I need (with some validation logic somewhere), but that seems unnecessarily verbose.如果重要,我正在使用 Spring OXM Jaxb2Marshaller 。If it matters, I'm using a Spring OXM Jaxb2Marshaller.推荐答案您可以选择几种不同的选项:There are a couple of different options you could do:如果一切都延伸了通用超类(即AbstractSpComparison)然后你可以利用 @XmlElementRef 注释。执行此操作时,子元素将对应于引用对象的类上的 @XmlRootElement 注释的值。If everything extends a common super class (i.e. AbstractSpComparison) then you could leverage the @XmlElementRef annotation. When you do this the child element will correspond to the value of the @XmlRootElement annotation on the class of the referenced object.@XmlElementRefprivate AbstractSpComparison comparison; 更多信息我在博客上写了更多关于这种方法的文章:I have written more about this approach on my blog: http://blog.bdoughan.com/2010/11/jaxb-and-inheritance-using-substitution.htmlhttp://blog.bdoughan.com/2010/11/jaxb-and-inheritance-using-substitution.html如果一切都没有扩展普通的超类,那么你可以利用 @XmlElements 注释,你可以明确地注释哪些元素可以出现,他们对应的是什么类:If everything doesn't extend a common super class then you can leverage the @XmlElements annotation where you explicitly annotate which elements could appear and what class they correspond to:@XmlElements({ @XmlElement(name="Eq", type=Eq.class), @XmlElement(name="BeginsWith", type=BeginsWith.class)})private AbstractSpComparison comparison; 更多信息我在博客上写了更多关于这种方法的文章:I have written more about this approach on my blog: http://blog.bdoughan.com/2010/10/jaxb-and-xsd-choice-xmlelements.htmlhttp://blog.bdoughan.com/2010/10/jaxb-and-xsd-choice-xmlelements.html 这篇关于使用JAXB编组一个只包含多个子节点类型之一的XML节点(SharePoint查询)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-03 18:25
查看更多