1

在像 web-app_2_5.xsd 这样的 xsd 上使用 xjc (jaxb) 不会创建可用的映射

没有 @XmlAccessorType 也没有办法使用映射来 Marshall 和 UnMarshall XML。可以做些什么来修改这些映射以创建可用的文件?(ant、maven、xjc 命令行都给出相同的输出)

这是生成的 WebAppType 例如:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "web-appType", propOrder = {
    "descriptionAndDisplayNameAndIcon"
})
public class WebAppType {

    @XmlElementRefs({
        @XmlElementRef(name = "resource-env-ref", namespace = "http://java.sun.com/xml/ns/javaee", type = JAXBElement.class),
        @XmlElementRef(name = "post-construct", namespace = "http://java.sun.com/xml/ns/javaee", type = JAXBElement.class),
        @XmlElementRef(name = "icon", namespace = "http://java.sun.com/xml/ns/javaee", type = JAXBElement.class),
        @XmlElementRef(name = "error-page", namespace = "http://java.sun.com/xml/ns/javaee", type = JAXBElement.class),
        @XmlElementRef(name = "jsp-config", namespace = "http://java.sun.com/xml/ns/javaee", type = JAXBElement.class),
        @XmlElementRef(name = "message-destination", namespace = "http://java.sun.com/xml/ns/javaee", type = JAXBElement.class),
        @XmlElementRef(name = "display-name", namespace = "http://java.sun.com/xml/ns/javaee", type = JAXBElement.class),
        @XmlElementRef(name = "security-constraint", namespace = "http://java.sun.com/xml/ns/javaee", type = JAXBElement.class),
        @XmlElementRef(name = "message-destination-ref", namespace = "http://java.sun.com/xml/ns/javaee", type = JAXBElement.class),
        @XmlElementRef(name = "security-role", namespace = "http://java.sun.com/xml/ns/javaee", type = JAXBElement.class),
        @XmlElementRef(name = "pre-destroy", namespace = "http://java.sun.com/xml/ns/javaee", type = JAXBElement.class),
        @XmlElementRef(name = "filter", namespace = "http://java.sun.com/xml/ns/javaee", type = JAXBElement.class),
        @XmlElementRef(name = "env-entry", namespace = "http://java.sun.com/xml/ns/javaee", type = JAXBElement.class),
        @XmlElementRef(name = "ejb-ref", namespace = "http://java.sun.com/xml/ns/javaee", type = JAXBElement.class),
        @XmlElementRef(name = "filter-mapping", namespace = "http://java.sun.com/xml/ns/javaee", type = JAXBElement.class),
        @XmlElementRef(name = "mime-mapping", namespace = "http://java.sun.com/xml/ns/javaee", type = JAXBElement.class),
        @XmlElementRef(name = "distributable", namespace = "http://java.sun.com/xml/ns/javaee", type = JAXBElement.class),
        @XmlElementRef(name = "persistence-unit-ref", namespace = "http://java.sun.com/xml/ns/javaee", type = JAXBElement.class),
        @XmlElementRef(name = "login-config", namespace = "http://java.sun.com/xml/ns/javaee", type = JAXBElement.class),
        @XmlElementRef(name = "servlet", namespace = "http://java.sun.com/xml/ns/javaee", type = JAXBElement.class),
        @XmlElementRef(name = "ejb-local-ref", namespace = "http://java.sun.com/xml/ns/javaee", type = JAXBElement.class),
        @XmlElementRef(name = "description", namespace = "http://java.sun.com/xml/ns/javaee", type = JAXBElement.class),
        @XmlElementRef(name = "service-ref", namespace = "http://java.sun.com/xml/ns/javaee", type = JAXBElement.class),
        @XmlElementRef(name = "persistence-context-ref", namespace = "http://java.sun.com/xml/ns/javaee", type = JAXBElement.class),
        @XmlElementRef(name = "context-param", namespace = "http://java.sun.com/xml/ns/javaee", type = JAXBElement.class),
        @XmlElementRef(name = "resource-ref", namespace = "http://java.sun.com/xml/ns/javaee", type = JAXBElement.class),
        @XmlElementRef(name = "session-config", namespace = "http://java.sun.com/xml/ns/javaee", type = JAXBElement.class),
        @XmlElementRef(name = "locale-encoding-mapping-list", namespace = "http://java.sun.com/xml/ns/javaee", type = JAXBElement.class),
        @XmlElementRef(name = "servlet-mapping", namespace = "http://java.sun.com/xml/ns/javaee", type = JAXBElement.class),
        @XmlElementRef(name = "welcome-file-list", namespace = "http://java.sun.com/xml/ns/javaee", type = JAXBElement.class),
        @XmlElementRef(name = "listener", namespace = "http://java.sun.com/xml/ns/javaee", type = JAXBElement.class)
    })
    protected List<JAXBElement<?>> descriptionAndDisplayNameAndIcon;
    @XmlAttribute(name = "version", required = true)
    @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
    protected java.lang.String version;
    @XmlAttribute(name = "id")
    @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
    @XmlID
    @XmlSchemaType(name = "ID")
    protected java.lang.String id;
    @XmlAttribute(name = "metadata-complete")
    protected Boolean metadataComplete;

