繁体   English   中英

使用 JAXB 解组 xml 文件

[英]Using JAXB to unmarshal xml file

我需要将archetype-catalog.xml文件的内容解析为 Java 对象结构。 为此,我想我会使用旧的JAXB 因此,我查找了 xml 文件的xsd定义并从中生成了 jaxb 类:

原型目录.java

import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;


/**
 *  0.0.0+
 * 
 * <p>Java class for ArchetypeCatalog complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType name="ArchetypeCatalog">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;all>
 *         &lt;element name="archetypes" minOccurs="0">
 *           &lt;complexType>
 *             &lt;complexContent>
 *               &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *                 &lt;sequence>
 *                   &lt;element name="archetype" type="{http://maven.apache.org/plugins/maven-archetype-plugin/archetype-catalog/1.0.0}Archetype" maxOccurs="unbounded" minOccurs="0"/>
 *                 &lt;/sequence>
 *               &lt;/restriction>
 *             &lt;/complexContent>
 *           &lt;/complexType>
 *         &lt;/element>
 *       &lt;/all>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ArchetypeCatalog", propOrder = {

})
public class ArchetypeCatalog {

    protected ArchetypeCatalog.Archetypes archetypes;

    /**
     * Gets the value of the archetypes property.
     * 
     * @return
     *     possible object is
     *     {@link ArchetypeCatalog.Archetypes }
     *     
     */
    public ArchetypeCatalog.Archetypes getArchetypes() {
        return archetypes;
    }

    /**
     * Sets the value of the archetypes property.
     * 
     * @param value
     *     allowed object is
     *     {@link ArchetypeCatalog.Archetypes }
     *     
     */
    public void setArchetypes(ArchetypeCatalog.Archetypes value) {
        this.archetypes = value;
    }


    /**
     * <p>Java class for anonymous complex type.
     * 
     * <p>The following schema fragment specifies the expected content contained within this class.
     * 
     * <pre>
     * &lt;complexType>
     *   &lt;complexContent>
     *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
     *       &lt;sequence>
     *         &lt;element name="archetype" type="{http://maven.apache.org/plugins/maven-archetype-plugin/archetype-catalog/1.0.0}Archetype" maxOccurs="unbounded" minOccurs="0"/>
     *       &lt;/sequence>
     *     &lt;/restriction>
     *   &lt;/complexContent>
     * &lt;/complexType>
     * </pre>
     * 
     * 
     */
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "archetype"
    })
    public static class Archetypes {

        protected List<Archetype> archetype;

        /**
         * Gets the value of the archetype 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 archetype property.
         * 
         * <p>
         * For example, to add a new item, do as follows:
         * <pre>
         *    getArchetype().add(newItem);
         * </pre>
         * 
         * 
         * <p>
         * Objects of the following type(s) are allowed in the list
         * {@link Archetype }
         * 
         * 
         */
        public List<Archetype> getArchetype() {
            if (archetype == null) {
                archetype = new ArrayList<Archetype>();
            }
            return this.archetype;
        }

    }

}

原型.java

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;


/**
 * 
 *              Informations to point to an Archetype referenced in the catalog.
 *          
 * 
 * <p>Java class for Archetype complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType name="Archetype">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;all>
 *         &lt;element name="groupId" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
 *         &lt;element name="artifactId" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
 *         &lt;element name="version" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
 *         &lt;element name="repository" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
 *         &lt;element name="description" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
 *       &lt;/all>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Archetype", propOrder = {

})
public class Archetype {

    protected String groupId;
    protected String artifactId;
    protected String version;
    protected String repository;
    protected String description;

    /**
     * Gets the value of the groupId property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getGroupId() {
        return groupId;
    }

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

    /**
     * Gets the value of the artifactId property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getArtifactId() {
        return artifactId;
    }

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

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

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

    /**
     * Gets the value of the repository property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getRepository() {
        return repository;
    }

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

    /**
     * Gets the value of the description property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getDescription() {
        return description;
    }

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

}

对象工厂.java

import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlElementDecl;
import javax.xml.bind.annotation.XmlRegistry;
import javax.xml.namespace.QName;


@XmlRegistry
public class ObjectFactory {

    private final static QName _ArchetypeCatalog_QNAME = new QName("http://maven.apache.org/plugins/maven-archetype-plugin/archetype-catalog/1.0.0", "archetype-catalog");


    public ObjectFactory() {
    }

    /**
     * Create an instance of {@link ArchetypeCatalog }
     * 
     */
    public ArchetypeCatalog createArchetypeCatalog() {
        return new ArchetypeCatalog();
    }

    /**
     * Create an instance of {@link Archetype }
     * 
     */
    public Archetype createArchetype() {
        return new Archetype();
    }

    /**
     * Create an instance of {@link ArchetypeCatalog.Archetypes }
     * 
     */
    public ArchetypeCatalog.Archetypes createArchetypeCatalogArchetypes() {
        return new ArchetypeCatalog.Archetypes();
    }

    /**
     * Create an instance of {@link JAXBElement }{@code <}{@link ArchetypeCatalog }{@code >}}
     * 
     */
    @XmlElementDecl(namespace = "http://maven.apache.org/plugins/maven-archetype-plugin/archetype-catalog/1.0.0", name = "archetype-catalog")
    public JAXBElement<ArchetypeCatalog> createArchetypeCatalog(ArchetypeCatalog value) {
        return new JAXBElement<ArchetypeCatalog>(_ArchetypeCatalog_QNAME, ArchetypeCatalog.class, null, value);
    }

}

