为什么XJC生成@XmlElementRefs(Why XJC generates @XmlElementRefs)
我正在使用和XJC实用程序从xsd架构的JAXB注释类,我有点惊讶,不明白为什么生成@XmlElementRefs。 所有这些都发生在以下xsd片段中(此处省略了名称空间)
<xs:complexType name="Header"> <xs:sequence> <xs:element name="CarriedOver" type="xs:boolean" minOccurs="1" maxOccurs="1"/> <xs:element name="CheckId" type="xs:string" minOccurs="1" maxOccurs="1"/> <xs:element name="Closed" type="xs:boolean" minOccurs="1" maxOccurs="1"/> <xs:element name="Dob" type="xs:string" minOccurs="1" maxOccurs="1"/> <xs:element name="Employees" type="lb:Employee" minOccurs="0" maxOccurs="unbounded"> <xs:annotation> <xs:documentation>List<Employee></xs:documentation> </xs:annotation> </xs:element> <xs:element name="GuestCountMode" type="ch:GuestCounting" minOccurs="1" maxOccurs="1"/> <xs:element name="Guests" type="xs:double" minOccurs="1" maxOccurs="1"/> <xs:element name="Managers" type="lb:Manager" minOccurs="0" maxOccurs="unbounded"> <xs:annotation> <xs:documentation>List<Manager></xs:documentation> </xs:annotation> </xs:element> <xs:element name="Period" type="ch:TypeVal" minOccurs="1" maxOccurs="1"/> <xs:element name="PrintedCheckId" type="xs:string" minOccurs="1" maxOccurs="1"/> <xs:element name="RevCenter" type="ch:TypeVal" minOccurs="1" maxOccurs="1"/> <xs:element name="Room" type="ch:TypeStrVal" minOccurs="1" maxOccurs="1"/> <xs:element name="StoreId" type="xs:string" minOccurs="1" maxOccurs="1"/> <xs:element name="Table" type="ch:TypeStrVal" minOccurs="1" maxOccurs="1"/> <xs:element name="TableType" type="ch:TableTypeDef" minOccurs="1" maxOccurs="1"/> <xs:element name="TerminalId" type="xs:string" minOccurs="1" maxOccurs="1"/> <xs:element name="Times" type="ch:TimeStamp" minOccurs="0" maxOccurs="unbounded"> <xs:annotation> <xs:documentation>List<TimeStamp></xs:documentation> </xs:annotation> </xs:element> <xs:element name="Training" type="xs:boolean" minOccurs="1" maxOccurs="1"/> <xs:element name="Managers" type="lb:Manager" minOccurs="0" maxOccurs="unbounded"/> <xs:element name="Times" type="ch:TimeStamp" minOccurs="0" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType>
xjc生成的java类如下:
package com.pif.check; import java.util.ArrayList; import java.util.List; import javax.xml.bind.JAXBElement; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElementRef; import javax.xml.bind.annotation.XmlElementRefs; import javax.xml.bind.annotation.XmlType; import com.pif.labor.Employee; import com.pif.labor.Manager; /** * <p>Java class for Header complex type. * * <p>The following schema fragment specifies the expected content contained within this class. * * <pre> * <complexType name="Header"> * <complexContent> * <restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> * <sequence> * <element name="CarriedOver" type="{http://www.w3.org/2001/XMLSchema}boolean"/> * <element name="CheckId" type="{http://www.w3.org/2001/XMLSchema}string"/> * <element name="Closed" type="{http://www.w3.org/2001/XMLSchema}boolean"/> * <element name="Dob" type="{http://www.w3.org/2001/XMLSchema}string"/> * <element name="Employees" type="{labor.pif.com}Employee" maxOccurs="unbounded" minOccurs="0"/> * <element name="GuestCountMode" type="{check.pif.com}GuestCounting"/> * <element name="Guests" type="{http://www.w3.org/2001/XMLSchema}double"/> * <element name="Managers" type="{labor.pif.com}Manager" maxOccurs="unbounded" minOccurs="0"/> * <element name="Period" type="{check.pif.com}TypeVal"/> * <element name="PrintedCheckId" type="{http://www.w3.org/2001/XMLSchema}string"/> * <element name="RevCenter" type="{check.pif.com}TypeVal"/> * <element name="Room" type="{check.pif.com}TypeStrVal"/> * <element name="StoreId" type="{http://www.w3.org/2001/XMLSchema}string"/> * <element name="Table" type="{check.pif.com}TypeStrVal"/> * <element name="TableType" type="{check.pif.com}TableTypeDef"/> * <element name="TerminalId" type="{http://www.w3.org/2001/XMLSchema}string"/> * <element name="Times" type="{check.pif.com}TimeStamp" maxOccurs="unbounded" minOccurs="0"/> * <element name="Training" type="{http://www.w3.org/2001/XMLSchema}boolean"/> * <element name="Managers" type="{labor.pif.com}Manager" maxOccurs="unbounded" minOccurs="0"/> * <element name="Times" type="{check.pif.com}TimeStamp" maxOccurs="unbounded" minOccurs="0"/> * </sequence> * </restriction> * </complexContent> * </complexType> * </pre> * * */ @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "Header", propOrder = { "content" }) public class Header { @XmlElementRefs({ @XmlElementRef(name = "Guests", type = JAXBElement.class, required = false), @XmlElementRef(name = "TableType", type = JAXBElement.class, required = false), @XmlElementRef(name = "PrintedCheckId", type = JAXBElement.class, required = false), @XmlElementRef(name = "RevCenter", type = JAXBElement.class, required = false), @XmlElementRef(name = "Dob", type = JAXBElement.class, required = false), @XmlElementRef(name = "Employees", type = JAXBElement.class, required = false), @XmlElementRef(name = "Room", type = JAXBElement.class, required = false), @XmlElementRef(name = "Table", type = JAXBElement.class, required = false), @XmlElementRef(name = "Closed", type = JAXBElement.class, required = false), @XmlElementRef(name = "GuestCountMode", type = JAXBElement.class, required = false), @XmlElementRef(name = "Times", type = JAXBElement.class, required = false), @XmlElementRef(name = "CheckId", type = JAXBElement.class, required = false), @XmlElementRef(name = "Period", type = JAXBElement.class, required = false), @XmlElementRef(name = "Managers", type = JAXBElement.class, required = false), @XmlElementRef(name = "StoreId", type = JAXBElement.class, required = false), @XmlElementRef(name = "TerminalId", type = JAXBElement.class, required = false), @XmlElementRef(name = "Training", type = JAXBElement.class, required = false), @XmlElementRef(name = "CarriedOver", type = JAXBElement.class, required = false) }) protected List<JAXBElement<?>> content; /** * Gets the rest of the content model. * * <p> * You are getting this "catch-all" property because of the following reason: * The field name "Managers" is used by two different parts of a schema. See: * line 160 of file:/C:/Users/JS185350/Documents/WorkSpace/WorkspaceEA/schemas/pif/check.xsd * line 141 of file:/C:/Users/JS185350/Documents/WorkSpace/WorkspaceEA/schemas/pif/check.xsd * <p> * To get rid of this property, apply a property customization to one * of both of the following declarations to change their names: * Gets the value of the content 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 content property. * * <p> * For example, to add a new item, do as follows: * <pre> * getContent().add(newItem); * </pre> * * * <p> * Objects of the following type(s) are allowed in the list * {@link JAXBElement }{@code <}{@link Double }{@code >} * {@link JAXBElement }{@code <}{@link String }{@code >} * {@link JAXBElement }{@code <}{@link TableTypeDef }{@code >} * {@link JAXBElement }{@code <}{@link TypeVal }{@code >} * {@link JAXBElement }{@code <}{@link Employee }{@code >} * {@link JAXBElement }{@code <}{@link String }{@code >} * {@link JAXBElement }{@code <}{@link TypeStrVal }{@code >} * {@link JAXBElement }{@code <}{@link Boolean }{@code >} * {@link JAXBElement }{@code <}{@link TypeStrVal }{@code >} * {@link JAXBElement }{@code <}{@link GuestCounting }{@code >} * {@link JAXBElement }{@code <}{@link TypeVal }{@code >} * {@link JAXBElement }{@code <}{@link String }{@code >} * {@link JAXBElement }{@code <}{@link TimeStamp }{@code >} * {@link JAXBElement }{@code <}{@link Manager }{@code >} * {@link JAXBElement }{@code <}{@link String }{@code >} * {@link JAXBElement }{@code <}{@link String }{@code >} * {@link JAXBElement }{@code <}{@link Boolean }{@code >} * {@link JAXBElement }{@code <}{@link Boolean }{@code >} * * */ public List<JAXBElement<?>> getContent() { if (content == null) { content = new ArrayList<JAXBElement<?>>(); } return this.content; } }
对我来说,似乎奇怪的是所有内容都包含在JaxbElement中 - 这很难处理。 我希望有类似的东西:
public class Header { private Boolean CarriedOver; private String CheckId; private Boolean Closed; private String Dob; private List<Employee> Employees; ... propper getters and setters ... }
有什么我想念的吗?
I am using and XJC utility for JAXB annotated classes from xsd schema and I am a bit surprised and don't understand why @XmlElementRefs gets generated. All happens in following xsd snippet (namespaces omitted here)
<xs:complexType name="Header"> <xs:sequence> <xs:element name="CarriedOver" type="xs:boolean" minOccurs="1" maxOccurs="1"/> <xs:element name="CheckId" type="xs:string" minOccurs="1" maxOccurs="1"/> <xs:element name="Closed" type="xs:boolean" minOccurs="1" maxOccurs="1"/> <xs:element name="Dob" type="xs:string" minOccurs="1" maxOccurs="1"/> <xs:element name="Employees" type="lb:Employee" minOccurs="0" maxOccurs="unbounded"> <xs:annotation> <xs:documentation>List<Employee></xs:documentation> </xs:annotation> </xs:element> <xs:element name="GuestCountMode" type="ch:GuestCounting" minOccurs="1" maxOccurs="1"/> <xs:element name="Guests" type="xs:double" minOccurs="1" maxOccurs="1"/> <xs:element name="Managers" type="lb:Manager" minOccurs="0" maxOccurs="unbounded"> <xs:annotation> <xs:documentation>List<Manager></xs:documentation> </xs:annotation> </xs:element> <xs:element name="Period" type="ch:TypeVal" minOccurs="1" maxOccurs="1"/> <xs:element name="PrintedCheckId" type="xs:string" minOccurs="1" maxOccurs="1"/> <xs:element name="RevCenter" type="ch:TypeVal" minOccurs="1" maxOccurs="1"/> <xs:element name="Room" type="ch:TypeStrVal" minOccurs="1" maxOccurs="1"/> <xs:element name="StoreId" type="xs:string" minOccurs="1" maxOccurs="1"/> <xs:element name="Table" type="ch:TypeStrVal" minOccurs="1" maxOccurs="1"/> <xs:element name="TableType" type="ch:TableTypeDef" minOccurs="1" maxOccurs="1"/> <xs:element name="TerminalId" type="xs:string" minOccurs="1" maxOccurs="1"/> <xs:element name="Times" type="ch:TimeStamp" minOccurs="0" maxOccurs="unbounded"> <xs:annotation> <xs:documentation>List<TimeStamp></xs:documentation> </xs:annotation> </xs:element> <xs:element name="Training" type="xs:boolean" minOccurs="1" maxOccurs="1"/> <xs:element name="Managers" type="lb:Manager" minOccurs="0" maxOccurs="unbounded"/> <xs:element name="Times" type="ch:TimeStamp" minOccurs="0" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType>
Generated java class by xjc is following:
package com.pif.check; import java.util.ArrayList; import java.util.List; import javax.xml.bind.JAXBElement; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElementRef; import javax.xml.bind.annotation.XmlElementRefs; import javax.xml.bind.annotation.XmlType; import com.pif.labor.Employee; import com.pif.labor.Manager; /** * <p>Java class for Header complex type. * * <p>The following schema fragment specifies the expected content contained within this class. * * <pre> * <complexType name="Header"> * <complexContent> * <restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> * <sequence> * <element name="CarriedOver" type="{http://www.w3.org/2001/XMLSchema}boolean"/> * <element name="CheckId" type="{http://www.w3.org/2001/XMLSchema}string"/> * <element name="Closed" type="{http://www.w3.org/2001/XMLSchema}boolean"/> * <element name="Dob" type="{http://www.w3.org/2001/XMLSchema}string"/> * <element name="Employees" type="{labor.pif.com}Employee" maxOccurs="unbounded" minOccurs="0"/> * <element name="GuestCountMode" type="{check.pif.com}GuestCounting"/> * <element name="Guests" type="{http://www.w3.org/2001/XMLSchema}double"/> * <element name="Managers" type="{labor.pif.com}Manager" maxOccurs="unbounded" minOccurs="0"/> * <element name="Period" type="{check.pif.com}TypeVal"/> * <element name="PrintedCheckId" type="{http://www.w3.org/2001/XMLSchema}string"/> * <element name="RevCenter" type="{check.pif.com}TypeVal"/> * <element name="Room" type="{check.pif.com}TypeStrVal"/> * <element name="StoreId" type="{http://www.w3.org/2001/XMLSchema}string"/> * <element name="Table" type="{check.pif.com}TypeStrVal"/> * <element name="TableType" type="{check.pif.com}TableTypeDef"/> * <element name="TerminalId" type="{http://www.w3.org/2001/XMLSchema}string"/> * <element name="Times" type="{check.pif.com}TimeStamp" maxOccurs="unbounded" minOccurs="0"/> * <element name="Training" type="{http://www.w3.org/2001/XMLSchema}boolean"/> * <element name="Managers" type="{labor.pif.com}Manager" maxOccurs="unbounded" minOccurs="0"/> * <element name="Times" type="{check.pif.com}TimeStamp" maxOccurs="unbounded" minOccurs="0"/> * </sequence> * </restriction> * </complexContent> * </complexType> * </pre> * * */ @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "Header", propOrder = { "content" }) public class Header { @XmlElementRefs({ @XmlElementRef(name = "Guests", type = JAXBElement.class, required = false), @XmlElementRef(name = "TableType", type = JAXBElement.class, required = false), @XmlElementRef(name = "PrintedCheckId", type = JAXBElement.class, required = false), @XmlElementRef(name = "RevCenter", type = JAXBElement.class, required = false), @XmlElementRef(name = "Dob", type = JAXBElement.class, required = false), @XmlElementRef(name = "Employees", type = JAXBElement.class, required = false), @XmlElementRef(name = "Room", type = JAXBElement.class, required = false), @XmlElementRef(name = "Table", type = JAXBElement.class, required = false), @XmlElementRef(name = "Closed", type = JAXBElement.class, required = false), @XmlElementRef(name = "GuestCountMode", type = JAXBElement.class, required = false), @XmlElementRef(name = "Times", type = JAXBElement.class, required = false), @XmlElementRef(name = "CheckId", type = JAXBElement.class, required = false), @XmlElementRef(name = "Period", type = JAXBElement.class, required = false), @XmlElementRef(name = "Managers", type = JAXBElement.class, required = false), @XmlElementRef(name = "StoreId", type = JAXBElement.class, required = false), @XmlElementRef(name = "TerminalId", type = JAXBElement.class, required = false), @XmlElementRef(name = "Training", type = JAXBElement.class, required = false), @XmlElementRef(name = "CarriedOver", type = JAXBElement.class, required = false) }) protected List<JAXBElement<?>> content; /** * Gets the rest of the content model. * * <p> * You are getting this "catch-all" property because of the following reason: * The field name "Managers" is used by two different parts of a schema. See: * line 160 of file:/C:/Users/JS185350/Documents/WorkSpace/WorkspaceEA/schemas/pif/check.xsd * line 141 of file:/C:/Users/JS185350/Documents/WorkSpace/WorkspaceEA/schemas/pif/check.xsd * <p> * To get rid of this property, apply a property customization to one * of both of the following declarations to change their names: * Gets the value of the content 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 content property. * * <p> * For example, to add a new item, do as follows: * <pre> * getContent().add(newItem); * </pre> * * * <p> * Objects of the following type(s) are allowed in the list * {@link JAXBElement }{@code <}{@link Double }{@code >} * {@link JAXBElement }{@code <}{@link String }{@code >} * {@link JAXBElement }{@code <}{@link TableTypeDef }{@code >} * {@link JAXBElement }{@code <}{@link TypeVal }{@code >} * {@link JAXBElement }{@code <}{@link Employee }{@code >} * {@link JAXBElement }{@code <}{@link String }{@code >} * {@link JAXBElement }{@code <}{@link TypeStrVal }{@code >} * {@link JAXBElement }{@code <}{@link Boolean }{@code >} * {@link JAXBElement }{@code <}{@link TypeStrVal }{@code >} * {@link JAXBElement }{@code <}{@link GuestCounting }{@code >} * {@link JAXBElement }{@code <}{@link TypeVal }{@code >} * {@link JAXBElement }{@code <}{@link String }{@code >} * {@link JAXBElement }{@code <}{@link TimeStamp }{@code >} * {@link JAXBElement }{@code <}{@link Manager }{@code >} * {@link JAXBElement }{@code <}{@link String }{@code >} * {@link JAXBElement }{@code <}{@link String }{@code >} * {@link JAXBElement }{@code <}{@link Boolean }{@code >} * {@link JAXBElement }{@code <}{@link Boolean }{@code >} * * */ public List<JAXBElement<?>> getContent() { if (content == null) { content = new ArrayList<JAXBElement<?>>(); } return this.content; } }
To me it seems strange that all is wrapped in JaxbElement - it is hard to work with that. I would expect something like:
public class Header { private Boolean CarriedOver; private String CheckId; private Boolean Closed; private String Dob; private List<Employee> Employees; ... propper getters and setters ... }
Is there something I am missing?
原文:https://stackoverflow.com/questions/20123171
最满意答案
你可以做几件事,但最好的事情可能是这样做:
public class CharacterControl { void Update() { GameObject.FindGameObjectByTag("Tag-of-scoremanager-").GetComponent <ScoreManager> ().Score += price; } }
另一种方法是将分数管理器作为检查器属性包括在内,如下所示:
ScoreManager manager;
然后将得分管理器预制件拖放到检查器中,然后增加它只需执行以下操作:
manager.Score += price;
There are several things that you could do, but the best thing is probably to do something like this:
public class CharacterControl { void Update() { GameObject.FindGameObjectByTag("Tag-of-scoremanager-").GetComponent <ScoreManager> ().Score += price; } }
Another way is for example to include the Score manager as an inspector property, like so:
ScoreManager manager;
and then drag and drop the score manager prefab into the inspector, and then to increment it simply do:
manager.Score += price;
相关问答
更多-
通过定位预制件的实例而不是预制件本身来更改对象的颜色。 使用Raycast找到游戏对象。 (由于您正在响应鼠标单击事件,我假设您正在选择要更改颜色的对象) private RaycastHit hit; void Update(){ Physics.Raycast (Camera.main.ScreenPointToRay (Input.mousePosition), out hit); if (Input.GetMouseButtonDown(1) && hit.collid ...
-
我下载了项目并注意到顶部和左侧的生成点以错误的方式旋转。 左旋转应为:270(或-90) 顶部旋转应为:180 就个人而言,我会失去所有的旋转并使用transform.right等来避免混淆 I downloaded the project and noticed that the top & left spawnpoints were rotated the wrong way. The left rotation should be: 270 (or -90) The top rotation shou ...
-
只需单击此按钮即可保存预制件: Simply click on this button will save your prefab:
-
预制件内的相机旋转(Camera rotation within a prefab)[2023-03-15]
但是现在我面临的问题是我的相机旋转也受到我的预制父级旋转的影响 那是因为你的Camera是玩家的孩子所以它会做任何玩家/父母正在做的事情。 你的相机不应该在播放器下面。 删除它并确保它不是播放器或任何其他对象的子节点。 该脚本应该工作或使相机跟随播放器而不使Camera成为GameObject的孩子。 删除Player = GameObject.Find ("Player"); 来自LateUpdate函数。 您已在Start函数中完成此操作,因此无需在LateUpdate函数中执行此LateUpdate ... -
您可以尝试将简单的销毁脚本添加到您的死亡动画对象中,该对象会在一段时间内销毁对象或在动画中触发它( Unity手册:使用动画事件 )。 当您实例化对象时,它将出现在所需的位置,无论“主”对象如何,它都将被破坏。 像这样销毁脚本: void DestroyMyObject() { Destroy(gameObject); } 脚本运行时间: void Start() { Invoke ("DestroyMyObject", 1f); } void DestroyMyObject() ...
-
通常我认为最好的方法是创建一个预制件然后给它一个公共组件或脚本,它可以接收消息并更改私有组件来控制你想要更改的方面。 在您描述的实例中,您只想更改图像组件,可以在实例化预制件时更改该组件。 问题变成了一个设计问题:您是否更愿意在实例化后从预制件中实例化游戏对象的脚本中更改属性(在本例中为图像组件)? 或者向预制件上的脚本发送消息,然后对该消息作出反应,相应地设置其属性? 您可以在此处看到有关实例化的相当全面和多样化的讨论。 我认为通常的共识是你不希望在大量的预制件上添加组件以“专门化”它们,并且你不想一次性 ...
-
选择特定的预制件(Selecting specific prefab)[2022-06-18]
在场景中的游戏对象上这样的东西(可能是一个用作控制器的空游戏对象): 请注意,如果使用默认的摄像头方向,则光线投射的方向应为-Vector2.up 。 public class RayShooter : MonoBehaviour { void Update() { if(Input.GetMouseButtonDown(1)) // 1 is rightclick, don't know if you might want leftclick instead ... -
来自预制件的RaycastAll没有从另一个预制件中拾取对撞机(RaycastAll from prefab is not picking up a collider from another prefab)[2022-02-14]
解决了! 我没有意识到有一个名为Debug.DrawRay的函数。 本质上,我没有意识到光线和线条的坐标不同。 我为测试目的绘制的线在局部空间中有一个特定点,因为它是第二个参数,与drawray一样,它是一个方向。 知道了这一点,我能够确定拳击的轨迹,就像之前一个朋克的拳头在完全狂野的方向上一样。 Solved it! I didn't realize that there was a function named Debug.DrawRay. in essence, I didn't realize th ... -
如何将可变信息从一个预制件传输到另一个预制件?(How do I transfer variable information from one prefab to another?)[2022-10-30]
你可以做几件事,但最好的事情可能是这样做: public class CharacterControl { void Update() { GameObject.FindGameObjectByTag("Tag-of-scoremanager-").GetComponent().Score += price; } } 另一种方法是将分数管理器作为检查器属性包括在内,如下所示: ScoreManager manager; 然后将 ... -
给每个预制件自己的速度(Give every prefab his own speed)[2023-01-14]
咦? 所以速度被覆盖并最终在所有实例化的预制件上都是相同的? 也许在Start方法中初始化speed变量时会有所帮助。 Huh? So the speed is overwritten and eventually ends up being the same on all instantiated prefabs? Maybe it helps if initialize the speed variable inside the Start method.