    /**
     * Gets the value of the descriptionAndDisplayNameAndIcon 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 descriptionAndDisplayNameAndIcon property.
     *
     * <p>
     * For example, to add a new item, do as follows:
     * <pre>
     *    getDescriptionAndDisplayNameAndIcon().add(newItem);
     * </pre>
     *
     *
     * <p>
     * Objects of the following type(s) are allowed in the list
     * {@link JAXBElement }{@code <}{@link ResourceEnvRefType }{@code >}
     * {@link JAXBElement }{@code <}{@link LifecycleCallbackType }{@code >}
     * {@link JAXBElement }{@code <}{@link IconType }{@code >}
     * {@link JAXBElement }{@code <}{@link ErrorPageType }{@code >}
     * {@link JAXBElement }{@code <}{@link JspConfigType }{@code >}
     * {@link JAXBElement }{@code <}{@link MessageDestinationType }{@code >}
     * {@link JAXBElement }{@code <}{@link DisplayNameType }{@code >}
     * {@link JAXBElement }{@code <}{@link SecurityConstraintType }{@code >}
     * {@link JAXBElement }{@code <}{@link MessageDestinationRefType }{@code >}
     * {@link JAXBElement }{@code <}{@link SecurityRoleType }{@code >}
     * {@link JAXBElement }{@code <}{@link LifecycleCallbackType }{@code >}
     * {@link JAXBElement }{@code <}{@link FilterType }{@code >}
     * {@link JAXBElement }{@code <}{@link EnvEntryType }{@code >}
     * {@link JAXBElement }{@code <}{@link EjbRefType }{@code >}
     * {@link JAXBElement }{@code <}{@link FilterMappingType }{@code >}
     * {@link JAXBElement }{@code <}{@link MimeMappingType }{@code >}
     * {@link JAXBElement }{@code <}{@link EmptyType }{@code >}
     * {@link JAXBElement }{@code <}{@link PersistenceUnitRefType }{@code >}
     * {@link JAXBElement }{@code <}{@link LoginConfigType }{@code >}
     * {@link JAXBElement }{@code <}{@link ServletType }{@code >}
     * {@link JAXBElement }{@code <}{@link EjbLocalRefType }{@code >}
     * {@link JAXBElement }{@code <}{@link DescriptionType }{@code >}
     * {@link JAXBElement }{@code <}{@link ServiceRefType }{@code >}
     * {@link JAXBElement }{@code <}{@link PersistenceContextRefType }{@code >}
     * {@link JAXBElement }{@code <}{@link ParamValueType }{@code >}
     * {@link JAXBElement }{@code <}{@link ResourceRefType }{@code >}
     * {@link JAXBElement }{@code <}{@link SessionConfigType }{@code >}
     * {@link JAXBElement }{@code <}{@link ServletMappingType }{@code >}
     * {@link JAXBElement }{@code <}{@link LocaleEncodingMappingListType }{@code >}
     * {@link JAXBElement }{@code <}{@link WelcomeFileListType }{@code >}
     * {@link JAXBElement }{@code <}{@link ListenerType }{@code >}
     *
     *
     */
    public List<JAXBElement<?>> getDescriptionAndDisplayNameAndIcon() {
        if (descriptionAndDisplayNameAndIcon == null) {
            descriptionAndDisplayNameAndIcon = new ArrayList<JAXBElement<?>>();
        }
        return this.descriptionAndDisplayNameAndIcon;
    }

    /**
     * Gets the value of the version property.
     *
     * @return
     *     possible object is
     *     {@link java.lang.String }
     *
     */
    public java.lang.String getVersion() {
        return version;
    }

    /**
     * Sets the value of the version property.
     *
     * @param value
     *     allowed object is
     *     {@link java.lang.String }
     *
     */
    public void setVersion(java.lang.String value) {
        this.version = value;
    }

    /**
     * Gets the value of the id property.
     *
     * @return
     *     possible object is
     *     {@link java.lang.String }
     *
     */
    public java.lang.String getId() {
        return id;
    }

    /**
     * Sets the value of the id property.
     *
     * @param value
     *     allowed object is
     *     {@link java.lang.String }
     *
     */
    public void setId(java.lang.String value) {
        this.id = value;
    }

    /**
     * Gets the value of the metadataComplete property.
     *
     * @return
     *     possible object is
     *     {@link Boolean }
     *
     */
    public Boolean isMetadataComplete() {
        return metadataComplete;
    }

    /**
     * Sets the value of the metadataComplete property.
     *
     * @param value
     *     allowed object is
     *     {@link Boolean }
     *
     */
    public void setMetadataComplete(Boolean value) {
        this.metadataComplete = value;
    }

}
4

1 回答 1

0

JAXB (JSR-222)实现将从任何XML 模式(包括 web-app_2_5.xsd)创建可用映射。

您需要JAXBContext在冒号 ( :) 分隔的所有生成的包名称列表中创建您的列表:

JAXBContext jc = JAXBContext.newInstance("org.example.foo:com.example.bar")

然后,您将能够通过执行以下操作将 XML 转换为 Java 对象(解组):

Unmarshaller unmarshaller = jc.createUnmarshaller();
Object result = unmarshaller.unmarshal(xml);

然后,您可以通过执行以下操作将对象写回 XML(编组):

Marshaller marshaller = jc.createMarshaller();
marshaller.marshal(result, System.out);
于 2013-10-23T14:30:37.957 回答