但是,当实际解组用于测试的示例文件时,我得到以下信息:

堆栈跟踪:

Exception in thread "main" java.lang.RuntimeException: an error occurred while unmarshalling xml into com.catalogupdater.plugin.jaxb.ArchetypeCatalog object
    at com.catalogupdater.plugin.converter.JAXBConverter.stringToObject(JAXBConverter.java:48)
    at Main.main(Main.java:60)
Caused by: javax.xml.bind.UnmarshalException: unexpected element (uri:"http://maven.apache.org/plugins/maven-archetype-plugin/archetype-catalog/1.0.0", local:"archetype-catalog"). Expected elements are (none)

任何建议这里出了什么问题? 亲切的问候

编辑:以下是我使用的完整解组逻辑:

   public static <T> T stringToObject( final String textClazz, final Class<T> clazz )
   {
      T returnedValue = null;
      try
      {
         final JAXBContext jaxbContext = JAXBContext.newInstance( clazz );
         final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
         final StringReader reader = new StringReader( textClazz );
         returnedValue = ( T ) unmarshaller.unmarshal( reader );
      }
      catch ( final JAXBException e )
      {
         throw new RuntimeException(
               "an error occurred while unmarshalling xml into " + clazz.getCanonicalName() + " object", e );
      }
      return returnedValue;
   }

你应该做的事情如下:

     final JAXBContext jaxbContext = JAXBContext.newInstance( clazz.getPackage().getName() );
     final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
     final StringReader reader = new StringReader( textClazz );
     returnedValue = ( T ) unmarshaller.unmarshal( reader ).getValue();

显然,如果您只创建clazz JAXBContext ,则不考虑ObjectFactory ObjectFactory是一个所谓的“XML注册表”,它说明哪个类应该用于哪个根元素。

你从解组中获得的是JAXBContext<SomeType> ,它包含SomeType类型的值(在你的例子中是ArchetypeCatalog )以及根元素的名称。 您可以通过getValue()获得您感兴趣的价值。

你在答案中所做的也有效,但这不是“JAXB方式”。

您手动添加了@XmlRootElement注释,这对生成的代码肯定不好。 您可以通过插件添加它,如jaxb2-annotate-plugin (免责声明:我是作者)。

但最终你应该更好地使用ObjectFactory因为它就是它的用途。 长期坚持标准模式你会更好。

我已经发现生成的ArchetypeCatalog类中缺少以下@RootElement Annotation:

@XmlRootElement(namespace = "http://maven.apache.org/plugins/maven-archetype-plugin/archetype-catalog/1.0.0", name = "archetype-catalog")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ArchetypeCatalog", propOrder = {

})
public class ArchetypeCatalog
{

 ...
}

我手动添加它,现在它工作正常。 不知道为什么不生成这个。 尽管如此,毕竟我认为从xsd规范生成类需要谨慎对待。

亲切的问候

编辑 :正如@ulab在评论中所述, 这里给出了缺少@RootElement的原因。

编辑 :此外,手动放置@XMLRootElement注释不是必需的或不可取的。 更多细节可以在@lexicor写的答案中找到。

另一种选择是首先检查用于生成类的 XJC 的版本和执行它的库的版本是否相同。 同样的事情发生在我身上,足以保证两个版本相同。 您可以在生成的类的初始注释和用于执行这些类的库的版本中看到此信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM