3

I have a Class named MYClass whose code is given below

package com.rest;


public class MyClass {

    private String var;

    public String getVar() {
        return var;
    }

    public void setVar(String var) {
        this.var = var;
    }
}

I have created its schema using schemagen ../src/com/rest/MyClass.java Generated Schema :

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:complexType name="myClass">
    <xs:sequence>
      <xs:element name="var" type="xs:string" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

Then, i have created JAXB artifacts by schema using

xjc -d <my_source_dir>\ -p com.rest.generated <my_generated_schema>.xsd

The generated artifacts code is given below

ObjectFactory :

//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6 
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
// Any modifications to this file will be lost upon recompilation of the source schema. 
// Generated on: 2012.06.01 at 08:56:31 PM PKT 
//


package com.rest.generated;

import javax.xml.bind.annotation.XmlRegistry;


/**
 * This object contains factory methods for each 
 * Java content interface and Java element interface 
 * generated in the com.rest.generated package. 
 * <p>An ObjectFactory allows you to programatically 
 * construct new instances of the Java representation 
 * for XML content. The Java representation of XML 
 * content can consist of schema derived interfaces 
 * and classes representing the binding of schema 
 * type definitions, element declarations and model 
 * groups.  Factory methods for each of these are 
 * provided in this class.
 * 
 */
@XmlRegistry
public class ObjectFactory {


    /**
     * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: com.rest.generated
     * 
     */
    public ObjectFactory() {
    }

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

}

and MyClass.java is

//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6 
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
// Any modifications to this file will be lost upon recompilation of the source schema. 
// Generated on: 2012.06.01 at 08:56:31 PM PKT 
//


package com.rest.generated;

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


/**
 * <p>Java class for myClass complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType name="myClass">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="var" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "myClass", propOrder = {
    "var"
})
public class MyClass {

    protected String var;

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

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

}

Problem : Cannot Found a method which creates JAXBElement. What should i do to get that method

4

3 に答える 3

1

XJCは、必要な場合にのみフィールドをJAXBElementsとして作成します。実際には、マッピングに「通常の」JavaBeanセマンティクスで表現できないメタ情報が必要な場合にのみ必要です。

これが必要な場合の例は、フィールドがnillableでminOccurs=0の場合です。このような要素を定義すると、ターゲットタイプではなくフィールドタイプとしてJAXBElementが取得されます。これにより、「nil」と「提供されていない」を区別できます。これは、おそらく実行する必要があります(そうでない場合は、スキーマでそのように定義していません)。

于 2012-06-01T16:35:45.093 に答える
0

MyClassのインスタンスを作成したい場合。

MyClass mc = ObjectFactory.createMyClass()

クラスから XML を作成する場合はマーシャラーを使用し、XML ファイルから MyClass のオブジェクトを作成する場合はアンマーシャラーを使用します。JEE5 ドキュメント

于 2012-06-01T16:42:02.143 に答える